Fix for chunk loading.

This commit is contained in:
Brianna O'Keefe 2018-12-27 03:59:49 -05:00
parent 9f394c21ce
commit 424e406f7c
10 changed files with 80 additions and 96 deletions

View File

@ -1,5 +1,6 @@
package com.songoda.epichoppers.api.hopper; package com.songoda.epichoppers.api.hopper;
import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -18,7 +19,7 @@ public interface Filter {
void setVoidList(List<ItemStack> voidList); void setVoidList(List<ItemStack> voidList);
Block getEndPoint(); Location getEndPoint();
void setEndPoint(Block endPoint); void setEndPoint(Location endPoint);
} }

View File

@ -109,9 +109,9 @@ public interface Hopper {
*/ */
void setTeleportTrigger(TeleportTrigger teleportTrigger); void setTeleportTrigger(TeleportTrigger teleportTrigger);
List<Block> getLinkedBlocks(); List<Location> getLinkedBlocks();
void addLinkedBlock(Block block); void addLinkedBlock(Location block);
void clearLinkedBlocks(); void clearLinkedBlocks();

View File

@ -151,15 +151,15 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
if (storage.containsGroup("sync")) { if (storage.containsGroup("sync")) {
for (StorageRow row : storage.getRowsByGroup("sync")) { for (StorageRow row : storage.getRowsByGroup("sync")) {
Location location = Serialize.getInstance().unserializeLocation(row.getKey()); Location location = Serialize.getInstance().unserializeLocation(row.getKey());
if (location == null || location.getBlock() == null) return; if (location == null) return;
int level = row.get("level").asInt(); int level = row.get("level").asInt();
List<String> blockLoc = row.get("block").asStringList(); List<String> blockLoc = row.get("block").asStringList();
List<Block> blocks = new ArrayList<>(); List<Location> blocks = new ArrayList<>();
if (blockLoc != null) { if (blockLoc != null) {
for (String string : blockLoc) { for (String string : blockLoc) {
blocks.add(Arconix.pl().getApi().serialize().unserializeLocation(string).getBlock()); blocks.add(Arconix.pl().getApi().serialize().unserializeLocation(string));
} }
} }
@ -177,7 +177,7 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
Material autoCrafting = Material.valueOf(row.get("autocrafting").asString() == null ? "AIR" : row.get("autocrafting").asString()); Material autoCrafting = Material.valueOf(row.get("autocrafting").asString() == null ? "AIR" : row.get("autocrafting").asString());
String blackLoc = row.get("black").asString(); String blackLoc = row.get("black").asString();
Block black = blackLoc == null ? null : Arconix.pl().getApi().serialize().unserializeLocation(blackLoc).getBlock(); Location black = blackLoc == null ? null : Arconix.pl().getApi().serialize().unserializeLocation(blackLoc);
EFilter filter = new EFilter(); EFilter filter = new EFilter();
@ -319,7 +319,7 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
new StorageItem("whitelist", hopper.getFilter().getWhiteList()), new StorageItem("whitelist", hopper.getFilter().getWhiteList()),
new StorageItem("blacklist", hopper.getFilter().getBlackList()), new StorageItem("blacklist", hopper.getFilter().getBlackList()),
new StorageItem("void", hopper.getFilter().getVoidList()), new StorageItem("void", hopper.getFilter().getVoidList()),
new StorageItem("black", hopper.getFilter().getEndPoint() == null ? null : Arconix.pl().getApi().serialize().serializeLocation(hopper.getFilter().getEndPoint().getLocation()))); new StorageItem("black", hopper.getFilter().getEndPoint() == null ? null : Arconix.pl().getApi().serialize().serializeLocation(hopper.getFilter().getEndPoint())));
} }
/* /*

View File

@ -30,7 +30,6 @@ public class HopHandler {
try { try {
this.instance = instance; this.instance = instance;
Bukkit.getScheduler().scheduleSyncDelayedTask(instance, () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(instance, () -> {
hopperCleaner();
Bukkit.getScheduler().scheduleSyncRepeatingTask(instance, this::hopperRunner, 0, instance.getConfig().getLong("Main.Amount of Ticks Between Hops")); Bukkit.getScheduler().scheduleSyncRepeatingTask(instance, this::hopperRunner, 0, instance.getConfig().getLong("Main.Amount of Ticks Between Hops"));
}, 40L); }, 40L);
} catch (Exception e) { } catch (Exception e) {
@ -38,48 +37,27 @@ public class HopHandler {
} }
} }
private void hopperCleaner() {
try {
ConfigurationSection data = instance.getConfig().createSection("data");
if (!data.contains("sync")) return;
for (String key : data.getConfigurationSection("sync").getKeys(false)) {
if (Arconix.pl().getApi().serialize().unserializeLocation(key).getWorld() == null) continue;
Block block = Arconix.pl().getApi().serialize().unserializeLocation(key).getBlock();
if (block != null && block.getState() instanceof Hopper) continue;
data.getConfigurationSection("sync").set(key, null);
instance.getLogger().info("EpicHoppers Removing non-hopper entry: " + key);
}
} catch (Exception e) {
Debugger.runReport(e);
}
}
private void hopperRunner() { private void hopperRunner() {
try { try {
main: main:
for (com.songoda.epichoppers.api.hopper.Hopper hopper : new HashMap<>(instance.getHopperManager().getHoppers()).values()) { for (com.songoda.epichoppers.api.hopper.Hopper hopper : new HashMap<>(instance.getHopperManager().getHoppers()).values()) {
Location location = hopper.getLocation(); Location location = hopper.getLocation();
int x = location.getBlockX() >> 4; int x = location.getBlockX() >> 4;
int z = location.getBlockZ() >> 4; int z = location.getBlockZ() >> 4;
try { if (!location.getWorld().isChunkLoaded(x, z))
if (!location.getWorld().isChunkLoaded(x, z)) {
continue;
}
} catch (Exception e) {
continue; continue;
}
Block block = location.getBlock();
if (block.isBlockPowered() || block.isBlockIndirectlyPowered()) continue; Block block = location.getBlock();
if (block == null || block.getType() != Material.HOPPER) { if (block == null || block.getType() != Material.HOPPER) {
instance.getHopperManager().removeHopper(location); instance.getHopperManager().removeHopper(location);
continue; continue;
} }
if (block.isBlockPowered() || block.isBlockIndirectlyPowered()) continue;
Hopper hopperBlock = hopper.getHopper(); Hopper hopperBlock = hopper.getHopper();
List<Material> blockedMaterials = new ArrayList<>(); List<Material> blockedMaterials = new ArrayList<>();
@ -98,38 +76,33 @@ public class HopHandler {
if (hopper.getLinkedBlocks() == null || hopper.getLinkedBlocks().isEmpty()) continue; if (hopper.getLinkedBlocks() == null || hopper.getLinkedBlocks().isEmpty()) continue;
for (Block destBlock : hopper.getLinkedBlocks()) { for (Location destinationLocation : hopper.getLinkedBlocks()) {
Location dest = destBlock.getLocation(); if (destinationLocation == null) continue;
if (dest == null) continue;
int destx = location.getBlockX() >> 4; int destx = location.getBlockX() >> 4;
int destz = location.getBlockZ() >> 4; int destz = location.getBlockZ() >> 4;
if (!dest.getWorld().isChunkLoaded(destx, destz)) {
continue;
}
Block b2 = dest.getBlock(); if (!destinationLocation.getWorld().isChunkLoaded(destx, destz))
if (!(b2.getState() instanceof InventoryHolder)) { continue;
Block destinationBlock = destinationLocation.getBlock();
if (!(destinationBlock.getState() instanceof InventoryHolder)) {
hopper.clearLinkedBlocks(); hopper.clearLinkedBlocks();
continue; continue;
} }
//InventoryHolder inventoryHolder = (InventoryHolder) b2.getState();
//TODO add some restrictions here if needed
BoostData boostData = instance.getBoostManager().getBoost(hopper.getPlacedBy()); BoostData boostData = instance.getBoostManager().getBoost(hopper.getPlacedBy());
int amt = hopper.getLevel().getAmount() * (boostData == null ? 1 : boostData.getMultiplier()); int amount = hopper.getLevel().getAmount() * (boostData == null ? 1 : boostData.getMultiplier());
List<ItemStack> whiteList = hopper.getFilter().getWhiteList(); List<ItemStack> whiteList = hopper.getFilter().getWhiteList();
List<ItemStack> blackList = hopper.getFilter().getBlackList(); List<ItemStack> blackList = hopper.getFilter().getBlackList();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
ItemStack it; ItemStack item;
if (is[i] != null) { if (is[i] != null) {
it = is[i].clone(); item = is[i].clone();
it.setAmount(1); item.setAmount(1);
} }
if (hopper.getLocation().getBlock().isBlockPowered() if (hopper.getLocation().getBlock().isBlockPowered()
|| is[i] != null && blockedMaterials.contains(is[i].getType())) { || is[i] != null && blockedMaterials.contains(is[i].getType())) {
@ -137,25 +110,25 @@ public class HopHandler {
if (i >= 5) continue; if (i >= 5) continue;
} }
int finalI = i; int finalIncrement = i;
if (is[i] != null if (is[i] != null
&& !whiteList.isEmpty() && !whiteList.isEmpty()
&& whiteList.stream().noneMatch(itemStack -> itemStack.isSimilar(is[finalI]))) { && whiteList.stream().noneMatch(itemStack -> itemStack.isSimilar(is[finalIncrement]))) {
doBlacklist(hopperBlock, hopper, is[i].clone(), is, amt, i); doBlacklist(hopperBlock, hopper, is[i].clone(), is, amount, i);
continue main; continue main;
} else { }
if (is[i] != null && blackList.stream().noneMatch(itemStack -> itemStack.isSimilar(is[finalI]))) {
if (addItem(hopperBlock, hopper, b2, is[i], is, amt, i)) { if (is[i] != null && blackList.stream().noneMatch(itemStack -> itemStack.isSimilar(is[finalIncrement]))) {
block.getState().update(); if (addItem(hopperBlock, hopper, destinationBlock, is[i], is, amount, i)) {
continue main; //block.getState().update();
} continue main;
} else {
if (is[i] != null && blackList.stream().anyMatch(itemStack -> itemStack.isSimilar(is[finalI]))) {
doBlacklist(hopperBlock, hopper, is[i].clone(), is, amt, i);
continue main;
}
} }
} }
if (is[i] != null && blackList.stream().anyMatch(itemStack -> itemStack.isSimilar(is[finalIncrement]))) {
doBlacklist(hopperBlock, hopper, is[i].clone(), is, amount, i);
continue main;
}
} }
} }
} }
@ -167,16 +140,17 @@ public class HopHandler {
private void doBlacklist(Hopper hopperBlock, com.songoda.epichoppers.api.hopper.Hopper hopper, ItemStack item, ItemStack[] isS, int amt, int place) { private void doBlacklist(Hopper hopperBlock, com.songoda.epichoppers.api.hopper.Hopper hopper, ItemStack item, ItemStack[] isS, int amt, int place) {
try { try {
Location loc = hopperBlock.getLocation(); Location location = hopperBlock.getLocation();
Block block = loc.getBlock(); Block block = location.getBlock();
if (hopper.getFilter().getEndPoint() != null if (hopper.getFilter().getEndPoint() != null
&& block != null && block.getState() instanceof Hopper) { && block != null && block.getState() instanceof Hopper) {
Location dest = hopper.getFilter().getEndPoint().getLocation(); Location dest = hopper.getFilter().getEndPoint();
int destx = loc.getBlockX() >> 4; int destx = location.getBlockX() >> 4;
int destz = loc.getBlockZ() >> 4; int destz = location.getBlockZ() >> 4;
if (!dest.getWorld().isChunkLoaded(destx, destz)) {
if (!dest.getWorld().isChunkLoaded(destx, destz))
return; return;
}
Block b2 = dest.getBlock(); Block b2 = dest.getBlock();
if (!(b2.getState() instanceof InventoryHolder)) { if (!(b2.getState() instanceof InventoryHolder)) {
@ -185,7 +159,7 @@ public class HopHandler {
} }
addItem(hopperBlock, hopper, b2, item, isS, amt, place); addItem(hopperBlock, hopper, b2, item, isS, amt, place);
block.getState().update(); //block.getState().update();
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -72,7 +72,15 @@ public class TeleportHandler {
while (instance.getHopperManager().isHopper(next.getLocation()) && instance.getHopperManager().getHopper(next.getLocation()).getLinkedBlocks() != null && num != 15) { while (instance.getHopperManager().isHopper(next.getLocation()) && instance.getHopperManager().getHopper(next.getLocation()).getLinkedBlocks() != null && num != 15) {
Hopper nextHopper = instance.getHopperManager().getHopper(next); Hopper nextHopper = instance.getHopperManager().getHopper(next);
if (nextHopper.getLinkedBlocks() != null && !nextHopper.getLinkedBlocks().isEmpty()) { if (nextHopper.getLinkedBlocks() != null && !nextHopper.getLinkedBlocks().isEmpty()) {
next = nextHopper.getLinkedBlocks().get(0); Location location = nextHopper.getLinkedBlocks().get(0);
int x = location.getBlockX() >> 4;
int z = location.getBlockZ() >> 4;
if (!location.getWorld().isChunkLoaded(x, z))
continue;
next = location.getBlock();
} }
if (!next.getType().equals(Material.HOPPER)) { if (!next.getType().equals(Material.HOPPER)) {

View File

@ -1,6 +1,7 @@
package com.songoda.epichoppers.hopper; package com.songoda.epichoppers.hopper;
import com.songoda.epichoppers.api.hopper.Filter; import com.songoda.epichoppers.api.hopper.Filter;
import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -13,7 +14,7 @@ public class EFilter implements Filter {
private List<ItemStack> blackList = new ArrayList<>(); private List<ItemStack> blackList = new ArrayList<>();
private List<ItemStack> voidList = new ArrayList<>(); private List<ItemStack> voidList = new ArrayList<>();
private Block endPoint; private Location endPoint;
@Override @Override
public List<ItemStack> getWhiteList() { public List<ItemStack> getWhiteList() {
@ -49,12 +50,12 @@ public class EFilter implements Filter {
} }
@Override @Override
public Block getEndPoint() { public Location getEndPoint() {
return endPoint; return endPoint;
} }
@Override @Override
public void setEndPoint(Block endPoint) { public void setEndPoint(Location endPoint) {
this.endPoint = endPoint; this.endPoint = endPoint;
} }
} }

View File

@ -36,13 +36,13 @@ public class EHopper implements Hopper {
private Level level; private Level level;
private UUID lastPlayer; private UUID lastPlayer;
private UUID placedBy; private UUID placedBy;
private List<Block> linkedBlocks; private List<Location> linkedBlocks;
private Filter filter; private Filter filter;
private TeleportTrigger teleportTrigger; private TeleportTrigger teleportTrigger;
private Material autoCrafting; private Material autoCrafting;
private org.bukkit.block.Hopper hopper; private org.bukkit.block.Hopper hopper;
public EHopper(Location location, Level level, UUID lastPlayer, UUID placedBy, List<Block> linkedBlocks, Filter filter, TeleportTrigger teleportTrigger, Material autoCrafting) { public EHopper(Location location, Level level, UUID lastPlayer, UUID placedBy, List<Location> linkedBlocks, Filter filter, TeleportTrigger teleportTrigger, Material autoCrafting) {
this.location = location; this.location = location;
this.level = level; this.level = level;
this.linkedBlocks = linkedBlocks; this.linkedBlocks = linkedBlocks;
@ -51,11 +51,18 @@ public class EHopper implements Hopper {
this.placedBy = placedBy; this.placedBy = placedBy;
this.teleportTrigger = teleportTrigger; this.teleportTrigger = teleportTrigger;
this.autoCrafting = autoCrafting; this.autoCrafting = autoCrafting;
int x = location.getBlockX() >> 4;
int z = location.getBlockZ() >> 4;
if (!location.getWorld().isChunkLoaded(x, z))
return;
this.reloadHopper(); this.reloadHopper();
this.syncName(); this.syncName();
} }
public EHopper(Block block, Level level, UUID lastPlayer, UUID placedBy, List<Block> linkedBlocks, Filter filter, TeleportTrigger teleportTrigger, Material autoCrafting) { public EHopper(Block block, Level level, UUID lastPlayer, UUID placedBy, List<Location> linkedBlocks, Filter filter, TeleportTrigger teleportTrigger, Material autoCrafting) {
this(block.getLocation(), level, lastPlayer, placedBy, linkedBlocks, filter, teleportTrigger, autoCrafting); this(block.getLocation(), level, lastPlayer, placedBy, linkedBlocks, filter, teleportTrigger, autoCrafting);
} }
@ -556,9 +563,9 @@ public class EHopper implements Hopper {
} }
if (!filtered) if (!filtered)
this.linkedBlocks.add(toLink); this.linkedBlocks.add(toLink.getLocation());
else else
this.filter.setEndPoint(toLink); this.filter.setEndPoint(toLink.getLocation());
this.lastPlayer = player.getUniqueId(); this.lastPlayer = player.getUniqueId();
if (level.getLinkAmount() > 1) { if (level.getLinkAmount() > 1) {
@ -637,12 +644,12 @@ public class EHopper implements Hopper {
} }
@Override @Override
public List<Block> getLinkedBlocks() { public List<Location> getLinkedBlocks() {
return Collections.unmodifiableList(linkedBlocks); return Collections.unmodifiableList(linkedBlocks);
} }
@Override @Override
public void addLinkedBlock(Block block) { public void addLinkedBlock(Location block) {
linkedBlocks.add(block); linkedBlocks.add(block);
} }

View File

@ -42,10 +42,6 @@ public class BlockListeners implements Listener {
public void onBlockPlace(BlockPlaceEvent e) { public void onBlockPlace(BlockPlaceEvent e) {
try { try {
Player player = e.getPlayer(); Player player = e.getPlayer();
if (e.getBlock().getType().equals(Material.ENDER_CHEST)) {
//instance.getDataFile().getConfig().set("data.enderTracker." + Arconix.pl().getApi().serialize().serializeLocation(e.getBlock()), player.getUniqueId().toString());
return;
}
if (e.getBlock().getType() != Material.HOPPER) return; if (e.getBlock().getType() != Material.HOPPER) return;
@ -103,10 +99,6 @@ public class BlockListeners implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) { public void onBlockBreak(BlockBreakEvent event) {
try { try {
if (event.getBlock().getType().equals(Material.ENDER_CHEST)) {
//instance.getDataFile().getConfig().set("data.enderTracker." + Arconix.pl().getApi().serialize().serializeLocation(event.getBlock()), null);
}
Block block = event.getBlock(); Block block = event.getBlock();
Player player = event.getPlayer(); Player player = event.getPlayer();

View File

@ -34,7 +34,7 @@ public class HopperListeners implements Listener {
Hopper hopper = instance.getHopperManager().getHopper(source.getLocation()); Hopper hopper = instance.getHopperManager().getHopper(source.getLocation());
if (hopper.getLinkedBlocks() == null || hopper.getLinkedBlocks().isEmpty()) { if (hopper.getLinkedBlocks() == null || hopper.getLinkedBlocks().isEmpty()) {
hopper.clearLinkedBlocks(); hopper.clearLinkedBlocks();
hopper.addLinkedBlock(event.getDestination().getLocation().getBlock()); hopper.addLinkedBlock(event.getDestination().getLocation());
} }
event.setCancelled(true); event.setCancelled(true);
} catch (Exception ee) { } catch (Exception ee) {

View File

@ -2,6 +2,7 @@ package com.songoda.epichoppers.storage;
import com.songoda.arconix.plugin.Arconix; import com.songoda.arconix.plugin.Arconix;
import com.songoda.epichoppers.utils.Serializers; import com.songoda.epichoppers.utils.Serializers;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -33,10 +34,10 @@ public class StorageItem {
this.object = object.toString(); this.object = object.toString();
} }
public StorageItem(String key, boolean type, List<Block> blocks) { public StorageItem(String key, boolean type, List<Location> blocks) {
StringBuilder object = new StringBuilder(); StringBuilder object = new StringBuilder();
for (Block block : blocks) { for (Location location : blocks) {
object.append(Arconix.pl().getApi().serialize().serializeLocation(block)); object.append(Arconix.pl().getApi().serialize().serializeLocation(location));
object.append(";;"); object.append(";;");
} }
this.key = key; this.key = key;