Fix null owner

This commit is contained in:
ceze88 2023-09-22 12:49:04 +02:00
parent 6413d74253
commit d4737640a7
8 changed files with 32 additions and 21 deletions

View File

@ -6,6 +6,7 @@ import com.craftaro.epichoppers.hopper.teleport.TeleportTrigger;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@ -23,7 +24,7 @@ public interface Hopper extends Data {
@Nullable UUID getLastPlayerOpened();
@Nullable UUID getPlacedBy();
@NotNull UUID getPlacedBy();
void setPlacedBy(UUID placedBy);

View File

@ -11,12 +11,12 @@ import java.util.UUID;
public class HopperBuilder {
private final HopperImpl hopper;
public HopperBuilder(Location location) {
this.hopper = new HopperImpl(location);
public HopperBuilder(Location location, UUID owner) {
this.hopper = new HopperImpl(location, owner);
}
public HopperBuilder(Block block) {
this(block.getLocation());
public HopperBuilder(Block block, UUID owner) {
this(block.getLocation(), owner);
}
public HopperBuilder setId(int id) {

View File

@ -33,6 +33,7 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import java.io.ByteArrayInputStream;
@ -55,7 +56,7 @@ public class HopperImpl implements Hopper {
private final Location location;
private Level level = getLevelManager().getLowestLevel();
private UUID lastPlayerOpened = null;
private UUID placedBy = null;
private UUID placedBy;
private final List<Location> linkedBlocks = new ArrayList<>();
private Filter filter = new Filter();
private TeleportTrigger teleportTrigger = TeleportTrigger.DISABLED;
@ -74,7 +75,8 @@ public class HopperImpl implements Hopper {
this.location = null;
}
public HopperImpl(Location location) {
public HopperImpl(Location location, UUID placedBy) {
this.placedBy = placedBy;
this.location = location;
this.id = EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getNextId("placed_hoppers");
}
@ -340,7 +342,7 @@ public class HopperImpl implements Hopper {
this.level = level;
}
public UUID getPlacedBy() {
public @NotNull UUID getPlacedBy() {
return this.placedBy;
}

View File

@ -11,6 +11,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class HopperManager {
private final Map<Location, HopperImpl> registeredHoppers = new HashMap<>();
@ -75,13 +76,17 @@ public class HopperManager {
return removed;
}
public HopperImpl getHopper(Location location) {
public HopperImpl getHopper(Location location, UUID createForIfNotExists) {
if (!this.registeredHoppers.containsKey(location = roundLocation(location))) {
if (!this.ready) {
throw new IllegalStateException("Hoppers are still being loaded");
}
HopperImpl hopper = addHopper(new HopperImpl(location));
if (createForIfNotExists == null) {
return null;
}
HopperImpl hopper = addHopper(new HopperImpl(location, createForIfNotExists));
this.plugin.getDataManager().save(hopper);
this.registeredHoppers.put(location, hopper);
return hopper;
@ -89,8 +94,8 @@ public class HopperManager {
return this.registeredHoppers.get(location);
}
public HopperImpl getHopper(Block block) {
return getHopper(block.getLocation());
public HopperImpl getHopper(Block block, UUID createForIfNotExists) {
return getHopper(block.getLocation(), createForIfNotExists);
}
/**

View File

@ -65,7 +65,11 @@ public class TeleportHandlerImpl implements TeleportHandler {
continue;
}
HopperImpl hopper = this.plugin.getHopperManager().getHopper(location);
HopperImpl hopper = this.plugin.getHopperManager().getHopper(location, null);
if (hopper == null) {
continue;
}
if (hopper.getTeleportTrigger() != TeleportTrigger.WALK_ON) {
continue;
@ -98,7 +102,7 @@ public class TeleportHandlerImpl implements TeleportHandler {
for (Location nextHopperLocation : lastHopper.getLinkedBlocks()) {
if (nextHopperLocation.getBlock().getState() instanceof org.bukkit.block.Hopper) {
Hopper hopper = this.plugin.getHopperManager().getHopper(nextHopperLocation);
Hopper hopper = this.plugin.getHopperManager().getHopper(nextHopperLocation, null);
if (hopper != null) {
return this.getChain(hopper, currentChainLength + 1);
}

View File

@ -69,9 +69,8 @@ public class BlockListeners implements Listener {
}
HopperImpl hopper = this.plugin.getHopperManager().addHopper(
new HopperBuilder(e.getBlock())
new HopperBuilder(e.getBlock(), player.getUniqueId())
.setLevel(this.plugin.getLevelManager().getLevel(item))
.setPlacedBy(player)
.setLastPlayerOpened(player).build());
HopperPlaceEvent hopperPlaceEvent = new HopperPlaceEvent(player, hopper);
@ -130,7 +129,7 @@ public class BlockListeners implements Listener {
return;
}
HopperImpl hopper = this.plugin.getHopperManager().getHopper(block);
HopperImpl hopper = this.plugin.getHopperManager().getHopper(block, player.getUniqueId());
GUIFilter.compileOpenGuiFilter(hopper);
GUIAutoSellFilter.compileOpenAutoSellFilter(hopper);

View File

@ -73,7 +73,7 @@ public class HopperListeners implements Listener {
return;
}
HopperImpl toHopper = this.plugin.getHopperManager().getHopper(destinationLocation);
HopperImpl toHopper = this.plugin.getHopperManager().getHopper(destinationLocation, null);
// minecraft 1.8 doesn't have a method to get the hopper's location from the inventory, so we use the holder instead
final ItemStack toMove = event.getItem();

View File

@ -38,12 +38,12 @@ public class InteractListeners implements Listener {
Location location = player.getLocation().getBlock().getRelative(BlockFace.SELF).getLocation();
Location down = location.getBlock().getRelative(BlockFace.DOWN).getLocation();
if (this.plugin.getHopperManager().isHopper(down)) {
HopperImpl hopper = this.plugin.getHopperManager().getHopper(down);
HopperImpl hopper = this.plugin.getHopperManager().getHopper(down, player.getUniqueId());
if (hopper.getTeleportTrigger() == TeleportTrigger.SNEAK) {
this.plugin.getTeleportHandler().tpEntity(player, hopper);
}
} else if (this.plugin.getHopperManager().isHopper(location)) {
HopperImpl hopper = this.plugin.getHopperManager().getHopper(location);
HopperImpl hopper = this.plugin.getHopperManager().getHopper(location, player.getUniqueId());
if (hopper.getTeleportTrigger() == TeleportTrigger.SNEAK) {
this.plugin.getTeleportHandler().tpEntity(player, hopper);
}
@ -90,7 +90,7 @@ public class InteractListeners implements Listener {
return;
}
HopperImpl hopper = this.plugin.getHopperManager().getHopper(event.getClickedBlock());
HopperImpl hopper = this.plugin.getHopperManager().getHopper(event.getClickedBlock(), player.getUniqueId());
if (!player.getInventory().getItemInHand().getType().name().contains("PICKAXE")) {
if (hopper.prepareForOpeningOverviewGui(player)) {
this.plugin.getGuiManager().showGUI(player, new GUIOverview(this.plugin, hopper, player));