Skip to content

Commit

Permalink
Fix null owner
Browse files Browse the repository at this point in the history
  • Loading branch information
ceze88 committed Sep 22, 2023
1 parent 6413d74 commit d473764
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand All @@ -23,7 +24,7 @@ public interface Hopper extends Data {

@Nullable UUID getLastPlayerOpened();

@Nullable UUID getPlacedBy();
@NotNull UUID getPlacedBy();

void setPlacedBy(UUID placedBy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand All @@ -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;
Expand All @@ -74,7 +75,8 @@ public HopperImpl() {
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");
}
Expand Down Expand Up @@ -340,7 +342,7 @@ public void setLevel(Level level) {
this.level = level;
}

public UUID getPlacedBy() {
public @NotNull UUID getPlacedBy() {
return this.placedBy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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<>();
Expand Down Expand Up @@ -75,22 +76,26 @@ public HopperImpl removeHopper(Location location) {
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;
}
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ private void teleportRunner() {
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;
Expand Down Expand Up @@ -98,7 +102,7 @@ private Hopper getChain(Hopper lastHopper, int currentChainLength) {

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ public void onBlockPlace(BlockPlaceEvent e) {
}

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);
Expand Down Expand Up @@ -130,7 +129,7 @@ public void onBlockBreak(BlockBreakEvent event) {
return;
}

HopperImpl hopper = this.plugin.getHopperManager().getHopper(block);
HopperImpl hopper = this.plugin.getHopperManager().getHopper(block, player.getUniqueId());

GUIFilter.compileOpenGuiFilter(hopper);
GUIAutoSellFilter.compileOpenAutoSellFilter(hopper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void onHop(InventoryMoveItemEvent event) {
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public void onPlayerToggleSneakEvent(PlayerToggleSneakEvent event) {
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);
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public void onBlockInteract(PlayerInteractEvent event) {
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));
Expand Down

0 comments on commit d473764

Please sign in to comment.