Added HopperBuilder.

This commit is contained in:
Brianna 2019-07-10 23:45:23 -04:00
parent 4349299321
commit 549a433934
8 changed files with 165 additions and 70 deletions

View File

@ -10,6 +10,7 @@ import com.songoda.epichoppers.enchantment.Enchantment;
import com.songoda.epichoppers.handlers.TeleportHandler;
import com.songoda.epichoppers.hopper.Filter;
import com.songoda.epichoppers.hopper.Hopper;
import com.songoda.epichoppers.hopper.HopperBuilder;
import com.songoda.epichoppers.hopper.HopperManager;
import com.songoda.epichoppers.hopper.levels.Level;
import com.songoda.epichoppers.hopper.levels.LevelManager;
@ -249,6 +250,12 @@ public class EpicHoppers extends JavaPlugin {
if (location == null) return;
int levelVal = row.get("level").asInt();
Level level = levelManager.isLevel(levelVal) ? levelManager.getLevel(levelVal) : levelManager.getLowestLevel();
String playerStr = row.get("player").asString();
String placedByStr = row.get("placedby").asString();
UUID lastPlayer = playerStr == null ? null : UUID.fromString(row.get("player").asString());
UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr);
List<String> blockLoc = row.get("block").asStringList();
List<Location> blocks = new ArrayList<>();
@ -258,47 +265,41 @@ public class EpicHoppers extends JavaPlugin {
}
}
TeleportTrigger teleportTrigger = TeleportTrigger.valueOf(row.get("teleporttrigger").asString() == null ? "DISABLED" : row.get("teleporttrigger").asString());
String playerStr = row.get("player").asString();
String placedByStr = row.get("placedby").asString();
UUID lastPlayer = playerStr == null ? null : UUID.fromString(playerStr);
UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr);
Filter filter = new Filter();
List<ItemStack> whiteList = row.get("whitelist").asItemStackList();
List<ItemStack> blackList = row.get("blacklist").asItemStackList();
List<ItemStack> voidList = row.get("void").asItemStackList();
String autoCraftingStr = row.get("autocrafting").asString() == null ? "AIR" : row.get("autocrafting").asString();
boolean autoSell = row.get("autosell").asBoolean();
String[] autoCraftingParts = autoCraftingStr.split(":");
ItemStack autoCrafting = new ItemStack(Material.valueOf(autoCraftingParts[0]), 1, Short.parseShort(autoCraftingParts.length == 2 ? autoCraftingParts[1] : "0"));
String blackLoc = row.get("black").asString();
Location black = blackLoc == null ? null : Methods.unserializeLocation(blackLoc);
boolean autoBreak = row.get("autobreak").asBoolean();
Filter filter = new Filter();
filter.setWhiteList(whiteList);
filter.setBlackList(blackList);
filter.setVoidList(voidList);
filter.setEndPoint(black);
Level level = levelManager.isLevel(levelVal) ? levelManager.getLevel(levelVal) : levelManager.getLowestLevel();
TeleportTrigger teleportTrigger = TeleportTrigger.valueOf(row.get("teleporttrigger").asString() == null ? "DISABLED" : row.get("teleporttrigger").asString());
Hopper hopper = new Hopper(location, level, lastPlayer, placedBy, blocks, filter, teleportTrigger, autoCrafting);
String autoCraftingStr = row.get("autocrafting").asString() == null ? "AIR" : row.get("autocrafting").asString();
String[] autoCraftingParts = autoCraftingStr.split(":");
ItemStack autoCrafting = new ItemStack(Material.valueOf(autoCraftingParts[0]), 1, Short.parseShort(autoCraftingParts.length == 2 ? autoCraftingParts[1] : "0"));
if (!autoSell)
hopper.setAutoSellTimer(-9999);
boolean autoSell = row.get("autosell").asBoolean();
if (autoBreak) hopper.toggleAutoBreaking();
boolean autoBreak = row.get("autobreak").asBoolean();
hopperManager.addHopper(location, hopper);
hopperManager.addHopper(new HopperBuilder(location)
.setLevel(level)
.setLastPlayerOpened(lastPlayer)
.setPlacedBy(placedBy)
.addLinkedBlocks(blocks.toArray(new Location[0]))
.setFilter(filter)
.setTeleportTrigger(teleportTrigger)
.setAutoCrafting(autoCrafting)
.setAutoSelling(autoSell)
.setAutoBreaking(autoBreak)
.build());
}
}

View File

@ -175,7 +175,7 @@ public class GUIFilter extends AbstractGUI {
ItemStack item = items[i];
if (item.getAmount() != 1) {
item.setAmount(item.getAmount() - 1);
Bukkit.getPlayer(hopper.getLastPlayer()).getInventory().addItem(item);
Bukkit.getPlayer(hopper.getLastPlayerOpened()).getInventory().addItem(item);
item.setAmount(1);
}
owhite.add(item);
@ -187,7 +187,7 @@ public class GUIFilter extends AbstractGUI {
ItemStack item = items[i];
if (item.getAmount() != 1) {
item.setAmount(item.getAmount() - 1);
Bukkit.getPlayer(hopper.getLastPlayer()).getInventory().addItem(item);
Bukkit.getPlayer(hopper.getLastPlayerOpened()).getInventory().addItem(item);
item.setAmount(1);
}
oblack.add(item);
@ -199,7 +199,7 @@ public class GUIFilter extends AbstractGUI {
ItemStack item = items[i];
if (item.getAmount() != 1) {
item.setAmount(item.getAmount() - 1);
Bukkit.getPlayer(hopper.getLastPlayer()).getInventory().addItem(item);
Bukkit.getPlayer(hopper.getLastPlayerOpened()).getInventory().addItem(item);
item.setAmount(1);
}
ovoid.add(item);

View File

@ -225,7 +225,7 @@ public class GUIOverview extends AbstractGUI {
inventory.setItem(25, Methods.getBackgroundGlass(true));
inventory.setItem(26, Methods.getBackgroundGlass(true));
hopper.setLastPlayer(player.getUniqueId());
hopper.setLastPlayerOpened(player.getUniqueId());
}
private void runTask() {
@ -269,7 +269,7 @@ public class GUIOverview extends AbstractGUI {
player.sendMessage(plugin.references.getPrefix() + plugin.getLocale().getMessage("event.hopper.desync"));
hopper.clearLinkedBlocks();
} else {
if (hopper.getLastPlayer() != null && !hopper.getLastPlayer().equals(player.getUniqueId())) {
if (hopper.getLastPlayerOpened() != null && !hopper.getLastPlayerOpened().equals(player.getUniqueId())) {
player.sendMessage(plugin.references.getPrefix() + plugin.getLocale().getMessage("event.hopper.syncdidnotplace"));
return;
}

View File

@ -27,40 +27,26 @@ import java.util.UUID;
public class Hopper {
private Location location;
private Level level;
private UUID lastPlayer;
private UUID placedBy;
private List<Location> linkedBlocks;
private Filter filter;
private TeleportTrigger teleportTrigger;
private ItemStack autoCrafting;
private int autoSellTimer;
private boolean autoBreaking;
private int transferTick;
private Level level = EpicHoppers.getInstance().getLevelManager().getLowestLevel();
private UUID lastPlayerOpened = null;
private UUID placedBy = null;
private List<Location> linkedBlocks = new ArrayList<>();
private Filter filter = new Filter();
private TeleportTrigger teleportTrigger = TeleportTrigger.DISABLED;
private ItemStack autoCrafting = null;
private int autoSellTimer = -9999;
private boolean autoBreaking = false;
private int transferTick = 0;
public Hopper(Location location, Level level, UUID lastPlayer, UUID placedBy, List<Location> linkedBlocks, Filter filter, TeleportTrigger teleportTrigger, ItemStack autoCrafting) {
public Hopper(Location location) {
this.location = location;
this.level = level;
this.linkedBlocks = linkedBlocks;
this.filter = filter;
this.lastPlayer = lastPlayer;
this.placedBy = placedBy;
this.teleportTrigger = teleportTrigger;
this.autoCrafting = autoCrafting;
this.autoSellTimer = 0;
this.autoBreaking = false;
this.transferTick = 0;
}
public Hopper(Block block, Level level, UUID lastPlayer, UUID placedBy, List<Location> linkedBlocks, Filter filter, TeleportTrigger teleportTrigger, ItemStack autoCrafting) {
this(block.getLocation(), level, lastPlayer, placedBy, linkedBlocks, filter, teleportTrigger, autoCrafting);
}
public void overview(Player player) {
if (lastPlayer != null
&& lastPlayer != player.getUniqueId()
&& Bukkit.getPlayer(lastPlayer) != null) {
Bukkit.getPlayer(lastPlayer).closeInventory();
if (lastPlayerOpened != null
&& lastPlayerOpened != player.getUniqueId()
&& Bukkit.getPlayer(lastPlayerOpened) != null) {
Bukkit.getPlayer(lastPlayerOpened).closeInventory();
}
if (placedBy == null) placedBy = player.getUniqueId();
@ -170,7 +156,7 @@ public class Hopper {
instance.getPlayerDataManager().getPlayerData(player).setSyncType(null);
return;
}
this.lastPlayer = player.getUniqueId();
this.lastPlayerOpened = player.getUniqueId();
if (level.getLinkAmount() > 1) {
if (getLinkedBlocks().size() == level.getLinkAmount()) {
@ -228,22 +214,34 @@ public class Hopper {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public UUID getPlacedBy() {
return placedBy;
}
public UUID getLastPlayer() {
return lastPlayer;
public void setPlacedBy(UUID placedBy) {
this.placedBy = placedBy;
}
public void setLastPlayer(UUID uuid) {
lastPlayer = uuid;
public UUID getLastPlayerOpened() {
return lastPlayerOpened;
}
public void setLastPlayerOpened(UUID uuid) {
lastPlayerOpened = uuid;
}
public ItemStack getAutoCrafting() {
return autoCrafting;
}
public void setAutoCrafting(ItemStack autoCrafting) {
this.autoCrafting = autoCrafting;
}
public void setAutoCrafting(Player player, ItemStack autoCrafting) {
this.autoCrafting = autoCrafting;
if (autoCrafting != null) {
@ -278,6 +276,10 @@ public class Hopper {
return autoBreaking;
}
public void setAutoBreaking(boolean autoBreaking) {
this.autoBreaking = autoBreaking;
}
public void toggleAutoBreaking() {
this.autoBreaking = !autoBreaking;
}
@ -302,4 +304,7 @@ public class Hopper {
return filter;
}
public void setFilter(Filter filter) {
this.filter = filter;
}
}

View File

@ -0,0 +1,82 @@
package com.songoda.epichoppers.hopper;
import com.songoda.epichoppers.hopper.levels.Level;
import com.songoda.epichoppers.utils.TeleportTrigger;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class HopperBuilder {
private final Hopper hopper;
public HopperBuilder(Location location) {
this.hopper = new Hopper(location);
}
public HopperBuilder(Block block) {
this(block.getLocation());
}
public HopperBuilder setLevel(Level level) {
this.hopper.setLevel(level);
return this;
}
public HopperBuilder addLinkedBlocks(Location... linkedBlocks) {
for (Location location : linkedBlocks)
hopper.addLinkedBlock(location);
return this;
}
public HopperBuilder setFilter(Filter filter) {
this.hopper.setFilter(filter);
return this;
}
public HopperBuilder setLastPlayerOpened(UUID uuid) {
this.hopper.setLastPlayerOpened(uuid);
return this;
}
public HopperBuilder setLastPlayerOpened(OfflinePlayer player) {
return setLastPlayerOpened(player.getUniqueId());
}
public HopperBuilder setPlacedBy(UUID uuid) {
this.hopper.setPlacedBy(uuid);
return this;
}
public HopperBuilder setPlacedBy(OfflinePlayer player) {
this.hopper.setPlacedBy(player.getUniqueId());
return this;
}
public HopperBuilder setTeleportTrigger(TeleportTrigger teleportTrigger) {
this.hopper.setTeleportTrigger(teleportTrigger);
return this;
}
public HopperBuilder setAutoCrafting(ItemStack autoCrafting) {
this.hopper.setAutoCrafting(autoCrafting);
return this;
}
public HopperBuilder setAutoSelling(boolean autoSelling) {
this.hopper.setAutoSellTimer(autoSelling ? 0 : -9999);
return this;
}
public HopperBuilder setAutoBreaking(boolean autoBreaking) {
this.hopper.setAutoBreaking(autoBreaking);
return this;
}
public Hopper build() {
return this.hopper;
}
}

View File

@ -1,12 +1,9 @@
package com.songoda.epichoppers.hopper;
import com.songoda.epichoppers.EpicHoppers;
import com.songoda.epichoppers.utils.TeleportTrigger;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -15,13 +12,18 @@ public class HopperManager {
private final Map<Location, Hopper> registeredHoppers = new HashMap<>();
public void addHopper(Hopper hopper) {
registeredHoppers.put(roundLocation(hopper.getLocation()), hopper);
}
@Deprecated
public void addHopper(Location location, Hopper hopper) {
registeredHoppers.put(roundLocation(location), hopper);
}
/**
* Removes a hopper and unlinks it from any other hoppers
*
* @param location The location of the hopper to remove
* @return The removed hopper, or null if none was removed
*/
@ -37,7 +39,7 @@ public class HopperManager {
public Hopper getHopper(Location location) {
if (!registeredHoppers.containsKey(roundLocation(location))) {
addHopper(location, new Hopper(location, EpicHoppers.getInstance().getLevelManager().getLowestLevel(), null, null, new ArrayList<>(), new Filter(), TeleportTrigger.DISABLED, null));
addHopper(new Hopper(location));
}
return registeredHoppers.get(roundLocation(location));
}
@ -60,7 +62,7 @@ public class HopperManager {
public Hopper getHopperFromPlayer(Player player) {
for (Hopper hopper : registeredHoppers.values()) {
if (hopper.getLastPlayer() == player.getUniqueId()) {
if (hopper.getLastPlayerOpened() == player.getUniqueId()) {
return hopper;
}
}

View File

@ -3,6 +3,7 @@ package com.songoda.epichoppers.listeners;
import com.songoda.epichoppers.EpicHoppers;
import com.songoda.epichoppers.hopper.Filter;
import com.songoda.epichoppers.hopper.Hopper;
import com.songoda.epichoppers.hopper.HopperBuilder;
import com.songoda.epichoppers.hopper.levels.Level;
import com.songoda.epichoppers.utils.Methods;
import com.songoda.epichoppers.utils.ServerVersion;
@ -59,7 +60,11 @@ public class BlockListeners implements Listener {
ItemStack item = e.getItemInHand().clone();
instance.getHopperManager().addHopper(e.getBlock().getLocation(), new Hopper(e.getBlock(), instance.getLevelManager().getLevel(item), player.getUniqueId(), player.getUniqueId(), new ArrayList<>(), new Filter(), TeleportTrigger.DISABLED, null));
instance.getHopperManager().addHopper(e.getBlock().getLocation(),
new HopperBuilder(e.getBlock())
.setLevel(instance.getLevelManager().getLevel(item))
.setPlacedBy(player)
.setLastPlayerOpened(player).build());
}
private int maxHoppers(Player player) {

View File

@ -43,7 +43,7 @@ public abstract class Storage {
new StorageItem("level", hopper.getLevel().getLevel()),
new StorageItem("block", true, hopper.getLinkedBlocks() == null || hopper.getLinkedBlocks().isEmpty() ? new ArrayList<>() : hopper.getLinkedBlocks()),
new StorageItem("placedby", hopper.getPlacedBy() == null ? null : hopper.getPlacedBy().toString()),
new StorageItem("player", hopper.getLastPlayer() == null ? null : hopper.getLastPlayer().toString()),
new StorageItem("player", hopper.getLastPlayerOpened() == null ? null : hopper.getLastPlayerOpened().toString()),
new StorageItem("teleporttrigger", hopper.getTeleportTrigger().toString()),
new StorageItem("autocrafting", hopper.getAutoCrafting() == null || hopper.getAutoCrafting().getType() == Material.AIR ? null : hopper.getAutoCrafting().getType().name() + (hopper.getAutoCrafting().getDurability() == 0 ? "" : ":" + hopper.getAutoCrafting().getDurability())), new StorageItem("whitelist", hopper.getFilter().getWhiteList()),