SteamWar/BauSystem
Archiviert
13
0

Simplify TNTSimulator

Dieser Commit ist enthalten in:
jojo 2021-01-28 10:40:33 +01:00
Ursprung 6c030c1f9d
Commit a219bfc7b0
5 geänderte Dateien mit 153 neuen und 170 gelöschten Zeilen

Datei anzeigen

@ -19,7 +19,7 @@
package de.steamwar.bausystem; package de.steamwar.bausystem;
import de.steamwar.bausystem.cannonsimulator.TNTSimulatorListener; import de.steamwar.bausystem.world.TNTSimulatorListener;
import de.steamwar.bausystem.commands.*; import de.steamwar.bausystem.commands.*;
import de.steamwar.bausystem.world.*; import de.steamwar.bausystem.world.*;
import de.steamwar.core.CommandRemover; import de.steamwar.core.CommandRemover;

Datei anzeigen

@ -1,133 +0,0 @@
/*
*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* /
*/
package de.steamwar.bausystem.cannonsimulator;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.util.Vector;
public class TNTSpawn implements Comparable<TNTSpawn> {
private static final World WORLD = Bukkit.getWorlds().get(0);
private String name = "";
private Vector position;
private int fuseTicks = 80;
private int count = 1;
private int tickOffset = 0;
private boolean xVelocity = true;
private boolean yVelocity = true;
private boolean zVelocity = true;
private boolean comparator = false;
public TNTSpawn(Vector position) {
this.position = position;
}
public void spawn() {
WORLD.spawn(position.toLocation(WORLD), TNTPrimed.class, tntPrimed -> {
tntPrimed.setFuseTicks(fuseTicks);
if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0));
if (!yVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setY(0));
if (!zVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setZ(0));
});
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Vector getPosition() {
return position;
}
public void setPosition(Vector position) {
this.position = position;
}
public int getFuseTicks() {
return fuseTicks;
}
public void setFuseTicks(int fuseTicks) {
this.fuseTicks = fuseTicks;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTickOffset() {
return tickOffset;
}
public void setTickOffset(int tickOffset) {
this.tickOffset = tickOffset;
}
public boolean isxVelocity() {
return xVelocity;
}
public void setxVelocity(boolean xVelocity) {
this.xVelocity = xVelocity;
}
public boolean isyVelocity() {
return yVelocity;
}
public void setyVelocity(boolean yVelocity) {
this.yVelocity = yVelocity;
}
public boolean iszVelocity() {
return zVelocity;
}
public void setzVelocity(boolean zVelocity) {
this.zVelocity = zVelocity;
}
public boolean isComparator() {
return comparator;
}
public void setComparator(boolean comparator) {
this.comparator = comparator;
}
@Override
public int compareTo(TNTSpawn tntSpawn) {
return -Integer.compare(tickOffset, tntSpawn.tickOffset);
}
}

Datei anzeigen

@ -23,7 +23,7 @@ package de.steamwar.bausystem.commands;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.cannonsimulator.TNTSimulator; import de.steamwar.bausystem.world.TNTSimulator;
import de.steamwar.bausystem.world.Welt; import de.steamwar.bausystem.world.Welt;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -32,6 +32,12 @@ import org.bukkit.entity.Player;
public class CommandSimulator implements CommandExecutor { public class CommandSimulator implements CommandExecutor {
private void help(Player player) {
player.sendMessage("§8/§esimulator §8- §7Öffnet die Simulations GUI");
player.sendMessage("§8/§esimulator start §8- §7Startet die Simulation");
player.sendMessage("§8/§esimulator wand §8- §7Legt den Simulator ins Inventar");
}
private boolean permissionCheck(Player player) { private boolean permissionCheck(Player player) {
if (Welt.noPermission(player, Permission.world)) { if (Welt.noPermission(player, Permission.world)) {
player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den Simulator nutzen"); player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den Simulator nutzen");
@ -58,6 +64,9 @@ public class CommandSimulator implements CommandExecutor {
case "start": case "start":
TNTSimulator.get(player).start(); TNTSimulator.get(player).start();
break; break;
default:
help(player);
break;
} }
return false; return false;
} }

Datei anzeigen

@ -19,7 +19,7 @@
* / * /
*/ */
package de.steamwar.bausystem.cannonsimulator; package de.steamwar.bausystem.world;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWAnvilInv;
@ -28,13 +28,14 @@ import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv; import de.steamwar.inventory.SWListInv;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Consumer; import org.bukkit.util.Consumer;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class TNTSimulator { public class TNTSimulator {
@ -46,19 +47,20 @@ public class TNTSimulator {
private static final Vector NZ_VECTOR = new Vector(0, 0, -0.0625); private static final Vector NZ_VECTOR = new Vector(0, 0, -0.0625);
private static final List<String> LORE = Collections.singletonList("§7Zum ändern klicken"); private static final List<String> LORE = Collections.singletonList("§7Zum ändern klicken");
static Map<Player, TNTSimulator> tntSimulatorMap = new HashMap<>(); static final Map<Player, TNTSimulator> TNT_SIMULATOR_MAP = new HashMap<>();
private final Set<TNTSpawn> TNT_SPAWNS = new HashSet<>();
public static final ItemStack WAND = new SWItem(Material.TRIPWIRE_HOOK, "§eKanonen Simulator", Arrays.asList("§eRechtsklick Block §8- §7Füge einen TNT hinzu", "§eRechtsklick Luft §8- §7Öffne den Simulator", "§eLinksklick §8- §7Starte die Simulation"), false, null).getItemStack(); public static final ItemStack WAND = new SWItem(Material.TRIPWIRE_HOOK, "§eKanonen Simulator", Arrays.asList("§eRechtsklick Block §8- §7Füge einen TNT hinzu", "§eRechtsklick Luft §8- §7Öffne den Simulator", "§eLinksklick §8- §7Starte die Simulation"), false, null).getItemStack();
public static TNTSimulator get(Player player) { public static TNTSimulator get(Player player) {
return tntSimulatorMap.computeIfAbsent(player, p -> new TNTSimulator()); return TNT_SIMULATOR_MAP.computeIfAbsent(player, p -> new TNTSimulator());
} }
public static void openSimulator(Player player) { public static void openSimulator(Player player) {
TNTSimulator tntSimulator = get(player); TNTSimulator tntSimulator = get(player);
List<SWListInv.SWListEntry<TNTSpawn>> swListEntryList = new ArrayList<>(); List<SWListInv.SWListEntry<TNTSpawn>> swListEntryList = new ArrayList<>();
tntSimulator.tntSpawns.forEach(tntSpawn -> { tntSimulator.TNT_SPAWNS.forEach(tntSpawn -> {
List<String> lore = new ArrayList<>(); List<String> lore = new ArrayList<>();
lore.add("§7Klicken zum einstellen"); lore.add("§7Klicken zum einstellen");
lore.add(""); lore.add("");
@ -77,7 +79,7 @@ public class TNTSimulator {
editTNT(player, tntSpawn); editTNT(player, tntSpawn);
}); });
swListInv.setItem(51, new SWItem(Material.BARRIER, "§cLösche alle TNT", clickType -> { swListInv.setItem(51, new SWItem(Material.BARRIER, "§cLösche alle TNT", clickType -> {
tntSimulator.tntSpawns.clear(); tntSimulator.TNT_SPAWNS.clear();
openSimulator(player); openSimulator(player);
})); }));
swListInv.setItem(49, new SWItem(Material.TNT, "§aNeues TNT", clickType -> { swListInv.setItem(49, new SWItem(Material.TNT, "§aNeues TNT", clickType -> {
@ -88,10 +90,10 @@ public class TNTSimulator {
vector.multiply(0.0625); vector.multiply(0.0625);
TNTSpawn tntSpawn = new TNTSpawn(vector); TNTSpawn tntSpawn = new TNTSpawn(vector);
if (tntSimulator.tntSpawns.contains(tntSpawn)) { if (tntSimulator.TNT_SPAWNS.contains(tntSpawn)) {
return; return;
} }
tntSimulator.tntSpawns.add(tntSpawn); tntSimulator.TNT_SPAWNS.add(tntSpawn);
editTNT(player, tntSpawn); editTNT(player, tntSpawn);
})); }));
swListInv.setItem(47, new SWItem(Material.FLINT_AND_STEEL, "§eSimulation starten", clickType -> { swListInv.setItem(47, new SWItem(Material.FLINT_AND_STEEL, "§eSimulation starten", clickType -> {
@ -111,7 +113,7 @@ public class TNTSimulator {
// Delete tnt // Delete tnt
swInventory.setItem(53, new SWItem(Material.BARRIER, "§cEntfernen", clickType -> { swInventory.setItem(53, new SWItem(Material.BARRIER, "§cEntfernen", clickType -> {
tntSimulator.tntSpawns.remove(tntSpawn); tntSimulator.TNT_SPAWNS.remove(tntSpawn);
openSimulator(player); openSimulator(player);
})); }));
@ -135,7 +137,7 @@ public class TNTSimulator {
editTNT(player, tntSpawn); editTNT(player, tntSpawn);
})); }));
swInventory.setItem(19, new SWItem(Material.TNT, "§eAnzahl §8- §7" + tntSpawn.getCount(), LORE, false, clickType -> { swInventory.setItem(19, new SWItem(Material.TNT, "§eAnzahl §8- §7" + tntSpawn.getCount(), LORE, false, clickType -> {
changeCount(player, tntSpawn.getCount(), count -> { changeCount(player, "Anzahl TNT", tntSpawn.getCount(), count -> {
if (count < 1) count = 1; if (count < 1) count = 1;
if (count > 400) count = 400; if (count > 400) count = 400;
tntSpawn.setCount(count); tntSpawn.setCount(count);
@ -159,7 +161,7 @@ public class TNTSimulator {
editTNT(player, tntSpawn); editTNT(player, tntSpawn);
})); }));
swInventory.setItem(20, new SWItem(Material.CLOCK, "§eTick §8- §7" + tntSpawn.getTickOffset(), LORE, false, clickType -> { swInventory.setItem(20, new SWItem(Material.CLOCK, "§eTick §8- §7" + tntSpawn.getTickOffset(), LORE, false, clickType -> {
changeCount(player, tntSpawn.getTickOffset(), tick -> { changeCount(player, "Tick Offset", tntSpawn.getTickOffset(), tick -> {
if (tick < 0) tick = 0; if (tick < 0) tick = 0;
if (tick > 8000) tick = 8000; if (tick > 8000) tick = 8000;
tntSpawn.setTickOffset(tick); tntSpawn.setTickOffset(tick);
@ -182,8 +184,8 @@ public class TNTSimulator {
} }
editTNT(player, tntSpawn); editTNT(player, tntSpawn);
})); }));
swInventory.setItem(21, new SWItem(Material.CLOCK, "§eFuseTicks §8- §7" + tntSpawn.getFuseTicks(), LORE, false, clickType -> { swInventory.setItem(21, new SWItem(Material.CLOCK, "§eFuse-Ticks §8- §7" + tntSpawn.getFuseTicks(), LORE, false, clickType -> {
changeCount(player, tntSpawn.getFuseTicks(), tick -> { changeCount(player, "Fuse-Ticks", tntSpawn.getFuseTicks(), tick -> {
if (tick < 0) tick = 0; if (tick < 0) tick = 0;
if (tick > 80) tick = 80; if (tick > 80) tick = 80;
tntSpawn.setFuseTicks(tick); tntSpawn.setFuseTicks(tick);
@ -269,24 +271,24 @@ public class TNTSimulator {
} }
static void startSimulation(Player player) { static void startSimulation(Player player) {
if (tntSimulatorMap.containsKey(player)) { if (TNT_SIMULATOR_MAP.containsKey(player)) {
tntSimulatorMap.get(player).start(); TNT_SIMULATOR_MAP.get(player).start();
} }
} }
static void addTNT(Player player, TNTSpawn tntSpawn) { static void addTNT(Player player, TNTSpawn tntSpawn) {
if (!tntSimulatorMap.containsKey(player)) { if (!TNT_SIMULATOR_MAP.containsKey(player)) {
tntSimulatorMap.put(player, new TNTSimulator()); TNT_SIMULATOR_MAP.put(player, new TNTSimulator());
} }
tntSimulatorMap.get(player).tntSpawns.add(tntSpawn); TNT_SIMULATOR_MAP.get(player).TNT_SPAWNS.add(tntSpawn);
} }
private static String active(boolean b) { private static String active(boolean b) {
return b ? "§aan" : "§caus"; return b ? "§aan" : "§caus";
} }
private static void changeCount(Player player, int defaultValue, Consumer<Integer> result, Runnable failure) { private static void changeCount(Player player, String name, int defaultValue, Consumer<Integer> result, Runnable failure) {
SWAnvilInv swAnvilInv = new SWAnvilInv(player, "Zahl", defaultValue + ""); SWAnvilInv swAnvilInv = new SWAnvilInv(player, name, defaultValue + "");
swAnvilInv.setItem(Material.PAPER); swAnvilInv.setItem(Material.PAPER);
swAnvilInv.setCallback(s -> { swAnvilInv.setCallback(s -> {
try { try {
@ -315,8 +317,6 @@ public class TNTSimulator {
return (int)(d * 10000) * 0.0001; return (int)(d * 10000) * 0.0001;
} }
private Set<TNTSpawn> tntSpawns = new HashSet<>();
private static class TNTSpawnPair { private static class TNTSpawnPair {
private int countLeft; private int countLeft;
@ -336,7 +336,7 @@ public class TNTSimulator {
public void start() { public void start() {
Map<Integer, List<TNTSpawnPair>[]> tntSpawnMap = new HashMap<>(); Map<Integer, List<TNTSpawnPair>[]> tntSpawnMap = new HashMap<>();
tntSpawns.forEach(tntSpawn -> { TNT_SPAWNS.forEach(tntSpawn -> {
tntSpawnMap.computeIfAbsent(tntSpawn.getTickOffset(), integer -> new ArrayList[]{new ArrayList<>(), new ArrayList<>()})[tntSpawn.isComparator() ? 1 : 0].add(new TNTSpawnPair(tntSpawn.getCount(), tntSpawn)); tntSpawnMap.computeIfAbsent(tntSpawn.getTickOffset(), integer -> new ArrayList[]{new ArrayList<>(), new ArrayList<>()})[tntSpawn.isComparator() ? 1 : 0].add(new TNTSpawnPair(tntSpawn.getCount(), tntSpawn));
}); });
tntSpawnMap.forEach((integer, tntSpawnPairs) -> { tntSpawnMap.forEach((integer, tntSpawnPairs) -> {
@ -348,15 +348,121 @@ public class TNTSimulator {
} }
private void spawnRandomList(List<TNTSpawnPair> tntSpawnPairs) { private void spawnRandomList(List<TNTSpawnPair> tntSpawnPairs) {
AtomicInteger index = new AtomicInteger(tntSpawnPairs.size() - 1); int index = tntSpawnPairs.size() - 1;
while (!tntSpawnPairs.isEmpty()) { while (!tntSpawnPairs.isEmpty()) {
TNTSpawnPair tntSpawnPair = tntSpawnPairs.get(index.get()); TNTSpawnPair tntSpawnPair = tntSpawnPairs.get(index);
tntSpawnPair.spawn(); tntSpawnPair.spawn();
if (tntSpawnPair.countLeft <= 0) { if (tntSpawnPair.countLeft <= 0) {
tntSpawnPairs.remove(index.get()); tntSpawnPairs.remove(index);
} }
if (index.decrementAndGet() < 0) index.set(tntSpawnPairs.size() - 1); if (--index < 0) index = tntSpawnPairs.size() - 1;
} }
} }
public static class TNTSpawn implements Comparable<TNTSpawn> {
private static final World WORLD = Bukkit.getWorlds().get(0);
private String name = "";
private Vector position;
private int fuseTicks = 80;
private int count = 1;
private int tickOffset = 0;
private boolean xVelocity = true;
private boolean yVelocity = true;
private boolean zVelocity = true;
private boolean comparator = false;
public TNTSpawn(Vector position) {
this.position = position;
}
public void spawn() {
WORLD.spawn(position.toLocation(WORLD), TNTPrimed.class, tntPrimed -> {
tntPrimed.setFuseTicks(fuseTicks);
if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0));
if (!yVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setY(0));
if (!zVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setZ(0));
});
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Vector getPosition() {
return position;
}
public void setPosition(Vector position) {
this.position = position;
}
public int getFuseTicks() {
return fuseTicks;
}
public void setFuseTicks(int fuseTicks) {
this.fuseTicks = fuseTicks;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTickOffset() {
return tickOffset;
}
public void setTickOffset(int tickOffset) {
this.tickOffset = tickOffset;
}
public boolean isxVelocity() {
return xVelocity;
}
public void setxVelocity(boolean xVelocity) {
this.xVelocity = xVelocity;
}
public boolean isyVelocity() {
return yVelocity;
}
public void setyVelocity(boolean yVelocity) {
this.yVelocity = yVelocity;
}
public boolean iszVelocity() {
return zVelocity;
}
public void setzVelocity(boolean zVelocity) {
this.zVelocity = zVelocity;
}
public boolean isComparator() {
return comparator;
}
public void setComparator(boolean comparator) {
this.comparator = comparator;
}
@Override
public int compareTo(TNTSpawn tntSpawn) {
return -Integer.compare(tickOffset, tntSpawn.tickOffset);
}
}
} }

Datei anzeigen

@ -19,11 +19,10 @@
* / * /
*/ */
package de.steamwar.bausystem.cannonsimulator; package de.steamwar.bausystem.world;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.Welt;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -32,6 +31,8 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import static de.steamwar.bausystem.world.TNTSimulator.*;
public class TNTSimulatorListener implements Listener { public class TNTSimulatorListener implements Listener {
private static final Vector HALF = new Vector(0.5, 0, 0.5); private static final Vector HALF = new Vector(0.5, 0, 0.5);
@ -47,29 +48,29 @@ public class TNTSimulatorListener implements Listener {
@EventHandler @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getItem() == null) return; if (event.getItem() == null) return;
if (!event.getItem().isSimilar(TNTSimulator.WAND)) return; if (!event.getItem().isSimilar(WAND)) return;
event.setCancelled(true); event.setCancelled(true);
if (!permissionCheck(event.getPlayer())) { if (!permissionCheck(event.getPlayer())) {
return; return;
} }
if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) { if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) {
TNTSimulator.startSimulation(event.getPlayer()); startSimulation(event.getPlayer());
return; return;
} }
if (event.getClickedBlock() == null) { if (event.getClickedBlock() == null) {
TNTSimulator.openSimulator(event.getPlayer()); openSimulator(event.getPlayer());
return; return;
} }
Vector location = event.getClickedBlock().getLocation().toVector().add(event.getBlockFace().getDirection()).add(HALF); Vector location = event.getClickedBlock().getLocation().toVector().add(event.getBlockFace().getDirection()).add(HALF);
TNTSpawn tntSpawn = new TNTSpawn(location); TNTSpawn tntSpawn = new TNTSpawn(location);
TNTSimulator.addTNT(event.getPlayer(), tntSpawn); addTNT(event.getPlayer(), tntSpawn);
TNTSimulator.editTNT(event.getPlayer(), tntSpawn); editTNT(event.getPlayer(), tntSpawn);
} }
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
TNTSimulator.tntSimulatorMap.remove(event.getPlayer()); TNT_SIMULATOR_MAP.remove(event.getPlayer());
} }
} }