forked from SteamWar/SteamWar
Add SWPlayer for per player storage and without Memory Leaks
This commit is contained in:
+39
-60
@@ -21,9 +21,10 @@ package de.steamwar.bausystem.features.countingwand;
|
||||
|
||||
import de.steamwar.bausystem.BauSystem;
|
||||
import de.steamwar.bausystem.region.Point;
|
||||
import de.steamwar.bausystem.shared.Pair;
|
||||
import de.steamwar.bausystem.utils.ItemUtils;
|
||||
import de.steamwar.core.SWPlayer;
|
||||
import de.steamwar.inventory.SWItem;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -31,13 +32,32 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@UtilityClass
|
||||
public class Countingwand {
|
||||
|
||||
@Data
|
||||
public class CountingWandComponent implements SWPlayer.Component {
|
||||
private Point pos1;
|
||||
private Point pos2;
|
||||
|
||||
public boolean set(boolean pos1, Point point) {
|
||||
if (this.pos1 == null || this.pos2 == null) {
|
||||
this.pos1 = point;
|
||||
this.pos2 = point;
|
||||
return true;
|
||||
}
|
||||
Point current = pos1 ? this.pos1 : this.pos2;
|
||||
if (current.equals(point)) return false;
|
||||
if (pos1) {
|
||||
this.pos1 = point;
|
||||
} else {
|
||||
this.pos2 = point;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemStack getWandItem(Player player) {
|
||||
ItemStack itemStack = new SWItem(Material.STICK, BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_NAME", player), Arrays.asList(BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_LORE1", player), BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_LORE2", player)), false, null).getItemStack();
|
||||
ItemUtils.setItem(itemStack, "countingwand");
|
||||
@@ -47,61 +67,20 @@ public class Countingwand {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
private final Map<String, Pair<Point, Point>> selections = new HashMap<>();
|
||||
public boolean isCountingwand(ItemStack itemStack) {
|
||||
return ItemUtils.isItem(itemStack, "countingwand");
|
||||
}
|
||||
|
||||
public boolean isCountingwand(ItemStack itemStack) {
|
||||
return ItemUtils.isItem(itemStack, "countingwand");
|
||||
}
|
||||
|
||||
public void checkSelection(final Point point, final boolean pos1, final Player p) {
|
||||
Pair<Point, Point> selection = selections.get(p.getUniqueId().toString());
|
||||
final boolean newPos;
|
||||
if (selection != null) {
|
||||
if (pos1) {
|
||||
newPos = !point.equals(selection.setKey(point));
|
||||
} else {
|
||||
newPos = !point.equals(selection.setValue(point));
|
||||
}
|
||||
} else {
|
||||
if (pos1) {
|
||||
selection = new Pair<>(point, null);
|
||||
} else {
|
||||
selection = new Pair<>(null, point);
|
||||
}
|
||||
selections.put(p.getUniqueId().toString(), selection);
|
||||
newPos = true;
|
||||
}
|
||||
|
||||
if (newPos) {
|
||||
String dimension = getDimensions(p, selection.getKey(), selection.getValue());
|
||||
String volume = getVolume(p, selection.getKey(), selection.getValue());
|
||||
if (pos1) {
|
||||
BauSystem.MESSAGE.send("COUNTINGWAND_MESSAGE_RCLICK", p, point.getX(), point.getY(), point.getZ(), dimension, volume);
|
||||
} else {
|
||||
BauSystem.MESSAGE.send("COUNTINGWAND_MESSAGE_LCLICK", p, point.getX(), point.getY(), point.getZ(), dimension, volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void removePlayer(Player p) {
|
||||
selections.remove(p.getUniqueId().toString());
|
||||
}
|
||||
|
||||
public String getVolume(Player player, final Point point1, final Point point2) {
|
||||
if (point1 == null || point2 == null) {
|
||||
return BauSystem.MESSAGE.parse("COUNTINGWAND_MESSAGE_VOLUME", player, 0);
|
||||
}
|
||||
return BauSystem.MESSAGE.parse("COUNTINGWAND_MESSAGE_VOLUME", player, getAmount(point1, point2));
|
||||
}
|
||||
|
||||
public String getDimensions(Player player, final Point point1, final Point point2) {
|
||||
if (point1 == null || point2 == null) {
|
||||
return BauSystem.MESSAGE.parse("COUNTINGWAND_MESSAGE_DIMENSION", player, 0, 0, 0);
|
||||
}
|
||||
return BauSystem.MESSAGE.parse("COUNTINGWAND_MESSAGE_DIMENSION", player, Math.abs(point1.getX() - point2.getX()) + 1, Math.abs(point1.getY() - point2.getY()) + 1, Math.abs(point1.getZ() - point2.getZ()) + 1);
|
||||
}
|
||||
|
||||
public int getAmount(final Point point1, final Point point2) {
|
||||
return (Math.abs(point1.getX() - point2.getX()) + 1) * (Math.abs(point1.getY() - point2.getY()) + 1) * (Math.abs(point1.getZ() - point2.getZ()) + 1);
|
||||
}
|
||||
public void checkSelection(final Point point, final boolean pos1, final Player p) {
|
||||
SWPlayer player = SWPlayer.of(p);
|
||||
CountingWandComponent countingWandComponent = player.getComponentOrDefault(CountingWandComponent.class, CountingWandComponent::new);
|
||||
if (countingWandComponent.set(pos1, point)) {
|
||||
Point point1 = countingWandComponent.pos1;
|
||||
Point point2 = countingWandComponent.pos2;
|
||||
int amount = (Math.abs(point1.getX() - point2.getX()) + 1) * (Math.abs(point1.getY() - point2.getY()) + 1) * (Math.abs(point1.getZ() - point2.getZ()) + 1);
|
||||
String dimension = player.using(BauSystem.MESSAGE).parse("COUNTINGWAND_MESSAGE_VOLUME", amount);
|
||||
String volume = player.parse("COUNTINGWAND_MESSAGE_DIMENSION", Math.abs(point1.getX() - point2.getX()) + 1, Math.abs(point1.getY() - point2.getY()) + 1, Math.abs(point1.getZ() - point2.getZ()) + 1);
|
||||
player.sendMessage(pos1 ? "COUNTINGWAND_MESSAGE_RCLICK" : "COUNTINGWAND_MESSAGE_LCLICK", point.getX(), point.getY(), point.getZ(), dimension, volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
-6
@@ -27,7 +27,6 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.util.RayTraceResult;
|
||||
|
||||
import java.util.Objects;
|
||||
@@ -67,9 +66,4 @@ public class CountingwandListener implements Listener {
|
||||
event.setCancelled(true);
|
||||
Countingwand.checkSelection(Point.fromLocation(Objects.requireNonNull(event.getClickedBlock()).getLocation()), false, event.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onLeave(PlayerQuitEvent event) {
|
||||
Countingwand.removePlayer(event.getPlayer());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user