Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Realizedd committed Aug 10, 2018
1 parent 6e38c80 commit 1a96865
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 65 deletions.
6 changes: 0 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ repositories {
url 'https://jitpack.io'
}

maven {
name 'sk89q-repo'
url 'http://maven.sk89q.com/repo/'
}

flatDir {
dirs 'libs'
}
Expand All @@ -45,7 +40,6 @@ dependencies {
compile 'org.projectlombok:lombok:1.16.20'
compile 'org.spigotmc:spigot-api:1.13-R0.1-SNAPSHOT'
compile 'com.github.RealizedMC.Duels:duels-api:v3.1.2'
compile 'com.sk89q:worldguard:6.1.1-SNAPSHOT'
compile name: 'spigot-1.7.10'
compile name: 'spigot-1.12.2'
compile name: 'spigot-1.13'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

public abstract class ARCommand {

protected final ArenaRegen extension;
protected final Duels api;
protected final ArenaManager arenaManager;
protected final ResetZoneManager zoneManager;

Expand All @@ -26,8 +24,6 @@ public abstract class ARCommand {
private final boolean playerOnly;

protected ARCommand(final ArenaRegen extension, final Duels api, final String name, final String usage, final String description, final int length, final boolean playerOnly) {
this.extension = extension;
this.api = api;
this.arenaManager = api.getArenaManager();
this.zoneManager = extension.getZoneManager();
this.name = name;
Expand Down
49 changes: 19 additions & 30 deletions src/main/java/me/realized/de/arenaregen/util/compat/NMSUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,6 @@ public final class NMSUtil {
}
}

public static void updateLighting(final Block block) {
final int x = block.getX();
final int y = block.getY();
final int z = block.getZ();

if (block.getType().name().contains("AIR") || !canAffectLighting(block.getWorld(), x, y, z)) {
return;
}

try {
if (CompatUtil.isPre1_8()) {
C_17.invoke(GET_HANDLE_WORLD.invoke(block.getWorld()), SKY_BLOCK_ENUM, x, y, z);
} else {
C.invoke(GET_HANDLE_WORLD.invoke(block.getWorld()), SKY_BLOCK_ENUM, BLOCK_POS_CONSTRUCTOR.newInstance(x, y, z));
}
} catch (Throwable ex) {
ex.printStackTrace();
}
}

public static void setBlockFast(final Block block, final Material material, final int data) {
final Chunk chunk = block.getChunk();
final int x = block.getX();
Expand All @@ -81,6 +61,12 @@ public static void setBlockFast(final Block block, final Material material, fina

if (CompatUtil.isPre1_8()) {
SET_BLOCK_7.invoke(chunkHandle, x & 0x0F, y, z & 0x0F, nmsBlock, data);

if (block.getType().name().contains("AIR") || isSurrounded(block.getWorld(), x, y, z)) {
return;
}

C_17.invoke(GET_HANDLE_WORLD.invoke(block.getWorld()), SKY_BLOCK_ENUM, x, y, z);
} else {
final Object blockPos = BLOCK_POS_CONSTRUCTOR.newInstance(x, y, z);

Expand All @@ -90,29 +76,32 @@ public static void setBlockFast(final Block block, final Material material, fina
} else {
SET_BLOCK.invoke(chunkHandle, blockPos, GET_BLOCK_DATA.invoke(nmsBlock), true);
}

if (block.getType().name().contains("AIR") || isSurrounded(block.getWorld(), x, y, z)) {
return;
}

C.invoke(GET_HANDLE_WORLD.invoke(block.getWorld()), SKY_BLOCK_ENUM, BLOCK_POS_CONSTRUCTOR.newInstance(x, y, z));
}
} catch (Throwable ex) {
ex.printStackTrace();
}
}



private static boolean canAffectLighting(final World world, final int x, final int y, final int z) {
private static boolean isSurrounded(final World world, final int x, final int y, final int z) {
final Block base = world.getBlockAt(x, y, z);
final Block east = base.getRelative(BlockFace.EAST);
final Block west = base.getRelative(BlockFace.WEST);
final Block south = base.getRelative(BlockFace.SOUTH);
final Block north = base.getRelative(BlockFace.NORTH);
final Block up = base.getRelative(BlockFace.UP);
final Block down = base.getRelative(BlockFace.DOWN);

return east.getType().isTransparent()
|| west.getType().isTransparent()
|| up.getType().isTransparent()
|| down.getType().isTransparent()
|| south.getType().isTransparent()
|| north.getType().isTransparent();
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}

private NMSUtil() {}
Expand Down
65 changes: 41 additions & 24 deletions src/main/java/me/realized/de/arenaregen/zone/ResetZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import lombok.Getter;
Expand All @@ -20,7 +21,6 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -42,7 +42,7 @@ public class ResetZone {
private final Map<Position, BlockInfo> blocks = new HashMap<>();
private final File file;

public ResetZone(final ArenaRegen extension, final Duels api, final String name, final File folder, final Location first, final Location second) {
ResetZone(final ArenaRegen extension, final Duels api, final String name, final File folder, final Location first, final Location second) {
this.api = api;
this.config = extension.getConfiguration();
this.name = name;
Expand All @@ -61,7 +61,7 @@ public ResetZone(final ArenaRegen extension, final Duels api, final String name,
);
}

public ResetZone(final ArenaRegen extension, final Duels api, final String name, final File file) {
ResetZone(final ArenaRegen extension, final Duels api, final String name, final File file) {
this.api = api;
this.config = extension.getConfiguration();

Expand Down Expand Up @@ -94,7 +94,7 @@ public ResetZone(final ArenaRegen extension, final Duels api, final String name,
});
}

public void save() throws IOException {
void save() throws IOException {
if (!file.exists()) {
file.createNewFile();
}
Expand All @@ -111,7 +111,7 @@ public void save() throws IOException {
config.save(file);
}

public void delete() {
void delete() {
blocks.clear();
file.delete();
}
Expand All @@ -123,7 +123,7 @@ public int getTotalBlocks() {
void loadBlocks() {
// Only store non-air blocks
doForAll(block -> {
if (block.getType().name().contains("AIR")) {
if (block.getType() == Material.AIR) {
return;
}

Expand All @@ -141,27 +141,29 @@ private void doForAll(final Consumer<Block> consumer) {
}
}

public void reset() {
final Set<Chunk> chunks = new HashSet<>();
@SuppressWarnings("deprecation")
void reset() {
final Set<ChunkLoc> chunks = new HashSet<>();

doForAll(block -> {
final BlockInfo info = blocks.get(new Position(block));

if (info == null || info.matches(block)) {
if (info == null) {
// If no stored information is available (= air) but block is not air, set to air
if (!block.getType().name().contains("AIR")) {
if (block.getType() != Material.AIR) {
NMSUtil.setBlockFast(block, Material.AIR, 0);
chunks.add(new ChunkLoc(block.getChunk()));
}

return;
} else if (info.matches(block)) {
return;
}

chunks.add(block.getChunk());
NMSUtil.setBlockFast(block, info.getType(), info.getData());
chunks.add(new ChunkLoc(block.getChunk()));
});
api.doSyncAfter(() -> {
doForAll(NMSUtil::updateLighting);
chunks.forEach(chunk -> chunk.getWorld().refreshChunk(chunk.getX(), chunk.getZ()));
}, 1L);
chunks.forEach(chunkLoc -> min.getWorld().refreshChunk(chunkLoc.x, chunkLoc.z));

if (!config.isRemoveDroppedItems()) {
return;
Expand All @@ -170,23 +172,38 @@ public void reset() {
min.getWorld().getEntitiesByClass(Item.class).stream().filter(item -> contains(item.getLocation())).forEach(Entity::remove);
}

public boolean contains(final Location location) {
private boolean contains(final Location location) {
return min.getWorld().equals(location.getWorld())
&& min.getBlockX() <= location.getBlockX() && location.getBlockX() <= max.getBlockX()
&& min.getBlockY() <= location.getBlockY() && location.getBlockY() <= max.getBlockY()
&& min.getBlockZ() <= location.getBlockZ() && location.getBlockZ() <= max.getBlockZ();
}

public boolean contains(final BlockState state) {
return contains(state.getLocation());
@SuppressWarnings("deprecation")
boolean isCached(final Block block) {
return contains(block.getLocation()) && blocks.containsKey(new Position(block));
}

public boolean contains(final Block block) {
return contains(block.getState());
}
private class ChunkLoc {

@SuppressWarnings("deprecation")
public boolean isCached(final Block block) {
return contains(block) && blocks.containsKey(new Position(block));
private final int x, z;

ChunkLoc(final Chunk chunk) {
this.x = chunk.getX();
this.z = chunk.getZ();
}

@Override
public boolean equals(final Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
final ChunkLoc chunkLoc = (ChunkLoc) o;
return x == chunkLoc.x && z == chunkLoc.z;
}

@Override
public int hashCode() {
return Objects.hash(x, z);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Selection get(final Player player) {
return selections.get(player.getUniqueId());
}

public ResetZone get(final String name) {
private ResetZone get(final String name) {
return zones.get(name);
}

Expand Down

0 comments on commit 1a96865

Please sign in to comment.