Add BauSystem module

Fix ci java version
Fix LinkageProcessor
This commit is contained in:
2024-08-05 13:28:50 +02:00
parent 41d31e6c9c
commit 3366a30b0c
526 changed files with 43550 additions and 149479 deletions
@@ -0,0 +1,112 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 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.features.attributescopy;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.MinVersion;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@Linked
public class AttributeRemoveCommand extends SWCommand {
public AttributeRemoveCommand() {
super("removeattribute", "attributesremove");
}
@Register({"all"})
@Register({"*"})
public void genericCommand(@Validator Player player) {
ItemStack itemStack = player.getInventory().getItemInMainHand();
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setLore(new ArrayList<>());
itemStack.setItemMeta(itemMeta);
BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_ALL", player);
}
@Register(description = "ATTRIBUTE_REMOVE_COMMAND_HELP")
public void genericCommand(@Validator Player player, @Mapper("attribute") String attribute) {
ItemStack itemStack = player.getInventory().getItemInMainHand();
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null) {
BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_NOT_FOUND", player);
return;
}
List<String> lore = itemMeta.getLore();
if (lore == null) {
BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_NOT_FOUND", player);
return;
}
if (lore.isEmpty()) {
BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_NOT_FOUND", player);
return;
}
if (!lore.get(0).equals("§eAttributes§8:")) {
BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_NOT_FOUND", player);
return;
}
lore.removeIf(s -> s.startsWith("§8-§7 " + attribute + "§8:"));
if (lore.size() == 1) {
itemStack.setItemMeta(null);
} else {
itemMeta.setLore(lore);
itemStack.setItemMeta(itemMeta);
}
BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_SINGLE", player, attribute);
}
@Mapper(value = "attribute", local = true)
public TypeMapper<String> attribute() {
return new TypeMapper<String>() {
@Override
public Collection<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
Player player = (Player) commandSender;
ItemStack itemStack = player.getInventory().getItemInMainHand();
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null) return null;
List<String> lore = itemMeta.getLore();
if (lore == null) return null;
if (lore.isEmpty()) return null;
if (!lore.get(0).equals("§eAttributes§8:")) return null;
return lore.stream()
.skip(1)
.map(s1 -> s1.substring(6))
.map(s1 -> s1.substring(0, s1.indexOf("§8:")))
.collect(Collectors.toList());
}
@Override
public String map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
return s;
}
};
}
}
@@ -0,0 +1,128 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.attributescopy;
import lombok.experimental.UtilityClass;
import org.bukkit.block.data.BlockData;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@UtilityClass
public class AttributeUtils {
private Map<Method, String> names = new HashMap<>();
private Map<Class<?>, List<Method>> getters = new HashMap<>();
private Map<Class<?>, Map<String, Method>> setters = new HashMap<>();
private void generate(BlockData blockData) {
Class<? extends BlockData> clazz = blockData.getClass();
if (getters.containsKey(clazz) && setters.containsKey(clazz)) return;
Map<String, List<Method>> methods = new HashMap<>();
for (Method declaredMethod : clazz.getMethods()) {
String s = declaredMethod.getName();
if (s.startsWith("get") && declaredMethod.getParameterCount() == 0) {
methods.computeIfAbsent(s.substring(3), aClass -> new ArrayList<>()).add(declaredMethod);
} else if (s.startsWith("is") && declaredMethod.getParameterCount() == 0) {
methods.computeIfAbsent(s.substring(2), aClass -> new ArrayList<>()).add(declaredMethod);
} else if (s.startsWith("set") && declaredMethod.getParameterCount() == 1) {
methods.computeIfAbsent(s.substring(3), aClass -> new ArrayList<>()).add(declaredMethod);
}
}
for (Map.Entry<String, List<Method>> entry : methods.entrySet()) {
if (entry.getValue().size() != 2) continue;
for (Method method : entry.getValue()) {
names.put(method, entry.getKey());
if (method.getName().startsWith("is") || method.getName().startsWith("get")) {
getters.computeIfAbsent(clazz, aClass -> new ArrayList<>()).add(method);
} else {
setters.computeIfAbsent(clazz, aClass -> new HashMap<>()).put(entry.getKey(), method);
}
}
}
}
public void copy(BlockData blockData, List<String> attributes) {
generate(blockData);
getters.getOrDefault(blockData.getClass(), new ArrayList<>()).forEach(method -> {
try {
Object invoke = method.invoke(blockData);
if (invoke != null) {
attributes.add("§8-§7 " + names.get(method) + "§8:§7 " + convert(invoke));
}
} catch (Exception e) {
// ignore
}
});
}
public void paste(BlockData blockData, List<String> attributes) {
generate(blockData);
for (String attribute : attributes) {
String[] split = attribute.split("§8:§7 ");
if (split.length != 2) continue;
String name = split[0].substring(6);
String value = split[1];
Method method = setters.getOrDefault(blockData.getClass(), new HashMap<>()).get(name);
if (method == null) continue;
try {
method.invoke(blockData, convert(value, method.getParameterTypes()[0]));
} catch (Exception e) {
// ignore
}
}
}
private String convert(Object o) {
if (o.getClass().isEnum()) {
return ((Enum<?>) o).name();
} else {
return o.toString();
}
}
private Object convert(String s, Class<?> type) {
if (type.isEnum()) {
return Enum.valueOf((Class<? extends Enum>) type, s);
} else if (type == int.class || type == Integer.class) {
return Integer.parseInt(s);
} else if (type == double.class || type == Double.class) {
return Double.parseDouble(s);
} else if (type == float.class || type == Float.class) {
return Float.parseFloat(s);
} else if (type == long.class || type == Long.class) {
return Long.parseLong(s);
} else if (type == short.class || type == Short.class) {
return Short.parseShort(s);
} else if (type == byte.class || type == Byte.class) {
return Byte.parseByte(s);
} else if (type == boolean.class || type == Boolean.class) {
return Boolean.parseBoolean(s);
} else {
return s;
}
}
}
@@ -0,0 +1,88 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 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.features.attributescopy;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.FluidCollisionMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.List;
@Linked
public class AttributesCopyCommand extends SWCommand {
public AttributesCopyCommand() {
super("copyattributes", "attributescopy", "ac");
}
@Register
public void genericCommand(@Validator Player player) {
Block block = player.getTargetBlockExact(8, FluidCollisionMode.ALWAYS);
if (block == null) return;
ItemStack mainHand = player.getInventory().getItemInMainHand();
if (!(block.getType().isItem() && block.getType() == mainHand.getType() || isSame(block, mainHand))) {
BauSystem.MESSAGE.send("ATTRIBUTES_CANT_COPY", player);
return;
}
BlockData blockData = block.getBlockData();
List<String> attributesToCopy = new ArrayList<>();
if (block.getType() != mainHand.getType()) {
attributesToCopy.add("§8-§7 Material§8:§7 " + block.getType().name());
}
AttributeUtils.copy(blockData, attributesToCopy);
if (attributesToCopy.isEmpty()) {
BauSystem.MESSAGE.send("ATTRIBUTES_NO_COPY", player);
return;
}
ItemMeta itemMeta = mainHand.getItemMeta();
List<String> lore = new ArrayList<>(attributesToCopy);
lore.add(0, "§eAttributes§8:");
itemMeta.setLore(lore);
mainHand.setItemMeta(itemMeta);
player.getInventory().setItemInMainHand(mainHand);
BauSystem.MESSAGE.send("ATTRIBUTES_COPIED", player);
}
private boolean isSame(Block block, ItemStack itemStack) {
if (itemStack.getType() == Material.REDSTONE && block.getType() == Material.REDSTONE_WIRE) return true;
if (itemStack.getType() == Material.PLAYER_HEAD && block.getType() == Material.PLAYER_WALL_HEAD) return true;
if (itemStack.getType() == Material.ZOMBIE_HEAD && block.getType() == Material.ZOMBIE_WALL_HEAD) return true;
if (itemStack.getType() == Material.CREEPER_HEAD && block.getType() == Material.CREEPER_WALL_HEAD) return true;
if (itemStack.getType() == Material.DRAGON_HEAD && block.getType() == Material.DRAGON_WALL_HEAD) return true;
if (itemStack.getType() == Material.SKELETON_SKULL && block.getType() == Material.SKELETON_WALL_SKULL) return true;
if (itemStack.getType() == Material.WITHER_SKELETON_SKULL && block.getType() == Material.WITHER_SKELETON_WALL_SKULL) return true;
if (itemStack.getType() == Material.TORCH && block.getType() == Material.WALL_TORCH) return true;
if (itemStack.getType() == Material.SOUL_TORCH && block.getType() == Material.SOUL_WALL_TORCH) return true;
if (itemStack.getType() == Material.REDSTONE_TORCH && block.getType() == Material.REDSTONE_WALL_TORCH) return true;
if (itemStack.getType() == Material.WHEAT_SEEDS && block.getType() == Material.WHEAT) return true;
if (itemStack.getType().name().contains("_BANNER") && block.getType().name().contains("_WALL_BANNER")) return true;
return false;
}
}
@@ -0,0 +1,88 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 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.features.attributescopy;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;
@Linked
public class AttributesPlaceListener implements Listener {
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
ItemStack itemStack = event.getItemInHand();
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null) return;
List<String> strings = itemMeta.getLore();
if (strings == null) return;
if (strings.isEmpty()) return;
if (!strings.get(0).equals("§eAttributes§8:")) return;
Material type = event.getBlock().getType();
OfflinePlayer offlinePlayer = null;
if (event.getBlock().getState() instanceof Skull) {
Skull skull = (Skull) event.getBlock().getState();
offlinePlayer = skull.getOwningPlayer();
}
OfflinePlayer finalPlayerProfile = offlinePlayer;
event.setCancelled(true);
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
Material material = strings.stream()
.filter(s -> s.startsWith("§8-§7 Material§8:§7 "))
.map(s -> s.replace("§8-§7 Material§8:§7 ", ""))
.map(String::toUpperCase)
.map(s -> {
try {
return Material.valueOf(s);
} catch (Exception e) {
return null;
}
})
.findFirst()
.orElse(type);
event.getBlock().setType(material, false);
Block block = event.getBlock();
BlockData blockData = block.getBlockData();
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
if (block.getState() instanceof Skull && finalPlayerProfile != null) {
Skull skull = (Skull) block.getState();
skull.setOwningPlayer(finalPlayerProfile);
skull.update(true, false);
}
}, 1);
AttributeUtils.paste(blockData, strings);
block.setBlockData(blockData, false);
}, 1);
}
}
@@ -0,0 +1,38 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.autostart;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class AutoStartCommand extends SWCommand {
public AutoStartCommand() {
super("timer", "autostarttimer", "at", "autostart");
}
@Register(description = "AUTOSTART_COMMAND_HELP")
public void genericCommand(@Validator Player p) {
SWUtils.giveItemToPlayer(p, AutostartListener.getWandItem(p));
}
}
@@ -0,0 +1,55 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.autostart;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class AutoStartGuiItem extends BauGuiItem {
public AutoStartGuiItem() {
super(24);
}
@Override
public ItemStack getItem(Player player) {
ItemStack itemStack = AutostartListener.getWandItem(player);
itemStack.setType(Material.FIREWORK_STAR);
return itemStack;
}
@Override
public boolean click(ClickType click, Player p) {
p.closeInventory();
p.performCommand("timer");
return false;
}
@Override
public Permission permission() {
return Permission.BUILD;
}
}
@@ -0,0 +1,150 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.autostart;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.ItemUtils;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.type.Chest;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
@Linked
public class AutostartListener implements Listener {
@Getter
public static AutostartListener instance;
{
instance = this;
}
public static ItemStack getWandItem(Player player) {
ItemStack itemStack = new SWItem(Material.FIREWORK_STAR, BauSystem.MESSAGE.parse("AUTOSTART_ITEM_NAME", player), Arrays.asList(BauSystem.MESSAGE.parse("AUTOSTART_ITEM_LORE", player)), false, null).getItemStack();
ItemUtils.setItem(itemStack, "autostart");
return itemStack;
}
@Getter
private Map<Region, Long> regionStartTime = new HashMap<>();
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
if (!ItemUtils.isItem(event.getItem(), "autostart")) {
return;
}
if (event.getClickedBlock() == null) {
return;
}
if (event.getClickedBlock().getBlockData() instanceof Chest) {
return;
}
if (event.getClickedBlock().getType() == Material.BEDROCK) {
event.getClickedBlock().setType(Material.SLIME_BLOCK);
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
event.getClickedBlock().setType(Material.BEDROCK, false);
}, 1);
}
activate(event.getPlayer());
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player)) {
return;
}
if(!Permission.BUILD.hasPermission((Player) event.getPlayer())) return;
if (!ItemUtils.isItem(event.getPlayer().getInventory().getItemInMainHand(), "autostart")) {
return;
}
if (event.getInventory().getLocation() == null) {
return;
}
if (event.getInventory().getLocation().getBlock().getBlockData() instanceof Chest) {
activate((Player) event.getPlayer());
}
}
public void activate(Player player) {
Region region = Region.getRegion(player.getLocation());
if (region.isGlobal()) {
BauSystem.MESSAGE.send("AUTOSTART_MESSAGE_NO_REGION", player);
return;
}
if (!region.hasType(RegionType.TESTBLOCK)) {
BauSystem.MESSAGE.send("AUTOSTART_MESSAGE_NO_REGION", player);
return;
}
if (regionStartTime.containsKey(region)) {
BauSystem.MESSAGE.send("AUTOSTART_MESSAGE_RESET", player);
} else {
BauSystem.MESSAGE.send("AUTOSTART_MESSAGE_START", player);
}
regionStartTime.put(region, TPSUtils.currentRealTick.get());
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
if (regionStartTime.isEmpty()) {
return;
}
event.blockList().forEach(block -> {
Region region = Region.getRegion(block.getLocation());
if (!regionStartTime.containsKey(region)) return;
if (!region.hasType(RegionType.TESTBLOCK)) return;
if (!region.inRegion(block.getLocation(), RegionType.TESTBLOCK, RegionExtensionType.EXTENSION)) return;
long tickDiff = TPSUtils.currentRealTick.get() - regionStartTime.remove(region);
long preFightDurationInSeconds = getPreFightDurationInSeconds(region);
RegionUtils.message(region, "AUTOSTART_MESSAGE_RESULT1", tickDiff);
RegionUtils.message(region, "AUTOSTART_MESSAGE_RESULT2", preFightDurationInSeconds, ((preFightDurationInSeconds * 20) - tickDiff));
RegionUtils.message(region, "AUTOSTART_MESSAGE_RESULT3");
});
}
private int getPreFightDurationInSeconds(Region region) {
File file = region.gameModeConfig();
if (file == null) return 30;
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
return config.getInt("Times.PreFightDuration", 30);
}
}
@@ -0,0 +1,154 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.backup;
import com.sk89q.worldedit.EditSession;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.Color;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ColorMode;
import de.steamwar.bausystem.region.tags.Tag;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.command.SWCommand;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import de.steamwar.command.TypeValidator;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.chat.ClickEvent;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Linked
public class BackupCommand extends SWCommand {
public BackupCommand() {
super("backup", "bu");
}
static boolean checkGlobalRegion(Region region, Player p) {
if (region.isGlobal()) {
BauSystem.MESSAGE.send("BACKUP_REGION_NO_REGION", p);
return true;
}
return false;
}
@Register(value = "create", description = "BACKUP_HELP_CREATE")
public void backupCreate(@Validator("owner") Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
if (!region.get(Tag.CHANGED)) {
BauSystem.MESSAGE.send("BACKUP_CREATE_NO_CHANGE", p);
return;
}
if (region.backup()) {
BauSystem.MESSAGE.send("BACKUP_CREATE_SUCCESS", p);
} else {
BauSystem.MESSAGE.send("BACKUP_CREATE_FAILURE", p);
}
}
@Register(value = "load", description = "BACKUP_HELP_LOAD")
public void backupLoad(@Validator("owner") Player p, @Mapper("backupName") String backupName) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
File backupFile = region.getBackupFile(backupName.replace('_', ' '));
if (backupFile == null) {
BauSystem.MESSAGE.send("BACKUP_LOAD_FAILURE", p);
return;
}
EditSession editSession = new PasteBuilder(new PasteBuilder.FileProvider(backupFile))
.pastePoint(region.getMinPoint().add(region.getPrototype().getSizeX() / 2, 0, region.getPrototype().getSizeZ() / 2))
.minPoint(region.getMinPoint())
.maxPoint(region.getMaxPoint())
.waterLevel(region.getWaterLevel())
.run();
region.remember(editSession);
BauSystem.MESSAGE.send("BACKUP_LOAD", p);
}
@Register(value = "list", description = "BACKUP_HELP_LIST")
public void backupList(Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
List<String> backups = listBackup(p);
BauSystem.MESSAGE.send("BACKUP_LIST_HEAD", p, backups.size());
backups.forEach(s -> {
BauSystem.MESSAGE.send("BACKUP_LIST_ENTRY", p, "/backup load " + s, new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/backup load " + s), s);
});
}
@Register(value = "gui", description = "BACKUP_HELP_GUI")
public void backupGui(Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
List<String> backups = listBackup(p);
List<SWListInv.SWListEntry<String>> swListEntries = new ArrayList<>();
List<String> lore = Arrays.asList(BauSystem.MESSAGE.parse("BACKUP_LORE", p));
for (int i = 0; i < backups.size(); i++) {
String s = backups.get(i);
SWItem swItem = new SWItem(Material.BRICK, BauSystem.MESSAGE.parse("BACKUP_ITEM_NAME", p, s), lore, false, clickType -> {});
swItem.getItemStack().setAmount(i + 1);
swListEntries.add(new SWListInv.SWListEntry<>(swItem, s));
}
SWListInv<String> swListInv = new SWListInv<>(p, BauSystem.MESSAGE.parse("BACKUP_INV_NAME", p), swListEntries, (clickType, s) -> {
p.getOpenInventory().close();
p.performCommand("backup load " + s);
});
swListInv.open();
}
@Mapper(value = "backupName", local = true)
public TypeMapper<String> backupMapper() {
return SWCommandUtils.createMapper(s -> s, (commandSender, s) -> listBackup((Player) commandSender));
}
private List<String> listBackup(Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return Collections.emptyList();
}
try {
return region.listBackup().stream().map(s -> s.substring(0, s.length() - 6).replace(' ', '_')).collect(Collectors.toList());
} catch (NullPointerException e) {
return Collections.emptyList();
}
}
}
@@ -0,0 +1,83 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.bau;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.config.BauServer;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.core.Core;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
@Linked
public class BauInfoBauGuiItem extends BauGuiItem {
public BauInfoBauGuiItem() {
super(7);
}
@Override
public ItemStack getItem(Player player) {
SWItem itemStack;
if (!player.getName().endsWith("")) {
itemStack = SWItem.getPlayerSkull(SteamwarUser.get(BauServer.getInstance().getOwner()).getUserName());
} else {
itemStack = new SWItem(Material.PLAYER_HEAD, "");
}
itemStack.setName(BauSystem.MESSAGE.parse("BAU_INFO_ITEM_NAME", player));
Region region = Region.getRegion(player.getLocation());
List<String> stringList = new ArrayList<>();
for (Flag flag : Flag.getFlags()) {
if (flag == Flag.PROTECT && region.getFloorLevel() == 0) {
continue;
}
if (flag == Flag.ITEMS && Core.getVersion() < 19) {
continue;
}
Flag.Value<?> value = region.get(flag);
if (value != null) {
stringList.add(BauSystem.MESSAGE.parse("BAU_INFO_ITEM_LORE_" + flag.name(), player, BauSystem.MESSAGE.parse(value.getChatValue(), player)));
}
}
itemStack.setLore(stringList);
return itemStack.getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
return false;
}
@Override
public Permission permission() {
return Permission.MEMBER;
}
}
@@ -0,0 +1,73 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.bau;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import de.steamwar.techhider.TechHider;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Collection;
import java.util.stream.Collectors;
@Linked
public class ForceSpectatorCommand extends SWCommand {
public ForceSpectatorCommand() {
super("forcespectator");
}
@Register
public void forceSpectator(@Validator("supervisor") Player player, @Mapper("builder") Player other) {
Permission.forceOnlySpectator(other);
}
@Mapper("builder")
public TypeMapper<Player> spectatorMapper() {
return new TypeMapper<>() {
@Override
public Player map(CommandSender commandSender, String[] previousArguments, String s) {
Player player = Bukkit.getPlayer(s);
if (player == null) {
return null;
}
if (Permission.BUILD.hasPermission(player) && !Permission.SUPERVISOR.hasPermission(player)) {
return player;
}
return null;
}
@Override
public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
return Bukkit.getOnlinePlayers().stream()
.filter(Permission.BUILD::hasPermission)
.filter(player -> !Permission.SUPERVISOR.hasPermission(player))
.map(Player::getName)
.collect(Collectors.toList());
}
};
}
}
@@ -0,0 +1,86 @@
package de.steamwar.bausystem.features.bau;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.config.BauServer;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.command.SWCommand;
import de.steamwar.core.TPSWatcher;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import de.steamwar.sql.BauweltMember;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.entity.Player;
import java.util.*;
@Linked
public class InfoCommand extends SWCommand {
@LinkedInstance
public BauServer bauServer;
public InfoCommand() {
super("bauinfo");
}
@Register(description = "BAU_INFO_COMMAND_HELP")
public void genericCommand(Player p) {
BauSystem.MESSAGE.send("BAU_INFO_COMMAND_OWNER", p, SteamwarUser.get(bauServer.getOwnerID()).getUserName());
Region region = Region.getRegion(p.getLocation());
for (Flag flag : Flag.getFlags()) {
if (flag == Flag.PROTECT && region.getFloorLevel() == 0) {
continue;
}
Flag.Value<?> value = region.get(flag);
if (value != null) {
BauSystem.MESSAGE.send("BAU_INFO_COMMAND_FLAG", p, BauSystem.MESSAGE.parse(flag.getChatValue(), p), BauSystem.MESSAGE.parse(value.getChatValue(), p));
}
}
if (Permission.BUILD.hasPermission(p)) {
List<BauweltMember> members = BauweltMember.getMembers(bauServer.getOwnerID());
Map<Permission, List<BauweltMember>> memberByPermission = new HashMap<>();
members.forEach(member -> {
if (Permission.SUPERVISOR.hasPermission(member)) {
memberByPermission.computeIfAbsent(Permission.SUPERVISOR, __ -> new ArrayList<>()).add(member);
} else if (Permission.BUILD.hasPermission(member)) {
memberByPermission.computeIfAbsent(Permission.BUILD, __ -> new ArrayList<>()).add(member);
} else {
memberByPermission.computeIfAbsent(Permission.MEMBER, __ -> new ArrayList<>()).add(member);
}
});
List<BauweltMember> supervisor = memberByPermission.getOrDefault(Permission.SUPERVISOR, Collections.emptyList());
BauSystem.MESSAGE.send("BAU_INFO_COMMAND_MEMBER", p, "§eSupervisor", supervisor.size(), supervisor.isEmpty() ? "§8<none>" : joining(supervisor));
List<BauweltMember> builder = memberByPermission.getOrDefault(Permission.BUILD, Collections.emptyList());
BauSystem.MESSAGE.send("BAU_INFO_COMMAND_MEMBER", p, "§6Builder", builder.size(), builder.isEmpty() ? "§8<none>" : joining(builder));
List<BauweltMember> spectator = memberByPermission.getOrDefault(Permission.MEMBER, Collections.emptyList());
BauSystem.MESSAGE.send("BAU_INFO_COMMAND_MEMBER", p, "§7Spectator", spectator.size(), spectator.isEmpty() ? "§8<none>" : joining(spectator));
}
StringBuilder tpsMessage = new StringBuilder();
tpsMessage.append(BauSystem.MESSAGE.parsePrefixed("BAU_INFO_COMMAND_TPS", p));
tpsMessage.append(" ").append(TPSWatcher.getTPS(TPSWatcher.TPSType.ONE_SECOND));
tpsMessage.append(" ").append(TPSWatcher.getTPS(TPSWatcher.TPSType.TEN_SECONDS));
tpsMessage.append(" ").append(TPSWatcher.getTPS(TPSWatcher.TPSType.ONE_MINUTE));
tpsMessage.append(" ").append(TPSWatcher.getTPS(TPSWatcher.TPSType.FIVE_MINUTES));
tpsMessage.append(" ").append(TPSWatcher.getTPS(TPSWatcher.TPSType.TEN_MINUTES));
p.sendMessage(tpsMessage.toString());
}
private String joining(List<BauweltMember> bauweltMembers) {
StringBuilder st = new StringBuilder();
for (int i = 0; i < bauweltMembers.size(); i++) {
if (i != 0) {
st.append("§8, ");
}
st.append("§7");
st.append(SteamwarUser.get(bauweltMembers.get(i).getMemberID()).getUserName());
}
return st.toString();
}
}
@@ -0,0 +1,96 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.cannon;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.cannon.depth.Depth;
import de.steamwar.bausystem.features.cannon.depth.DepthManager;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.stream.Collectors;
@Linked
public class CannonDetector implements Listener {
private Map<TNTPrimed, Vector> velocities = new HashMap<>();
private Map<TNTPrimed, Set<UUID>> propulsionOfProjectile = new HashMap<>();
@EventHandler(priority = EventPriority.LOW)
public void onEntityExplode(EntityExplodeEvent event) {
if (!(event.getEntity() instanceof TNTPrimed)) {
return;
}
TNTPrimed tnt = (TNTPrimed) event.getEntity();
propulsionOfProjectile.remove(tnt);
DepthManager.update(tnt, event.blockList());
List<TNTPrimed> tnts = Bukkit.getWorlds().get(0).getEntitiesByClass(TNTPrimed.class)
.stream()
.filter(entity -> entity != tnt)
.filter(entity -> entity.getFuseTicks() > 1)
.filter(entity -> entity.getLocation().distance(event.getLocation()) <= 8)
.collect(Collectors.toList());
if (tnts.isEmpty()) {
return;
}
boolean isEmpty = velocities.isEmpty();
tnts.forEach(tntPrimed -> {
velocities.put(tntPrimed, tntPrimed.getVelocity().clone());
propulsionOfProjectile.computeIfAbsent(tntPrimed, __ -> new HashSet<>()).add(tnt.getUniqueId());
});
if (!isEmpty) {
return;
}
BauSystem.runTaskLater(BauSystem.getInstance(), () -> {
Map<CannonKey, List<TNTPrimed>> grouped = new HashMap<>();
velocities.forEach((tntPrimed, vector) -> {
boolean xBiggest = Math.abs(vector.getX()) > Math.abs(vector.getZ());
boolean zBiggest = Math.abs(vector.getZ()) > Math.abs(vector.getX());
Vector vec = new Vector(xBiggest ? Math.signum(vector.getX()) : 0, Math.round(vector.getY() * 100), zBiggest ? Math.signum(vector.getZ()) : 0);
grouped.computeIfAbsent(new CannonKey(propulsionOfProjectile.get(tntPrimed), vec), ignored -> new ArrayList<>()).add(tntPrimed);
});
grouped.forEach((cannonKey, tntPrimeds) -> {
if (tntPrimeds.size() <= 5) return;
Region region = Region.getRegion(tntPrimeds.get(0).getLocation());
if (region.isGlobal()) return;
if (!region.hasType(RegionType.TESTBLOCK)) return;
Depth depth = new Depth(region);
DepthManager.init(tntPrimeds, depth);
});
velocities.clear();
}, 1);
}
}
@@ -0,0 +1,37 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.cannon;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.util.Vector;
import java.util.Set;
import java.util.UUID;
@AllArgsConstructor
@EqualsAndHashCode
@Getter
public final class CannonKey {
private Set<UUID> propulsions;
private Vector velocityVector;
}
@@ -0,0 +1,109 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.cannon.depth;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Depth {
private Region region;
private Vector minVector = null;
private Vector maxVector = null;
private int tntCount = 0;
public Depth(Region region) {
this.region = region;
}
public void update(List<Block> blocks) {
List<Block> blocksList = blocks.stream()
.filter(block -> region.inRegion(block.getLocation(), RegionType.TESTBLOCK, RegionExtensionType.EXTENSION))
.collect(Collectors.toList());
tntCount++;
for (Block block : blocksList) {
internalUpdate(block);
}
}
public void finish() {
if (maxVector == null || minVector == null) return;
Vector dimensions = maxVector.subtract(minVector);
dimensions.setX(Math.abs(dimensions.getX()));
dimensions.setY(Math.abs(dimensions.getY()));
dimensions.setZ(Math.abs(dimensions.getZ()));
RegionUtils.message(region, player -> {
player.spigot().sendMessage(getMessage(player, dimensions.getBlockX() + 1, dimensions.getBlockY() + 1, dimensions.getBlockZ() + 1, tntCount));
});
}
private void internalUpdate(Block block) {
if (minVector == null) {
minVector = block.getLocation().toVector();
}
minVector.setX(Math.min(minVector.getX(), block.getX()));
minVector.setY(Math.min(minVector.getY(), block.getY()));
minVector.setZ(Math.min(minVector.getZ(), block.getZ()));
if (maxVector == null) {
maxVector = block.getLocation().toVector();
}
maxVector.setX(Math.max(maxVector.getX(), block.getX()));
maxVector.setY(Math.max(maxVector.getY(), block.getY()));
maxVector.setZ(Math.max(maxVector.getZ(), block.getZ()));
}
private static BaseComponent[] getMessage(Player player, int x, int y, int z, int tntCount) {
final Set<Integer> dimensions = new HashSet<>();
dimensions.add(x);
dimensions.add(y);
dimensions.add(z);
int max = getMax(dimensions);
TextComponent headerComponent = new TextComponent(BauSystem.MESSAGE.parse("DEPTH_COUNTER_MESSAGE", player));
TextComponent depthComponent = new TextComponent(BauSystem.MESSAGE.parse("DEPTH_COUNTER_COUNT", player, x == max ? "§e" : "§7", x, y == max ? "§e" : "§7", y, z == max ? "§e" : "§7", z));
depthComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent[]{new TextComponent(BauSystem.MESSAGE.parse("DEPTH_COUNTER_HOVER", player))}));
TextComponent tntComponent = new TextComponent(BauSystem.MESSAGE.parse("DEPTH_COUNTER_TNT", player, tntCount));
return new BaseComponent[]{headerComponent, depthComponent, tntComponent};
}
private static int getMax(Set<Integer> values) {
return values.stream().max(Integer::compare).orElse(0);
}
}
@@ -0,0 +1,48 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.cannon.depth;
import lombok.experimental.UtilityClass;
import org.bukkit.block.Block;
import org.bukkit.entity.TNTPrimed;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@UtilityClass
public class DepthManager {
private Map<TNTPrimed, Depth> depths = new HashMap<>();
public void init(List<TNTPrimed> list, Depth depth) {
for (TNTPrimed tnt : list) {
depths.putIfAbsent(tnt, depth);
}
}
public void update(TNTPrimed tnt, List<Block> blocks) {
Depth depth = depths.remove(tnt);
if (depth == null) return;
depth.update(blocks);
if (depths.containsValue(depth)) return;
depth.finish();
}
}
@@ -0,0 +1,107 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.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.inventory.SWItem;
import lombok.experimental.UtilityClass;
import org.bukkit.Material;
import org.bukkit.entity.Player;
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 {
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");
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setCustomModelData(1);
itemStack.setItemMeta(itemMeta);
return itemStack;
}
private final Map<String, Pair<Point, Point>> selections = new HashMap<>();
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);
}
}
@@ -0,0 +1,39 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.countingwand;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class CountingwandCommand extends SWCommand {
public CountingwandCommand() {
super("countingwand", "/countingwand", "cwand", "/cwand", "zollstock", "/zollstock");
}
@Register(description = "COUNTINGWAND_COMMAND_HELP")
public void genericCommand(final Player p) {
SWUtils.giveItemToPlayer(p, Countingwand.getWandItem(p));
}
}
@@ -0,0 +1,53 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.countingwand;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class CountingwandGuiItem extends BauGuiItem {
public CountingwandGuiItem() {
super(22);
}
@Override
public ItemStack getItem(Player player) {
return Countingwand.getWandItem(player);
}
@Override
public boolean click(ClickType click, Player p) {
p.closeInventory();
p.performCommand("countingwand");
return false;
}
@Override
public Permission permission() {
return Permission.MEMBER;
}
}
@@ -0,0 +1,75 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.countingwand;
import de.steamwar.bausystem.region.Point;
import de.steamwar.linkage.Linked;
import org.bukkit.FluidCollisionMode;
import org.bukkit.event.EventHandler;
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;
@Linked
public class CountingwandListener implements Listener {
@EventHandler
public void onBlockBreak(final BlockBreakEvent event) {
if (!Countingwand.isCountingwand(event.getPlayer().getInventory().getItemInMainHand())) {
return;
}
event.setCancelled(true);
Countingwand.checkSelection(Point.fromLocation(event.getBlock().getLocation()), true, event.getPlayer());
}
@EventHandler
public void onPlayerInteract(final PlayerInteractEvent event) {
if (!Countingwand.isCountingwand(event.getItem())) {
return;
}
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_AIR) {
RayTraceResult rayTraceResult = event.getPlayer().rayTraceBlocks(200, FluidCollisionMode.NEVER);
if (rayTraceResult == null) {
return;
}
Countingwand.checkSelection(Point.fromLocation(Objects.requireNonNull(rayTraceResult.getHitBlock()).getLocation()), event.getAction() == Action.LEFT_CLICK_AIR, event.getPlayer());
return;
}
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
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());
}
}
@@ -0,0 +1,141 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.design.endstone;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.entity.REntity;
import de.steamwar.entity.REntityServer;
import de.steamwar.entity.RFallingBlockEntity;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DesignEndStone {
private static final World WORLD = Bukkit.getWorlds().get(0);
private int minX, minY, minZ, maxX, maxY, maxZ;
private REntityServer entityServer = new REntityServer();
private List<REntity> entities = new ArrayList<>();
private Set<Location> locations = new HashSet<>();
private boolean wsOrAs;
private double maxBlastResistance;
public DesignEndStone(Region region) {
this.minX = region.getMinPointBuild().getX();
this.minY = region.getMinPointBuild().getY();
this.minZ = region.getMinPointBuild().getZ();
this.maxX = region.getMaxPointBuild().getX();
this.maxY = region.getMaxPointBuild().getY();
this.maxZ = region.getMaxPointBuild().getZ();
wsOrAs = region.getName().startsWith("ws") || region.getName().startsWith("as");
maxBlastResistance = wsOrAs ? 6.1 : 9.0;
entityServer.setCallback((player, rEntity, entityAction) -> {
if (entityAction != REntityServer.EntityAction.ATTACK) return;
Location location = new Location(WORLD, rEntity.getX(), rEntity.getY(), rEntity.getZ());
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
location.getBlock().breakNaturally();
calc();
}, 1);
});
}
public void calc() {
entities.forEach(REntity::die);
entities.clear();
locations.clear();
calc(minX, minY, minZ, maxX, maxY, minZ, 0, 0, 1, maxZ - minZ);
calc(minX, minY, maxZ, maxX, maxY, maxZ, 0, 0, -1, maxZ - minZ);
calc(minX, minY, minZ, minX, maxY, maxZ, 1, 0, 0, maxX - minX);
calc(maxX, minY, minZ, maxX, maxY, maxZ, -1, 0, 0, maxX - minX);
if (wsOrAs) {
calc(minX, minY, minZ, maxX, minY, maxZ, 0, 1, 0, maxY - minY + 1);
} else {
int airBlocks = 0;
double minAirBlocks = (maxX - minX) * (maxZ - minZ) * 0.1;
for (int x = minX; x < maxX; x++) {
for (int z = minZ; z < maxZ; z++) {
if (WORLD.getBlockAt(x, minY, z).getType().isAir()) {
airBlocks++;
if (airBlocks > minAirBlocks) break;
}
}
}
if (airBlocks > minAirBlocks) {
calc(minX, minY, minZ, maxX, minY, maxZ, 0, 1, 0, maxY - minY + 1);
}
}
calc(minX, maxY, minZ, maxX, maxY, maxZ, 0, -1, 0, maxY - minY + 1);
}
private void calc(int minX, int minY, int minZ, int maxX, int maxY, int maxZ, int dirX, int dirY, int dirZ, int steps) {
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
for (int step = 0; step < steps; step++) {
int cx = x + step * dirX;
int cy = y + step * dirY;
int cz = z + step * dirZ;
Material material = WORLD.getBlockAt(cx, cy, cz).getType();
if (material != Material.WATER && material != Material.LAVA && material.getBlastResistance() >= maxBlastResistance) {
Location location = new Location(WORLD, cx + 0.5, cy, cz + 0.5);
if (!locations.add(location)) break;
RFallingBlockEntity entity = new RFallingBlockEntity(entityServer, location, Material.RED_STAINED_GLASS);
entity.setNoGravity(true);
entity.setGlowing(true);
entities.add(entity);
break;
} else if (!material.isAir() && material != Material.WATER && material != Material.LAVA) {
break;
}
}
}
}
}
}
public void toggle(Player player) {
if (entityServer.getPlayers().contains(player)) {
entityServer.removePlayer(player);
BauSystem.MESSAGE.sendPrefixless("DESIGN_ENDSTONE_DISABLE", player, ChatMessageType.ACTION_BAR);
} else {
entityServer.addPlayer(player);
calc();
BauSystem.MESSAGE.sendPrefixless("DESIGN_ENDSTONE_ENABLE", player, ChatMessageType.ACTION_BAR);
}
}
public boolean removePlayer(Player player) {
entityServer.removePlayer(player);
return entityServer.getPlayers().isEmpty();
}
}
@@ -0,0 +1,97 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.design.endstone;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@Linked
public class DesignEndStoneCommand extends SWCommand implements Listener {
public DesignEndStoneCommand() {
super("designendstone");
}
private Map<Region, DesignEndStone> designEndStoneMap = new HashMap<>();
@Register(description = "DESIGN_ENDSTONE_COMMAND_HELP")
public void genericCommand(@Validator Player player) {
Region region = Region.getRegion(player.getLocation());
if (!region.hasType(RegionType.BUILD)) {
BauSystem.MESSAGE.send("DESIGN_ENDSTONE_REGION_ERROR", player);
return;
}
designEndStoneMap.computeIfAbsent(region, DesignEndStone::new).toggle(player);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
disableDesignEndStone(event.getPlayer());
}
@EventHandler
public void onBauMemberUpdate(BauMemberUpdateEvent event) {
event.getNewSpectator().forEach(this::disableDesignEndStone);
}
private void disableDesignEndStone(Player player) {
new HashSet<>(designEndStoneMap.entrySet()).forEach(regionDesignEndStoneEntry -> {
if (regionDesignEndStoneEntry.getValue().removePlayer(player)) {
designEndStoneMap.remove(regionDesignEndStoneEntry.getKey());
}
});
}
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
update(event.getBlock().getLocation());
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
BauSystem.runTaskLater(BauSystem.getInstance(), () -> {
update(event.getBlock().getLocation());
}, 1);
}
private void update(Location location) {
Region region = Region.getRegion(location);
DesignEndStone designEndStone = designEndStoneMap.get(region);
if (designEndStone == null) {
return;
}
designEndStone.calc();
}
}
@@ -0,0 +1,44 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.detonator;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@AllArgsConstructor
@RequiredArgsConstructor
@Getter
public enum Detoblock {
SWITCH(0, true, "DETONATOR_BUTTON_SWITCH"),
WOOD_BUTTON(30, "DETONATOR_BUTTON_WOOD_BUTTON"),
STONE_BUTTON(20, "DETONATOR_BUTTON_STONE_BUTTON"),
PRESSURE_PLATE(30, "DETONATOR_BUTTON_PRESSURE_PLATE"),
WEIGHTED_PRESSURE_PLATE(20, "DETONATOR_BUTTON_WEIGHTED-PRESSURE_PLATE"),
TRIPWIRE(30, "DETONATOR_BUTTON_TRIPWIRE"),
NOTEBLOCK(1, "DETONATOR_BUTTON_NOTEBLOCK"),
DAYLIGHTSENSOR(0, true, "DETONATOR_BUTTON_DAYLIGHTSENSOR"),
POWERABLE(0, true, "DETONATOR_BUTTON_POWERABLE"),
INVALID(-1, "DETONATOR_BUTTON_INVALID");
private final int time;
private boolean toggle;
private final String name;
}
@@ -0,0 +1,225 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.detonator;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.features.autostart.AutostartListener;
import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage;
import de.steamwar.bausystem.features.detonator.storage.ItemStorage;
import de.steamwar.entity.REntityServer;
import de.steamwar.entity.RFallingBlockEntity;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.*;
import org.bukkit.block.data.type.DaylightDetector;
import org.bukkit.block.data.type.Switch;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import java.util.*;
@UtilityClass
public class Detonator {
private static final Map<Player, REntityServer> ENTITIES_MAP = new HashMap<>();
private static final Vector HALF = new Vector(0.5, 0, 0.5);
public static boolean isDetonator(ItemStack itemStack) {
return ItemStorage.isDetonator(itemStack);
}
public static void showDetonator(Player p, List<Location> locs) {
if (ENTITIES_MAP.containsKey(p)) return;
REntityServer entities = new REntityServer();
entities.setCallback((player, rEntity, entityAction) -> {
Vector vector = new Vector(rEntity.getX(), rEntity.getY(), rEntity.getZ());
DetonatorListener.addLocationToDetonator(vector.toLocation(player.getWorld()).getBlock().getLocation(), player);
DetonatorListener.HAS_UPDATED.add(player);
});
entities.addPlayer(p);
ENTITIES_MAP.put(p, entities);
locs.forEach(location -> {
RFallingBlockEntity entity = new RFallingBlockEntity(entities, location.clone().add(HALF), Material.RED_STAINED_GLASS);
entity.setNoGravity(true);
});
}
public static void hideDetonator(Player p) {
ENTITIES_MAP.remove(p).close();
}
public static boolean hasActiveDetonatorShow(Player p) {
return ENTITIES_MAP.containsKey(p);
}
public static void activateDetonator(DetonatorStorage detonator) {
Player p = detonator.getPlayer();
if (Config.getInstance().get(p).getPlainValueOrDefault("detonator-autostart", false)) {
AutostartListener.instance.activate(p);
}
Map<Integer, Set<Block>> deactivate = new HashMap<>();
Set<Location> invalid = new HashSet<>();
for (Location location : detonator.getLocations()) {
Block block = location.getBlock();
Detoblock detoblock = getBlock(block);
if (detoblock == Detoblock.INVALID) {
invalid.add(location);
continue;
}
if (detoblock.isToggle()) {
setBlockPower(block, !getBlockPower(block));
updateButton(block, detoblock);
} else {
setBlockPower(block, true);
updateButton(block, detoblock);
deactivate.computeIfAbsent(detoblock.getTime(), integer -> new HashSet<>()).add(block);
}
}
int s = detonator.getLocations().size() - invalid.size();
if (s == 1) {
SWUtils.sendToActionbar(p, BauSystem.MESSAGE.parse("DETONATOR_POINT_ACT", p));
} else {
SWUtils.sendToActionbar(p, BauSystem.MESSAGE.parse("DETONATOR_POINTS_ACT", p, s));
}
if (!invalid.isEmpty()) {
int invalidPoints = invalid.size();
if (invalidPoints == 1) {
BauSystem.MESSAGE.send("DETONATOR_INVALID_POINT", p);
} else {
BauSystem.MESSAGE.send("DETONATOR_INVALID_POINTS", p, invalidPoints);
}
detonator.write();
}
deactivate.forEach((integer, blocks1) ->
Bukkit.getScheduler().scheduleSyncDelayedTask(BauSystem.getInstance(), () -> blocks1.forEach(block -> {
setBlockPower(block, false);
updateButton(block, getBlock(block));
}), integer));
}
public static void updateButton(Block block, Detoblock detoblock) {
if (block.getBlockData() instanceof Switch) {
Switch sw = (Switch) block.getBlockData();
FaceAttachable.AttachedFace face = sw.getAttachedFace();
if (face == FaceAttachable.AttachedFace.FLOOR) {
update(block.getRelative(BlockFace.DOWN));
} else if (face == FaceAttachable.AttachedFace.CEILING) {
update(block.getRelative(BlockFace.UP));
} else {
update(block.getRelative(sw.getFacing().getOppositeFace()));
}
} else if (detoblock == Detoblock.TRIPWIRE) {
update(block);
} else if (detoblock == Detoblock.PRESSURE_PLATE || detoblock == Detoblock.WEIGHTED_PRESSURE_PLATE) {
update(block.getRelative(BlockFace.DOWN));
}
}
private static void update(Block block) {
BlockData data = block.getBlockData();
block.setType(Material.BARRIER, true);
block.setBlockData(data, true);
}
private static void setBlockPower(Block block, boolean state) {
BlockData data = block.getBlockData();
if (data instanceof Powerable) {
Powerable pow = (Powerable) data;
pow.setPowered(state);
}
if (data instanceof Openable) {
Openable openable = (Openable) data;
openable.setOpen(state);
}
if (data instanceof DaylightDetector) {
DaylightDetector detector = (DaylightDetector) data;
detector.setInverted(state);
}
if (data instanceof AnaloguePowerable) {
AnaloguePowerable powerable = (AnaloguePowerable) data;
if (block.getType() == Material.REDSTONE_WIRE) {
powerable.setPower(state ? 15 : 0);
} else {
powerable.setPower(state ? 1 : 0);
}
}
block.setBlockData(data);
}
private static boolean getBlockPower(Block block) {
BlockData data = block.getBlockData();
if (data instanceof Powerable) {
Powerable pow = (Powerable) data;
return pow.isPowered();
}
if (data instanceof DaylightDetector) {
DaylightDetector detector = (DaylightDetector) data;
return detector.isInverted();
}
if (data instanceof AnaloguePowerable) {
AnaloguePowerable powerable = (AnaloguePowerable) data;
return powerable.getPower() > 0;
}
return false;
}
static Detoblock getBlock(Block block) {
switch (block.getType()) {
case LEVER:
return Detoblock.SWITCH;
case TRIPWIRE:
return Detoblock.TRIPWIRE;
case NOTE_BLOCK:
return Detoblock.NOTEBLOCK;
case DAYLIGHT_DETECTOR:
return Detoblock.DAYLIGHTSENSOR;
case HEAVY_WEIGHTED_PRESSURE_PLATE:
case LIGHT_WEIGHTED_PRESSURE_PLATE:
return Detoblock.WEIGHTED_PRESSURE_PLATE;
default:
if (block.getType().name().contains("STONE_BUTTON")) {
return Detoblock.STONE_BUTTON;
} else if (block.getType().name().contains("BUTTON")) {
return Detoblock.WOOD_BUTTON;
} else if (block.getType().name().contains("PRESSURE_PLATE")) {
return Detoblock.PRESSURE_PLATE;
} else if (block.getBlockData() instanceof Powerable) {
return Detoblock.POWERABLE;
} else {
return Detoblock.INVALID;
}
}
}
}
@@ -0,0 +1,52 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.detonator;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class DetonatorBauGuiItem extends BauGuiItem {
public DetonatorBauGuiItem() {
super(8);
}
@Override
public ItemStack getItem(Player player) {
return DetonatorCommand.getWAND(player);
}
@Override
public boolean click(ClickType click, Player p) {
p.closeInventory();
p.performCommand("detonator wand");
return false;
}
@Override
public Permission permission() {
return Permission.BUILD;
}
}
@@ -0,0 +1,87 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.detonator;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage;
import de.steamwar.bausystem.features.detonator.storage.ItemStorage;
import de.steamwar.bausystem.utils.ItemUtils;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays;
@Linked
public class DetonatorCommand extends SWCommand {
public static ItemStack getWAND(Player player) {
ItemStack wand = new ItemStack(Material.BLAZE_ROD);
ItemStorage.setDetonator(wand);
ItemMeta meta = wand.getItemMeta();
meta.setDisplayName(BauSystem.MESSAGE.parse("DETONATOR_WAND_NAME", player));
meta.setLore(Arrays.asList(BauSystem.MESSAGE.parse("DETONATOR_WAND_LORE_1", player),
BauSystem.MESSAGE.parse("DETONATOR_WAND_LORE_2", player),
BauSystem.MESSAGE.parse("DETONATOR_WAND_LORE_3", player)));
meta.setCustomModelData(3);
wand.setItemMeta(meta);
ItemUtils.setItem(wand, "detonator");
return wand;
}
public DetonatorCommand() {
super("detonator", "dt");
}
@Register(value = "wand", description = "DETONATOR_HELP_WAND")
public void giveWand(@Validator Player p) {
SWUtils.giveItemToPlayer(p, getWAND(p));
}
@Register(value = "click", description = "DETONATOR_HELP_CLICK")
public void clickDetonator(@Validator Player p) {
Detonator.activateDetonator(new ItemStorage(p));
}
@Register(value = "clear", description = "DETONATOR_HELP_CLEAR")
public void clearDetonator(Player p) {
DetonatorStorage storage = new ItemStorage(p);
storage.clear();
storage.write();
}
@Register(value = "autostart", description = "DETONATOR_HELP_AUTOSTART")
public void toggleAutostartTimer(Player p) {
boolean current = Config.getInstance().get(p).getPlainValueOrDefault("detonator-autostart", false);
Config.getInstance().get(p).put("detonator-autostart", !current);
if (!current) {
BauSystem.MESSAGE.send("DETONATOR_AUTOSTART_ENABLE", p);
} else {
BauSystem.MESSAGE.send("DETONATOR_AUTOSTART_DISABLE", p);
}
}
}
@@ -0,0 +1,129 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.detonator;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage;
import de.steamwar.bausystem.features.detonator.storage.ItemStorage;
import de.steamwar.linkage.Linked;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
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.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import java.util.HashSet;
import java.util.Set;
@Linked
public class DetonatorListener implements Listener {
static final Set<Player> HAS_UPDATED = new HashSet<>();
static void addLocationToDetonator(Location location, Player p) {
Detoblock detoblock = Detonator.getBlock(location.getBlock());
if (detoblock == Detoblock.INVALID) {
SWUtils.sendToActionbar(p, BauSystem.MESSAGE.parse("DETONATOR_INVALID_BLOCK", p));
return;
}
DetonatorStorage detonator = new ItemStorage(p);
if (!p.isSneaking()) {
detonator.clear();
}
if (detonator.getLocations().contains(location)) {
detonator.removeLocation(location);
SWUtils.sendToActionbar(p, BauSystem.MESSAGE.parse("DETONATOR_LOC_REMOVE", p, BauSystem.MESSAGE.parse(detoblock.getName(), p)));
} else {
detonator.addLocation(location);
SWUtils.sendToActionbar(p, BauSystem.MESSAGE.parse("DETONATOR_LOC_ADD", p, BauSystem.MESSAGE.parse(detoblock.getName(), p)));
}
detonator.write();
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
Player p = event.getPlayer();
if (Detonator.isDetonator(p.getInventory().getItemInMainHand())) {
event.setCancelled(true);
addLocationToDetonator(event.getBlock().getLocation(), p);
HAS_UPDATED.add(event.getPlayer());
}
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
if (!Detonator.isDetonator(event.getItem())) {
return;
}
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
event.setCancelled(true);
DetonatorStorage detonator = new ItemStorage(event.getPlayer());
Detonator.activateDetonator(detonator);
HAS_UPDATED.add(event.getPlayer());
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent event) {
if (!Permission.BUILD.hasPermission(event.getPlayer()) ||!Detonator.isDetonator(event.getPlayer().getInventory().getItemInMainHand())) {
if (Detonator.hasActiveDetonatorShow(event.getPlayer())) {
Detonator.hideDetonator(event.getPlayer());
}
} else {
if (!Detonator.hasActiveDetonatorShow(event.getPlayer())) {
Detonator.showDetonator(event.getPlayer(), new ItemStorage(event.getPlayer()).getLocations());
}
}
if (HAS_UPDATED.contains(event.getPlayer())) {
HAS_UPDATED.remove(event.getPlayer());
if (Detonator.hasActiveDetonatorShow(event.getPlayer())) {
Detonator.hideDetonator(event.getPlayer());
Detonator.showDetonator(event.getPlayer(), new ItemStorage(event.getPlayer()).getLocations());
}
}
}
@EventHandler
public void onPlayerItemHeld(PlayerItemHeldEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
if (Detonator.isDetonator(event.getPlayer().getInventory().getItemInMainHand())) {
HAS_UPDATED.add(event.getPlayer());
}
}
@EventHandler
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
if (Detonator.isDetonator(event.getMainHandItem()) || Detonator.isDetonator(event.getOffHandItem())) {
HAS_UPDATED.add(event.getPlayer());
}
}
}
@@ -0,0 +1,40 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.detonator.storage;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.List;
public interface DetonatorStorage {
void addLocation(Location location);
List<Location> getLocations();
void removeLocation(Location location);
void clear();
void write();
Player getPlayer();
}
@@ -0,0 +1,191 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.detonator.storage;
import de.steamwar.bausystem.SWUtils;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.function.IntFunction;
public class ItemStorage implements DetonatorStorage {
private static final NamespacedKey DETO_KEY = SWUtils.getNamespaceKey("deto");
private static final NamespacedKey SLOT_KEY = SWUtils.getNamespaceKey("slots");
private static final IntFunction<NamespacedKey> DETOLOCATION_BUILDER = integer -> SWUtils.getNamespaceKey("deto_loc_" + integer);
@Getter
private final Player p;
private final List<Location> locs;
private int slot;
private ItemStack itemStack;
public ItemStorage(Player p) {
this.p = p;
if (isDetonator(p.getInventory().getItemInMainHand())) {
slot = p.getInventory().getHeldItemSlot();
itemStack = p.getInventory().getItemInMainHand();
} else {
ItemStack[] inv = p.getInventory().getContents();
for (int i = 0; i < inv.length; i++) {
if (isDetonator(inv[i])) {
slot = i;
itemStack = inv[i];
break;
}
}
}
locs = read();
}
public static ItemStack setDetonator(ItemStack item) {
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return item;
}
PersistentDataContainer dataContainer = meta.getPersistentDataContainer();
dataContainer.set(DETO_KEY, PersistentDataType.BYTE, (byte) 1);
dataContainer.set(SLOT_KEY, PersistentDataType.INTEGER, 0);
item.setItemMeta(meta);
return item;
}
public static boolean isDetonator(ItemStack item) {
if (item == null) {
return false;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return false;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
return container.has(DETO_KEY, PersistentDataType.BYTE) && container.has(SLOT_KEY, PersistentDataType.INTEGER);
}
@Override
public void addLocation(Location location) {
locs.add(location);
}
@Override
public List<Location> getLocations() {
return locs;
}
@Override
public void removeLocation(Location location) {
locs.remove(location);
}
@Override
public void clear() {
internalClear();
locs.clear();
apply();
}
@Override
public void write() {
write(locs);
}
@Override
public Player getPlayer() {
return p;
}
private List<Location> read() {
List<Location> tempLocs = new LinkedList<>();
if (itemStack == null) {
return Collections.emptyList();
}
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return Collections.emptyList();
}
PersistentDataContainer container = meta.getPersistentDataContainer();
int length = length();
for (int i = 0; i < length; i++) {
NamespacedKey key = DETOLOCATION_BUILDER.apply(i);
int[] loc = container.get(key, PersistentDataType.INTEGER_ARRAY);
tempLocs.add(new Location(p.getWorld(), loc[0], loc[1], loc[2]));
}
return tempLocs;
}
private int length() {
ItemMeta meta = itemStack.getItemMeta();
PersistentDataContainer container = meta.getPersistentDataContainer();
return container.get(SLOT_KEY, PersistentDataType.INTEGER);
}
private void internalClear() {
ItemMeta meta = itemStack.getItemMeta();
PersistentDataContainer container = meta.getPersistentDataContainer();
int length = length();
for (int i = 0; i < length; i++) {
NamespacedKey key = DETOLOCATION_BUILDER.apply(i);
container.remove(key);
}
container.set(SLOT_KEY, PersistentDataType.INTEGER, 0);
itemStack.setItemMeta(meta);
}
private void write(List<Location> locs) {
internalClear();
ItemMeta meta = itemStack.getItemMeta();
PersistentDataContainer container = meta.getPersistentDataContainer();
int i = 0;
for (Location loc : locs) {
NamespacedKey key = DETOLOCATION_BUILDER.apply(i);
int[] slotLoc = new int[]{
loc.getBlockX(),
loc.getBlockY(),
loc.getBlockZ()
};
container.set(key, PersistentDataType.INTEGER_ARRAY, slotLoc);
i++;
}
container.set(SLOT_KEY, PersistentDataType.INTEGER, i);
itemStack.setItemMeta(meta);
apply();
}
private void apply() {
p.getInventory().setItem(slot, itemStack);
}
}
@@ -0,0 +1,120 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.gui;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.gui.editor.BauGuiMapping;
import de.steamwar.bausystem.linkage.LinkageUtils;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWInventory;
import lombok.experimental.UtilityClass;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.*;
@UtilityClass
public class BauGUI {
private static final Map<Integer, BauGuiItem> ITEMS = new HashMap<>();
public static Map<Integer, BauGuiItem> getITEMS() {
if (ITEMS.isEmpty()) LinkageUtils.linkGUIItems();
return ITEMS;
}
private static final Set<Player> OPEN_INVS = new HashSet<>();
private static boolean updating = false;
public static ItemStack getGUI_ITEM(Player p) {
ItemStack GUI_ITEM = new ItemStack(Material.NETHER_STAR);
ItemMeta meta = GUI_ITEM.getItemMeta();
meta.setDisplayName(ChatColor.YELLOW + BauSystem.MESSAGE.parse("GUI_NAME", p));
GUI_ITEM.setItemMeta(meta);
return GUI_ITEM;
}
public static void addItem(BauGuiItem item) {
if (ITEMS.containsKey(item.getId())) {
throw new IllegalArgumentException("Duplicate Id Key: " + item.getId() + ". From Classes: " + ITEMS.get(item.getId()).getClass().getName() + " and " + item.getClass().getName());
}
ITEMS.put(item.getId(), item);
}
public static void openBauGui(Player p) {
if (!updating) {
OPEN_INVS.add(p);
}
BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p);
SWInventory inv = new SWInventory(p, mapping.getSize(), BauSystem.MESSAGE.parse("GUI_NAME", p));
getITEMS().values().forEach(item -> {
if (!mapping.isShown(item.getId())) {
return;
}
inv.setItem(mapping.getSlot(item.getId()), permissionLore(item.getItem(p), item.permission(), p), clickType -> {
if (item.permission().hasPermission(p)) {
if (item.click(clickType, p)) {
update();
}
} else {
p.closeInventory();
BauSystem.MESSAGE.send("NO_PERMISSION", p);
}
});
});
inv.addCloseCallback(clickType -> {
if (!updating) {
OPEN_INVS.remove(p);
}
});
inv.open();
}
public static void update() {
updating = true;
OPEN_INVS.forEach(BauGUI::openBauGui);
updating = false;
}
public static void giveItem(Player p) {
SWUtils.giveItemToPlayer(p, getGUI_ITEM(p));
}
private static ItemStack permissionLore(ItemStack itemStack, Permission permission, Player p) {
ItemMeta meta = itemStack.getItemMeta();
if (!permission.hasPermission(p)) {
List<String> lore = meta.getLore();
if (lore == null) {
lore = Collections.singletonList(BauSystem.MESSAGE.parse("NO_PERMISSION", p));
} else {
lore.add(BauSystem.MESSAGE.parse("NO_PERMISSION", p));
}
meta.setLore(lore);
}
itemStack.setItemMeta(meta);
return itemStack;
}
}
@@ -0,0 +1,49 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.gui;
import de.steamwar.bausystem.features.gui.editor.BauGuiEditor;
import de.steamwar.command.SWCommand;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class BauGUICommand extends SWCommand {
public BauGUICommand() {
super("gui", "baugui", "g");
}
@Register(help = true)
public void genericHelp(Player p, String... args) {
BauGUI.openBauGui(p);
}
@Register("item")
public void giveItem(Player p) {
BauGUI.giveItem(p);
}
@Register("editor")
public void openEditor(Player p) {
BauGuiEditor.openGuiEditor(p, new SWItem().getItemStack());
}
}
@@ -0,0 +1,60 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.gui;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
@Linked
public class BauGuiBauGuiItem extends BauGuiItem {
public BauGuiBauGuiItem() {
super(14);
}
@Override
public ItemStack getItem(Player player) {
return new SWItem(Material.NETHER_STAR, ChatColor.YELLOW + BauSystem.MESSAGE.parse("GUI_NAME", player),
Arrays.asList(BauSystem.MESSAGE.parse("GUI_ITEM_LORE1", player), BauSystem.MESSAGE.parse("GUI_ITEM_LORE2", player)), false, clickType -> {
}).getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
p.closeInventory();
BauGUI.giveItem(p);
return false;
}
@Override
public Permission permission() {
return Permission.MEMBER;
}
}
@@ -0,0 +1,43 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.gui;
import de.steamwar.linkage.Linked;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
@Linked
public class BauGuiListener implements Listener {
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getItem() == null) {
return;
}
if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
if (event.getItem().isSimilar(BauGUI.getGUI_ITEM(event.getPlayer()))) {
BauGUI.openBauGui(event.getPlayer());
}
}
}
}
@@ -0,0 +1,233 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.gui.editor;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.gui.BauGUI;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.linkage.Linked;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import java.util.*;
@Linked
public class BauGuiEditor implements Listener {
@Getter
private static final List<Player> open_Edits = new ArrayList<>();
public static void openGuiEditor(Player p, ItemStack cursor) {
BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p);
Inventory inv = Bukkit.createInventory(null, mapping.getSize() + 9, BauSystem.MESSAGE.parse("GUI_EDITOR_TITLE", p));
for (Map.Entry<Integer, Integer> e : mapping.getMapping().entrySet()) {
if (e.getValue() >= 0) {
if (e.getValue() < mapping.getSize()) {
inv.setItem(e.getValue(), addId(BauGUI.getITEMS().get(e.getKey()).getItem(p), e.getKey()));
}
}
}
for (int j = mapping.getSize(); j < mapping.getSize() + 9; j++) {
inv.setItem(j, new SWItem(Material.WHITE_STAINED_GLASS_PANE, "").getItemStack());
}
inv.setItem(mapping.getSize() + 3, new SWItem(mapping.getSize() == 9 * 5 ? Material.GRAY_STAINED_GLASS_PANE : Material.LIME_STAINED_GLASS_PANE, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_ROW_P", p)).getItemStack());
inv.setItem(mapping.getSize() + 2, new SWItem(mapping.getSize() == 9 ? Material.GRAY_STAINED_GLASS_PANE : Material.RED_STAINED_GLASS_PANE, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_ROW_M", p)).getItemStack());
inv.setItem(mapping.getSize() + 5, new SWItem(Material.BARRIER, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_TRASH", p), Arrays.asList(BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_TRASH_LORE", p)), false, clickType -> {
}).getItemStack());
inv.setItem(mapping.getSize() + 6, new SWItem(Material.SCUTE, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_MORE", p)).getItemStack());
inv.setItem(mapping.getSize() + 8, new SWItem(Material.ARROW, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_CLOSE", p)).getItemStack());
p.openInventory(inv);
p.getOpenInventory().setCursor(cursor == null ? new SWItem().getItemStack() : cursor);
open_Edits.add(p);
BauGuiMapping.startWatchdog();
}
private static ItemStack addId(ItemStack itemStack, int id) {
ItemMeta meta = itemStack.getItemMeta();
meta.getPersistentDataContainer().set(new NamespacedKey(BauSystem.getInstance(), "gui-item-id"), PersistentDataType.INTEGER, id);
itemStack.setItemMeta(meta);
return itemStack;
}
private static int getId(ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
return meta.getPersistentDataContainer().get(new NamespacedKey(BauSystem.getInstance(), "gui-item-id"), PersistentDataType.INTEGER);
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (!open_Edits.contains(event.getWhoClicked())) {
return;
}
ItemStack i = event.getCurrentItem();
Player p = (Player) event.getWhoClicked();
BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p);
if (event.getClickedInventory() == p.getInventory()) {
event.setCancelled(true);
}
if (event.getHotbarButton() != -1) {
event.setCancelled(true);
}
if (i == null) {
return;
}
if (event.getSlot() - mapping.getSize() >= 0) {
event.setCancelled(true);
}
switch (event.getSlot() - mapping.getSize()) {
case 2:
if (mapping.getSize() == 9) {
return;
}
saveMapping(p);
mapping.setSize(mapping.getSize() - 9);
openGuiEditor(p, event.getCursor());
break;
case 3:
if (mapping.getSize() == 9 * 5) {
return;
}
saveMapping(p);
mapping.setSize(mapping.getSize() + 9);
openGuiEditor(p, event.getCursor());
break;
case 5:
event.getView().setCursor(new SWItem().getItemStack());
break;
case 6:
saveMapping(p);
List<SWListInv.SWListEntry<BauGuiItem>> items = new ArrayList<>();
for (BauGuiItem item : BauGUI.getITEMS().values()) {
if (mapping.isShown(item.getId())) {
continue;
}
SWItem ip = new SWItem();
ip.setItemStack(item.getItem(p));
items.add(new SWListInv.SWListEntry(ip, item));
}
if (items.isEmpty()) {
return;
}
SWListInv<BauGuiItem> inv = new SWListInv<>(p, BauSystem.MESSAGE.parse("GUI_EDITOR_TITLE_MORE", p), items, (clickType, item) -> {
openGuiEditor(p, addId(item.getItem(p), item.getId()));
});
inv.open();
break;
case 8:
saveMapping(p);
BauGUI.openBauGui(p);
break;
default:
}
}
@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
if (open_Edits.contains(event.getEntity())) {
event.setCancelled(true);
}
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if (open_Edits.contains(event.getPlayer())) {
event.setCancelled(true);
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
open_Edits.remove(event.getPlayer());
}
@EventHandler
public void onInventoryDrag(InventoryDragEvent event) {
if (open_Edits.contains(event.getWhoClicked())) {
if (event.getRawSlots().stream().anyMatch(integer -> event.getView().getInventory(integer) == event.getWhoClicked().getInventory())) {
event.setCancelled(true);
}
}
}
private void saveMapping(Player p) {
saveMapping(p.getOpenInventory(), BauGuiMapping.getGuiMapping(p));
}
private void saveMapping(InventoryView view, BauGuiMapping mapping) {
if (mapping.isSaved()) {
return;
}
HashMap<Integer, Integer> newMapping = new HashMap<>();
for (int i = 0; i < view.getTopInventory().getContents().length; i++) {
ItemStack itemStack = view.getTopInventory().getContents()[i];
if (itemStack == null || itemStack.getType() == Material.AIR || i >= mapping.getSize()) continue;
newMapping.put(getId(itemStack), i);
}
for (Map.Entry<Integer, BauGuiItem> e : BauGUI.getITEMS().entrySet()) {
if (!newMapping.containsKey(e.getKey())) {
newMapping.put(e.getKey(), -1);
}
}
mapping.setMapping(newMapping);
mapping.save();
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (!open_Edits.contains(event.getPlayer())) {
return;
}
Player p = (Player) event.getPlayer();
saveMapping(event.getView(), BauGuiMapping.getGuiMapping(p));
open_Edits.remove(p);
}
}
@@ -0,0 +1,54 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.gui.editor;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class BauGuiEditorGuiItem extends BauGuiItem {
public BauGuiEditorGuiItem() {
super(25);
}
@Override
public Permission permission() {
return Permission.MEMBER;
}
@Override
public ItemStack getItem(Player player) {
return new SWItem(Material.IRON_PICKAXE, BauSystem.MESSAGE.parse("GUI_EDITOR_ITEM_NAME", player)).getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
p.performCommand("gui editor");
return false;
}
}
@@ -0,0 +1,138 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.gui.editor;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.features.gui.BauGUI;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import yapion.hierarchy.types.YAPIONObject;
import java.util.*;
public class BauGuiMapping {
private static final HashMap<UUID, BauGuiMapping> fromUUID = new HashMap<>();
private static final List<BauGuiMapping> mappings = new ArrayList<>();
private static BukkitTask task;
public static void startWatchdog() {
if (task == null) {
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), bukkitTask -> {
mappings.forEach(BauGuiMapping::tick);
if (BauGuiEditor.getOpen_Edits().isEmpty()) {
bukkitTask.cancel();
task = null;
}
}, 1, 1);
}
}
@Getter
private final YAPIONObject object;
@Getter
private final Player owner;
@Getter
@Setter
private boolean saved;
protected BauGuiMapping(YAPIONObject object, Player p) {
this.object = object;
this.owner = p;
fromUUID.put(p.getUniqueId(), this);
mappings.add(this);
}
public static BauGuiMapping getGuiMapping(Player p) {
BauGuiMapping mapping = fromUUID.get(p.getUniqueId());
if (mapping == null) {
YAPIONObject yapionObject = Config.getInstance().get(p);
mapping = new BauGuiMapping(yapionObject.getObject("baugui"), p);
}
return mapping;
}
public boolean isShown(int id) {
return object.getPlainValueOrDefault(Integer.toString(id), -1) >= 0;
}
public int getSlot(int id) {
return object.getPlainValue(Integer.toString(id));
}
public Map<Integer, Integer> getMapping() {
return internalReadMap();
}
public void setMapping(Map<Integer, Integer> mapping) {
internalWriteMap(mapping);
}
public int getSize() {
return object.getPlainValueOrDefault("size", 45);
}
public void setSize(int size) {
if (size % 9 != 0) {
return;
}
object.add("size", size);
}
private void tick() {
this.saved = false;
}
public void save() {
if (saved) return;
YAPIONObject config = Config.getInstance().get(owner);
config.add("baugui", object);
Config.getInstance().save(owner);
saved = true;
}
private Map<Integer, Integer> internalReadMap() {
Map<Integer, Integer> map = new HashMap<>();
for (Map.Entry<Integer, BauGuiItem> e : BauGUI.getITEMS().entrySet()) {
Integer value = object.getPlainValueOrDefault(e.getKey().toString(), -1);
if (value == -1) continue;
map.put(e.getKey(), value);
}
return map;
}
private void internalWriteMap(Map<Integer, Integer> map) {
for (Map.Entry<Integer, BauGuiItem> e : BauGUI.getITEMS().entrySet()) {
object.remove(e.getKey().toString());
Integer value = map.getOrDefault(e.getKey(), -1);
if (value == -1) continue;
object.add(e.getKey().toString(), value);
}
}
}
@@ -0,0 +1,93 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.hotbar;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.core.Core;
import lombok.experimental.UtilityClass;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONObject;
import yapion.hierarchy.types.YAPIONValue;
import yapion.parser.YAPIONParser;
import yapion.serializing.YAPIONDeserializer;
import yapion.serializing.YAPIONSerializer;
import java.util.HashSet;
import java.util.Set;
@UtilityClass
public class DefaultHotbar {
private final YAPIONArray DEFAULT_HOTBAR = YAPIONParser.parse("{[{@type(org.bukkit.inventory.ItemStack)v(2230)type(WOODEN_AXE)meta{@type(org.bukkit.inventory.meta.ItemMeta)meta-type(UNSPECIFIC)display-name(WorldEdit Wand)lore{@type(java.util.ArrayList)values[Left click: select pos #1,Right click: select pos #2]}}},{@type(org.bukkit.inventory.ItemStack)v(2230)type(COMPASS)meta{@type(org.bukkit.inventory.meta.ItemMeta)meta-type(UNSPECIFIC)display-name(Navigation Wand)lore{@type(java.util.ArrayList)values[Left click: jump to location,Right click: pass through walls]}}},null,null,null,null,null,null,{@type(org.bukkit.inventory.ItemStack)v(2230)type(NETHER_STAR)meta{@type(org.bukkit.inventory.meta.ItemMeta)meta-type(UNSPECIFIC)display-name(§eBau GUI)}},null,null,{@type(org.bukkit.inventory.ItemStack)v(3117)type(ELYTRA)},null]}").getArray("");
public void updateHotbar(Player p) {
ItemStack[] hotbar = new ItemStack[13];
System.arraycopy(p.getInventory().getContents(), 0, hotbar, 0, 9);
System.arraycopy(p.getInventory().getArmorContents(), 0, hotbar, 9, 4);
YAPIONArray yapionArray = new YAPIONArray();
for (ItemStack itemStack : hotbar) {
if (itemStack != null) {
yapionArray.add(YAPIONSerializer.serialize(itemStack));
} else {
yapionArray.add(new YAPIONValue<>(null));
}
}
Config.getInstance().get(p).add("hotbar-" + Core.getVersion(), yapionArray);
Config.getInstance().save(p);
}
public void setHotbar(Player p) {
ItemStack[] hotbar = getItems(p);
ItemStack[] inv = p.getInventory().getContents();
ItemStack[] armor = p.getInventory().getArmorContents();
System.arraycopy(hotbar, 0, inv, 0, 9);
System.arraycopy(hotbar, 9, armor, 0, 4);
p.getInventory().setContents(inv);
p.getInventory().setArmorContents(armor);
}
public ItemStack[] getItems(Player p) {
Config.getInstance().get(p).remap("hotbar", "hotbar-19");
YAPIONArray yapionArray = Config.getInstance().get(p).getYAPIONArrayOrSetDefault("hotbar-" + Core.getVersion(), defaultHotbar());
ItemStack[] hotbar = new ItemStack[13];
Set<Integer> invalid = new HashSet<>();
yapionArray.forEach((integer, yapionAnyType) -> {
if (yapionAnyType instanceof YAPIONValue) {
hotbar[integer] = null;
} else {
try {
hotbar[integer] = YAPIONDeserializer.deserialize((YAPIONObject) yapionAnyType);
} catch (Exception e) {
invalid.add(integer);
hotbar[integer] = null;
}
}
});
invalid.forEach(i -> yapionArray.set(i, new YAPIONValue<>(null)));
if (!invalid.isEmpty()) Config.getInstance().save(p);
return hotbar;
}
public YAPIONArray defaultHotbar() {
return DEFAULT_HOTBAR;
}
}
@@ -0,0 +1,60 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.hotbar;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.command.SWCommand;
import de.steamwar.inventory.SWInventory;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@Linked
public class HotbarCommand extends SWCommand {
public HotbarCommand() {
super("hotbar", "hb");
addDefaultHelpMessage("HOTBAR_HELP_GENERIC");
}
@Register(value = "load", description = "HOTBAR_HELP_LOAD")
public void loadHotbar(@Validator Player p) {
DefaultHotbar.setHotbar(p);
BauSystem.MESSAGE.send("HOTBAR_LOADED", p);
}
@Register(value = "save", description = "HOTBAR_HELP_SAVE")
public void saveHotbar(Player p) {
DefaultHotbar.updateHotbar(p);
BauSystem.MESSAGE.send("HOTBAR_SAVED", p);
}
@Register(value = "show", description = "HOTBAR_HELP_SHOW")
public void showHotbar(Player p) {
SWInventory inv = new SWInventory(p, 18, BauSystem.MESSAGE.parse("HOTBAR_INVENTORY", p));
ItemStack[] hotbar = DefaultHotbar.getItems(p);
for (int i = 0; i < hotbar.length; i++) {
if (hotbar[i] == null) continue;
inv.setItem(i, hotbar[i], clickType -> {
});
}
inv.open();
}
}
@@ -0,0 +1,48 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.hotbar;
import de.steamwar.bausystem.Permission;
import de.steamwar.linkage.Linked;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
@Linked
public class HotbarListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
if (allNull(event.getPlayer().getInventory().getContents()) && allNull(event.getPlayer().getInventory().getArmorContents())) {
DefaultHotbar.setHotbar(event.getPlayer());
}
}
private boolean allNull(Object[] arr) {
for (Object o : arr) {
if (o != null) {
return false;
}
}
return true;
}
}
@@ -0,0 +1,42 @@
package de.steamwar.bausystem.features.inventoryfiller;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import java.util.Collections;
@Linked
public class InventoryFillBauGuiItem extends BauGuiItem {
public InventoryFillBauGuiItem() {
super(34);
}
@LinkedInstance
public InventoryFillerCommand command;
@Override
public Permission permission() {
return Permission.MEMBER;
}
@Override
public ItemStack getItem(Player player) {
String loreKey = Config.getInstance().get(player).getPlainValueOrDefault("inventoryfill", false) ? "OTHER_ITEMS_INVENTORY_FILL_LORE_ACTIVE" : "OTHER_ITEMS_INVENTORY_FILL_LORE_INACTIVE";
return new SWItem(Material.HOPPER, BauSystem.MESSAGE.parse("OTHER_ITEMS_INVENTORY_FILL_NAME", player), Collections.singletonList(BauSystem.MESSAGE.parse(loreKey, player)), false, clickType -> {}).getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
command.toggle(p);
return false;
}
}
@@ -0,0 +1,124 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 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.features.inventoryfiller;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Container;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@Linked
public class InventoryFiller implements Listener {
@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("inventoryfill", false)) return;
if (!event.getPlayer().isSneaking()) return;
Block block = event.getPlayer().getTargetBlockExact(5);
if (block == null) return;
if (!(block.getState() instanceof Container)) return;
Container container = (Container) block.getState();
Inventory inventory = container.getInventory();
ItemStack itemStack = event.getItemDrop().getItemStack().clone();
itemStack.setAmount(itemStack.getType().getMaxStackSize());
event.setCancelled(true);
for (int i = 0; i < inventory.getSize(); i++) {
inventory.setItem(i, itemStack);
}
BauSystem.MESSAGE.sendPrefixless("CHESTFILLER_FILLED", event.getPlayer(), ChatMessageType.ACTION_BAR);
}
/**
* For MacOS user: https://www.curseforge.com/minecraft/mc-mods/shift-scroll-fix
*/
@EventHandler
public void onPlayerItemHeld(PlayerItemHeldEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("inventoryfill", false)) return;
if (!event.getPlayer().isSneaking()) return;
ItemStack itemStack = event.getPlayer().getInventory().getItemInMainHand();
if (itemStack.getType() == Material.AIR) return;
Block block = event.getPlayer().getTargetBlockExact(5);
if (block == null) return;
if (!(block.getState() instanceof Container)) return;
Container container = (Container) block.getState();
Inventory inventory = container.getInventory();
event.setCancelled(true);
int upperBound = event.getPreviousSlot() + 4;
if (upperBound > 8) upperBound -= 8;
int lowerBound = event.getPreviousSlot() - 5;
if (lowerBound < 0) lowerBound += 8;
int direction = 0;
if (event.getPreviousSlot() != 0 && (event.getNewSlot() > event.getPreviousSlot() || (upperBound < event.getPreviousSlot() && event.getNewSlot() < upperBound))) {
direction = 1;
} else if (event.getPreviousSlot() == 0 && event.getNewSlot() > upperBound) {
direction = -1;
} else if (event.getNewSlot() < event.getPreviousSlot() || (lowerBound > event.getPreviousSlot() && event.getNewSlot() >= lowerBound)) {
direction = -1;
} else if (event.getPreviousSlot() == 0) {
direction = 1;
}
int count = 0;
int emptySlot = -1;
int filledSlot = -1;
for (int i = 0; i < inventory.getSize(); i++) {
ItemStack current = inventory.getItem(i);
if (current == null) {
if (emptySlot == -1) emptySlot = i;
continue;
}
if (current.getType() == itemStack.getType()) {
count += current.getAmount();
int emptyAmount = itemStack.getType().getMaxStackSize() - current.getAmount();
if (direction == -1) filledSlot = Math.max(filledSlot, i);
if (emptyAmount > 0) filledSlot = Math.min(i, filledSlot);
if (emptyAmount > 0 && filledSlot == -1) filledSlot = i;
}
}
int slotToUse = direction == -1 ? filledSlot : (filledSlot == -1 ? emptySlot : filledSlot);
if (slotToUse == -1) return;
ItemStack current = inventory.getItem(slotToUse);
if (current == null) {
ItemStack now = itemStack.clone();
now.setAmount(1);
inventory.setItem(slotToUse, now);
} else {
current.setAmount(current.getAmount() + direction);
}
BauSystem.MESSAGE.sendPrefixless("CHESTFILLER_COUNT", event.getPlayer(), ChatMessageType.ACTION_BAR, itemStack.getType(), count + direction);
}
}
@@ -0,0 +1,28 @@
package de.steamwar.bausystem.features.inventoryfiller;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class InventoryFillerCommand extends SWCommand {
public InventoryFillerCommand() {
super("inventoryfill");
}
@Register(description = {"INVENTORY_FILL_HELP", "INVENTORY_FILL_INFO"})
public void toggle(Player player) {
boolean inventoryFill = Config.getInstance().get(player).getPlainValueOrDefault("inventoryfill", false);
Config.getInstance().get(player).put("inventoryfill", !inventoryFill);
if (!inventoryFill) {
SWUtils.sendToActionbar(player, BauSystem.MESSAGE.parse("INVENTORY_FILL_ENABLE", player));
BauSystem.MESSAGE.send("INVENTORY_FILL_INFO", player);
}else {
SWUtils.sendToActionbar(player, BauSystem.MESSAGE.parse("INVENTORY_FILL_DISABLE", player));
}
}
}
@@ -0,0 +1,34 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.killchecker;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Cuboid {
private double x;
private double y;
private double z;
private double dx;
private double dy;
private double dz;
}
@@ -0,0 +1,115 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.killchecker;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.bausystem.utils.bossbar.BossBarService;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Linked
public class KillcheckerCommand extends SWCommand implements Listener {
private Map<Region, KillcheckerVisualizer> visualizers = new HashMap<>();
@LinkedInstance
public BossBarService bossBarService;
public KillcheckerCommand() {
super("killchecker");
addDefaultHelpMessage("KILLCHECKER_INFO");
addDefaultHelpMessage("KILLCHECKER_INFO2");
}
@Register(value = "enable", description = "KILLCHECKER_HELP_ENABLE")
public void genericCommand(@Validator Player player, @OptionalValue("-outline") @StaticValue(value = {"-area", "-outline"}, allowISE = true) boolean onlyOutline) {
Region region = Region.getRegion(player.getLocation());
KillcheckerVisualizer killcheckerVisualizer = visualizers.computeIfAbsent(region, region1 -> new KillcheckerVisualizer(region1, bossBarService));
killcheckerVisualizer.recalc();
killcheckerVisualizer.show(player, onlyOutline);
BauSystem.MESSAGE.send("KILLCHECKER_ENABLE", player);
}
@Register(value = "disable", description = "KILLCHECKER_HELP_DISABLE")
public void disableCommand(Player player) {
Region region = Region.getRegion(player.getLocation());
KillcheckerVisualizer killcheckerVisualizer = visualizers.get(region);
if (killcheckerVisualizer != null) {
if (killcheckerVisualizer.hide(player)) {
visualizers.remove(region);
}
}
BauSystem.MESSAGE.send("KILLCHECKER_DISABLE", player);
}
@EventHandler
public void onBauMemberUpdate(BauMemberUpdateEvent event) {
event.getNewSpectator().forEach(this::hide);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
hide(event.getPlayer());
}
private void hide(Player player) {
new HashSet<>(visualizers.entrySet()).forEach(regionKillcheckerVisualizerEntry -> {
if (regionKillcheckerVisualizerEntry.getValue().hide(player)) {
visualizers.remove(regionKillcheckerVisualizerEntry.getKey());
}
});
}
private void recalc(Block block) {
Region region = Region.getRegion(block.getLocation());
KillcheckerVisualizer killcheckerVisualizer = visualizers.get(region);
if (killcheckerVisualizer != null) {
killcheckerVisualizer.recalc();
}
}
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
recalc(event.getBlock());
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
recalc(event.getBlock());
}, 1);
}
}
@@ -0,0 +1,390 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.killchecker;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.bossbar.BauSystemBossbar;
import de.steamwar.bausystem.utils.bossbar.BossBarService;
import de.steamwar.entity.REntity;
import de.steamwar.entity.REntityServer;
import de.steamwar.entity.RFallingBlockEntity;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.boss.BarColor;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class KillcheckerVisualizer {
private static final Material[] MATERIALS = new Material[]{
Material.LIME_STAINED_GLASS,
Material.LIME_CONCRETE,
Material.GREEN_STAINED_GLASS,
Material.GREEN_CONCRETE,
Material.YELLOW_STAINED_GLASS,
Material.YELLOW_CONCRETE,
Material.ORANGE_STAINED_GLASS,
Material.ORANGE_CONCRETE,
Material.RED_STAINED_GLASS,
Material.RED_CONCRETE,
Material.PURPLE_STAINED_GLASS,
Material.PURPLE_CONCRETE,
Material.BLACK_STAINED_GLASS,
Material.BLACK_CONCRETE,
};
private static final World WORLD = Bukkit.getWorlds().get(0);
private static final double SURROUND = 4.5;
private final Point minPoint;
private final Point maxPoint;
private final int yArea;
private final int zArea;
private final int xArea;
private final Region region;
private final BossBarService bossBarService;
public KillcheckerVisualizer(Region region, BossBarService bossBarService) {
this.region = region;
this.minPoint = region.getMinPoint(RegionType.BUILD, RegionExtensionType.NORMAL);
this.maxPoint = region.getMaxPoint(RegionType.BUILD, RegionExtensionType.NORMAL);
yArea = (maxPoint.getX() - minPoint.getX()) * (maxPoint.getZ() - minPoint.getZ());
zArea = (maxPoint.getX() - minPoint.getX()) * (maxPoint.getY() - minPoint.getY());
xArea = (maxPoint.getY() - minPoint.getY()) * (maxPoint.getZ() - minPoint.getZ());
this.bossBarService = bossBarService;
}
private final REntityServer outline = new REntityServer();
private final REntityServer inner = new REntityServer();
private final Map<Point, Integer> killCount = new HashMap<>();
private final Set<Point> outlinePointsCache = new HashSet<>();
private final Map<Point, REntity> rEntities = new HashMap<>();
private double percent = 0;
private int kills = 0;
private int cannonCount = 0;
public void recalc() {
Set<Cuboid> cuboids = new HashSet<>();
Set<Point> points = new HashSet<>();
for (int x = minPoint.getX() + 1; x < maxPoint.getX(); x++) {
for (int y = minPoint.getY(); y < maxPoint.getY(); y++) {
for (int z = minPoint.getZ() + 1; z < maxPoint.getZ(); z++) {
if (points.contains(new Point(x, y, z))) continue;
Block block = WORLD.getBlockAt(x, y, z);
if (block.getType().isAir()) continue;
String name = block.getType().name();
if (!name.endsWith("_WOOL") && !name.endsWith("_STAINED_GLASS") && !name.endsWith("_CONCRETE") && !name.endsWith("_TERRACOTTA")) continue;
if (name.equals("_GLAZED_TERRACOTTA")) continue;
Cuboid cuboid = create(block.getType(), x, y, z);
cuboids.add(cuboid);
for (int dx = (int) cuboid.getX(); dx <= cuboid.getDx(); dx++) {
for (int dy = (int) cuboid.getY(); dy <= cuboid.getDy(); dy++) {
for (int dz = (int) cuboid.getZ(); dz <= cuboid.getDz(); dz++) {
points.add(new Point(dx, dy, dz));
}
}
}
}
}
}
cannonCount = cuboids.size();
Map<Point, Integer> kill = new HashMap<>();
int yKills = 0;
int yCount = 0;
Set<Point> yPoints = new HashSet<>();
for (int x = minPoint.getX(); x <= maxPoint.getX(); x++) {
for (int z = minPoint.getZ(); z <= maxPoint.getZ(); z++) {
Set<Cuboid> cuboidSet = new HashSet<>();
for (Cuboid cuboid : cuboids) {
if (x >= cuboid.getX() - SURROUND && x < cuboid.getDx() + SURROUND && z >= cuboid.getZ() - SURROUND && z < cuboid.getDz() + SURROUND) {
cuboidSet.add(cuboid);
}
}
if (cuboidSet.size() > 1) {
yCount++;
yKills += splitIntoDoubleKills(cuboidSet.size());
Point p2 = new Point(x, maxPoint.getY() + 1, z);
yPoints.add(p2);
kill.put(p2, Math.max(kill.getOrDefault(p2, 0), cuboidSet.size()));
}
}
}
int xKills = 0;
int xCount = 0;
Set<Point> xPoints = new HashSet<>();
for (int y = minPoint.getY(); y <= maxPoint.getY(); y++) {
for (int z = minPoint.getZ(); z <= maxPoint.getZ(); z++) {
Set<Cuboid> cuboidSet = new HashSet<>();
for (Cuboid cuboid : cuboids) {
if (y >= cuboid.getY() - SURROUND && y < cuboid.getDy() + SURROUND && z >= cuboid.getZ() - SURROUND && z < cuboid.getDz() + SURROUND) {
cuboidSet.add(cuboid);
}
}
if (cuboidSet.size() > 1) {
xCount++;
xKills += splitIntoDoubleKills(cuboidSet.size());
Point p1 = new Point(minPoint.getX() - 1, y, z);
xPoints.add(p1);
kill.put(p1, Math.max(kill.getOrDefault(p1, 0), cuboidSet.size()));
Point p2 = new Point(maxPoint.getX() + 1, y, z);
xPoints.add(p2);
kill.put(p2, Math.max(kill.getOrDefault(p2, 0), cuboidSet.size()));
}
}
}
int zKills = 0;
int zCount = 0;
Set<Point> zPoints = new HashSet<>();
for (int x = minPoint.getX(); x <= maxPoint.getX(); x++) {
for (int y = minPoint.getY(); y <= maxPoint.getY(); y++) {
Set<Cuboid> cuboidSet = new HashSet<>();
for (Cuboid cuboid : cuboids) {
if (x >= cuboid.getX() - SURROUND && x < cuboid.getDx() + SURROUND && y >= cuboid.getY() - SURROUND && y < cuboid.getDy() + SURROUND) {
cuboidSet.add(cuboid);
}
}
if (cuboidSet.size() > 1) {
zCount++;
zKills += splitIntoDoubleKills(cuboidSet.size());
Point p1 = new Point(x, y, minPoint.getZ() - 1);
zPoints.add(p1);
kill.put(p1, Math.max(kill.getOrDefault(p1, 0), cuboidSet.size()));
Point p2 = new Point(x, y, maxPoint.getZ() + 1);
zPoints.add(p2);
kill.put(p2, Math.max(kill.getOrDefault(p2, 0), cuboidSet.size()));
}
}
}
Set<Point> outlinePoints = new HashSet<>();
yPoints.forEach(point -> {
Point p1 = new Point(point.getX() - 1, point.getY(), point.getZ());
Point p2 = new Point(point.getX() + 1, point.getY(), point.getZ());
Point p3 = new Point(point.getX(), point.getY(), point.getZ() - 1);
Point p4 = new Point(point.getX(), point.getY(), point.getZ() + 1);
Point p5 = new Point(point.getX() - 1, point.getY(), point.getZ() - 1);
Point p6 = new Point(point.getX() - 1, point.getY(), point.getZ() + 1);
Point p7 = new Point(point.getX() + 1, point.getY(), point.getZ() - 1);
Point p8 = new Point(point.getX() + 1, point.getY(), point.getZ() + 1);
int count = kill.get(point);
int surrounded = 0;
if (kill.getOrDefault(p1, 0) == count) surrounded++;
if (kill.getOrDefault(p2, 0) == count) surrounded++;
if (kill.getOrDefault(p3, 0) == count) surrounded++;
if (kill.getOrDefault(p4, 0) == count) surrounded++;
if (surrounded != 4) outlinePoints.add(point);
if (kill.getOrDefault(p5, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p6, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p7, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p8, 0) != count) outlinePoints.add(point);
});
xPoints.forEach(point -> {
Point p1 = new Point(point.getX(), point.getY() - 1, point.getZ());
Point p2 = new Point(point.getX(), point.getY() + 1, point.getZ());
Point p3 = new Point(point.getX(), point.getY(), point.getZ() - 1);
Point p4 = new Point(point.getX(), point.getY(), point.getZ() + 1);
Point p5 = new Point(point.getX(), point.getY() - 1, point.getZ() - 1);
Point p6 = new Point(point.getX(), point.getY() - 1, point.getZ() + 1);
Point p7 = new Point(point.getX(), point.getY() + 1, point.getZ() - 1);
Point p8 = new Point(point.getX(), point.getY() + 1, point.getZ() + 1);
int count = kill.get(point);
int surrounded = 0;
if (kill.getOrDefault(p1, 0) == count) surrounded++;
if (kill.getOrDefault(p2, 0) == count) surrounded++;
if (kill.getOrDefault(p3, 0) == count) surrounded++;
if (kill.getOrDefault(p4, 0) == count) surrounded++;
if (surrounded != 4) outlinePoints.add(point);
if (kill.getOrDefault(p5, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p6, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p7, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p8, 0) != count) outlinePoints.add(point);
});
zPoints.forEach(point -> {
Point p1 = new Point(point.getX() - 1, point.getY(), point.getZ());
Point p2 = new Point(point.getX() + 1, point.getY(), point.getZ());
Point p3 = new Point(point.getX(), point.getY() - 1, point.getZ());
Point p4 = new Point(point.getX(), point.getY() + 1, point.getZ());
Point p5 = new Point(point.getX() - 1, point.getY() - 1, point.getZ());
Point p6 = new Point(point.getX() - 1, point.getY() + 1, point.getZ());
Point p7 = new Point(point.getX() + 1, point.getY() - 1, point.getZ());
Point p8 = new Point(point.getX() + 1, point.getY() + 1, point.getZ());
int count = kill.get(point);
int surrounded = 0;
if (kill.getOrDefault(p1, 0) == count) surrounded++;
if (kill.getOrDefault(p2, 0) == count) surrounded++;
if (kill.getOrDefault(p3, 0) == count) surrounded++;
if (kill.getOrDefault(p4, 0) == count) surrounded++;
if (surrounded != 4) outlinePoints.add(point);
if (kill.getOrDefault(p5, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p6, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p7, 0) != count) outlinePoints.add(point);
if (kill.getOrDefault(p8, 0) != count) outlinePoints.add(point);
});
double xPercent = zCount / (double) xArea;
double yPercent = yCount / (double) yArea;
double zPercent = xCount / (double) zArea;
percent = (xPercent + yPercent + zPercent) / 3;
kills = zKills + yKills + xKills;
outline.getPlayers().forEach(this::updateBossBar);
Set<Point> pointSet = new HashSet<>(killCount.keySet());
Set<Point> outlinePointsCacheLast = new HashSet<>(outlinePointsCache);
outlinePointsCache.clear();
for (Point point : pointSet) {
if (!kill.containsKey(point)) {
rEntities.get(point).die();
rEntities.remove(point);
killCount.remove(point);
}
}
kill.forEach((point, count) -> {
if (rEntities.containsKey(point)) {
if (killCount.get(point) == count && outlinePoints.contains(point) == outlinePointsCacheLast.contains(point)) return;
rEntities.get(point).die();
}
RFallingBlockEntity entity = new RFallingBlockEntity(outlinePoints.contains(point) ? outline : inner, point.toLocation(WORLD, 0.5, 0, 0.5), MATERIALS[Math.min(count - 1, MATERIALS.length) - 1]);
entity.setNoGravity(true);
rEntities.put(point, entity);
if (outlinePoints.contains(point)) outlinePointsCache.add(point);
killCount.put(point, count);
});
}
private int splitIntoDoubleKills(int kills) {
return kills * (kills - 1) / 2;
}
private void updateBossBar(Player player) {
BauSystemBossbar bossbar = bossBarService.get(player, region, "killchecker");
bossbar.setTitle(BauSystem.MESSAGE.parse("KILLCHECKER_BOSSBAR", player, kills, ((int) (percent * 1000) / 10.0), cannonCount));
bossbar.setProgress(Math.min(Math.max(percent, 0), 1));
if (percent >= 0.35) {
bossbar.setColor(BarColor.RED);
} else if (percent >= 0.25) {
bossbar.setColor(BarColor.PURPLE);
} else if (percent >= 0.15) {
bossbar.setColor(BarColor.YELLOW);
} else {
bossbar.setColor(BarColor.GREEN);
}
}
private Cuboid create(Material type, int x, int y, int z) {
Set<Point> checked = new HashSet<>();
Set<Point> points = new HashSet<>();
points.add(new Point(x, y, z));
while (!points.isEmpty()) {
Point point = points.iterator().next();
points.remove(point);
if (!checked.add(point)) continue;
if (point.getX() < minPoint.getX() || point.getX() > maxPoint.getX()) continue;
if (point.getY() < minPoint.getY() || point.getY() > maxPoint.getY()) continue;
if (point.getZ() < minPoint.getZ() || point.getZ() > maxPoint.getZ()) continue;
if (WORLD.getBlockAt(point.getX() + 1, point.getY(), point.getZ()).getType() == type) {
points.add(new Point(point.getX() + 1, point.getY(), point.getZ()));
}
if (WORLD.getBlockAt(point.getX(), point.getY() + 1, point.getZ()).getType() == type) {
points.add(new Point(point.getX(), point.getY() + 1, point.getZ()));
}
if (WORLD.getBlockAt(point.getX(), point.getY(), point.getZ() + 1).getType() == type) {
points.add(new Point(point.getX(), point.getY(), point.getZ() + 1));
}
if (WORLD.getBlockAt(point.getX() - 1, point.getY(), point.getZ()).getType() == type) {
points.add(new Point(point.getX() - 1, point.getY(), point.getZ()));
}
if (WORLD.getBlockAt(point.getX(), point.getY() - 1, point.getZ()).getType() == type) {
points.add(new Point(point.getX(), point.getY() - 1, point.getZ()));
}
if (WORLD.getBlockAt(point.getX(), point.getY(), point.getZ() - 1).getType() == type) {
points.add(new Point(point.getX(), point.getY(), point.getZ() - 1));
}
}
int minX = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int minY = Integer.MAX_VALUE;
int maxY = Integer.MIN_VALUE;
int minZ = Integer.MAX_VALUE;
int maxZ = Integer.MIN_VALUE;
for (Point point : checked) {
if (point.getX() < minX) minX = point.getX();
if (point.getX() > maxX) maxX = point.getX();
if (point.getY() < minY) minY = point.getY();
if (point.getY() > maxY) maxY = point.getY();
if (point.getZ() < minZ) minZ = point.getZ();
if (point.getZ() > maxZ) maxZ = point.getZ();
}
return new Cuboid(minX, minY, minZ, maxX, maxY, maxZ);
}
public void show(Player player, boolean onlyOutline) {
outline.addPlayer(player);
if (!onlyOutline) {
inner.addPlayer(player);
} else {
inner.removePlayer(player);
}
updateBossBar(player);
}
public boolean hide(Player player) {
outline.removePlayer(player);
inner.removePlayer(player);
bossBarService.remove(player, region, "killchecker");
if (outline.getPlayers().isEmpty() && inner.getPlayers().isEmpty()) {
outline.close();
inner.close();
return true;
}
return false;
}
}
@@ -0,0 +1,380 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.features.loader.elements.LoaderElement;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.impl.LoaderTNT;
import de.steamwar.bausystem.features.loader.elements.impl.LoaderWait;
import de.steamwar.bausystem.shared.EnumDisplay;
import de.steamwar.inventory.SWAnvilInv;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
public class Loader implements Listener {
private static final Map<Player, Loader> LOADER_MAP = new HashMap<>();
public static Loader getLoader(Player player) {
return LOADER_MAP.get(player);
}
public static void newLoader(Player player) {
LOADER_MAP.put(player, new Loader(player));
}
private final Player p;
@Getter
private Stage stage = Stage.SETUP;
private LoaderRecorder recorder;
private List<LoaderElement> elements = new ArrayList<>();
private int currentElement = 0;
private long waitTime = 0;
public Loader(Player p) {
this.p = p;
this.recorder = new LoaderRecorder(p, elements);
Bukkit.getPluginManager().registerEvents(this, BauSystem.getInstance());
BauSystem.runTaskTimer(BauSystem.getInstance(), () -> {
if (stage != Stage.RUNNING) return;
if(!Permission.BUILD.hasPermission(p)) return;
if (waitTime > 0) {
waitTime--;
return;
}
if (currentElement >= elements.size()) {
currentElement = 0;
if (stage == Stage.SINGLE) {
stage = Stage.PAUSE;
return;
}
}
while (currentElement < elements.size()) {
LoaderElement element = elements.get(currentElement);
currentElement++;
element.execute(delay -> waitTime = delay);
if (waitTime > 0) {
if (element instanceof LoaderTNT) currentElement--;
return;
}
}
}, 0, 1);
}
public void single() {
if (stage == Stage.END) return;
if (stage == Stage.RUNNING) return;
stage = Stage.SINGLE;
if (recorder != null) {
recorder.stop();
recorder = null;
}
if (elements.isEmpty()) {
BauSystem.MESSAGE.send("LOADER_NOTHING_RECORDED", p);
stop();
}
}
public void start() {
if (stage == Stage.END) return;
if (stage == Stage.RUNNING) return;
stage = Stage.RUNNING;
if (recorder != null) {
recorder.stop();
recorder = null;
}
if (elements.isEmpty()) {
BauSystem.MESSAGE.send("LOADER_NOTHING_RECORDED", p);
stop();
}
}
public void pause() {
if (stage == Stage.END) return;
if (stage == Stage.PAUSE) return;
stage = Stage.PAUSE;
if (recorder != null) {
recorder.stop();
recorder = null;
}
}
public void stop() {
stage = Stage.END;
if (recorder != null) {
recorder.stop();
recorder = null;
}
elements.clear();
LOADER_MAP.remove(p);
}
public boolean setTicksBetweenShots(int delay) {
if (elements.size() == 0) return false;
LoaderElement loaderElement = elements.get(elements.size() - 1);
if (loaderElement instanceof LoaderWait) {
((LoaderWait) loaderElement).setDelay(delay);
return true;
}
return false;
}
public void setTicksBetweenBlocks(int delay) {
for (int i = 0; i < elements.size() - 1; i++) {
LoaderElement loaderElement = elements.get(i);
if (loaderElement instanceof LoaderWait) {
((LoaderWait) loaderElement).setDelay(delay);
}
}
}
public void gui(SettingsSorting settingsSorting) {
List<SWListInv.SWListEntry<LoaderElement>> list = new ArrayList<>();
AtomicBoolean allWait = new AtomicBoolean(true);
Runnable updateRunnable = () -> {
list.clear();
for (int i = 0; i < elements.size(); i++) {
LoaderElement previous = i > 0 ? elements.get(i - 1) : null;
LoaderElement current = elements.get(i);
LoaderElement next = i < elements.size() - 1 ? elements.get(i + 1) : null;
if (!settingsSorting.shouldShow(previous, current, next)) {
continue;
}
if ((!(current instanceof LoaderWait))) {
allWait.set(false);
}
SWItem item = current.menu(p);
if (current instanceof LoaderInteractionElement<?>) {
LoaderInteractionElement<?> interactionElement = (LoaderInteractionElement<?>) current;
List<String> lore = new ArrayList<>();
if (item.getItemMeta() != null && item.getItemMeta().getLore() != null) {
lore.addAll(item.getItemMeta().getLore());
lore.add("§8");
}
lore.add(BauSystem.MESSAGE.parse("LOADER_SETTING_MODES", p, interactionElement.size()));
lore.add(BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", p));
item.setLore(lore);
} else {
List<String> lore = new ArrayList<>();
if (item.getItemMeta() != null && item.getItemMeta().getLore() != null) {
lore.addAll(item.getItemMeta().getLore());
lore.add("§8");
}
lore.add(BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", p));
item.setLore(lore);
}
list.add(new SWListInv.SWListEntry<>(item, current));
}
if (list.isEmpty()) {
allWait.set(false);
}
};
updateRunnable.run();
SWListInv<LoaderElement> swListInv = new SWListInv<>(p, BauSystem.MESSAGE.parse("LOADER_GUI_TITLE", p), false, list, (clickType, loaderElement) -> {});
swListInv.setCallback((clickType, entry) -> entry.click(p, () -> {
updateRunnable.run();
swListInv.open();
}));
SWItem settingItem = new SWItem(settingsSorting.getMaterial(), "§e" + BauSystem.MESSAGE.parse(settingsSorting.getName(), p), clickType -> {
if (clickType == ClickType.LEFT) {
int index = settingsSorting.ordinal() + 1;
if (index >= SettingsSorting.LENGTH) {
index = 0;
}
gui(SettingsSorting.values()[index]);
} else if (clickType == ClickType.RIGHT) {
int index = settingsSorting.ordinal() - 1;
if (index < 0) {
index = SettingsSorting.LENGTH - 1;
}
gui(SettingsSorting.values()[index]);
}
});
List<String> strings = new ArrayList<>();
for (SettingsSorting setting : SettingsSorting.values()) {
if (setting == settingsSorting) {
strings.add("§e> §7" + BauSystem.MESSAGE.parse(setting.getName(), p));
} else {
strings.add("§8> §7" + BauSystem.MESSAGE.parse(setting.getName(), p));
}
}
settingItem.setLore(strings);
swListInv.setItem(48, settingItem);
if (allWait.get()) {
SWItem setWait = new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS_SET_ALL", p), clickType -> {
SWAnvilInv swAnvilInv = new SWAnvilInv(p, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS_TITLE", p), "");
swAnvilInv.setCallback(s -> {
try {
long delay = Math.max(Long.parseLong(s), 0);
list.forEach(loaderElementSWListEntry -> {
((LoaderWait) loaderElementSWListEntry.getObject()).setDelay(delay);
});
} catch (NumberFormatException ignored) {
}
gui(settingsSorting);
});
swAnvilInv.open();
});
swListInv.setItem(50, setWait);
} else {
swListInv.setItem(50, new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§8"));
}
swListInv.open();
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
if (event.getPlayer() != p) return;
stop();
}
public String getProgress() {
return Math.max(currentElement, 1) + "§8/§7" + elements.size();
}
public enum SettingsSorting {
ALL {
@Override
public Material getMaterial() {
return Material.STRUCTURE_VOID;
}
@Override
public String getName() {
return "LOADER_GUI_SHOW_ALL";
}
@Override
public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) {
return true;
}
},
WAIT {
@Override
public Material getMaterial() {
return Material.CLOCK;
}
@Override
public String getName() {
return "LOADER_GUI_SHOW_WAITS";
}
@Override
public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) {
return current instanceof LoaderWait;
}
},
WAIT_BETWEEN_TNT {
@Override
public Material getMaterial() {
return Material.REDSTONE_BLOCK;
}
@Override
public String getName() {
return "LOADER_GUI_SHOW_WAITS_BETWEEN_TNT";
}
@Override
public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) {
return previous instanceof LoaderTNT && current instanceof LoaderWait && next instanceof LoaderTNT;
}
},
INTERACTIONS {
@Override
public Material getMaterial() {
return Material.REPEATER;
}
@Override
public String getName() {
return "LOADER_GUI_SHOW_INTERACTIONS";
}
@Override
public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) {
return current instanceof LoaderInteractionElement && !(current instanceof LoaderTNT);
}
},
TNT {
@Override
public Material getMaterial() {
return Material.TNT;
}
@Override
public String getName() {
return "LOADER_GUI_SHOW_TNT";
}
@Override
public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) {
return current instanceof LoaderTNT;
}
},
;
public static int LENGTH = SettingsSorting.values().length;
public abstract Material getMaterial();
public abstract String getName();
public abstract boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next);
}
@AllArgsConstructor
public enum Stage implements EnumDisplay {
SETUP("LOADER_SETUP"),
RUNNING("LOADER_RUNNING"),
SINGLE("LOADER_SINGLE"),
PAUSE("LOADER_PAUSE"),
END("LOADER_END");
@Getter
private final String chatValue;
}
}
@@ -0,0 +1,122 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@Linked
public class LoaderCommand extends SWCommand implements Listener {
public LoaderCommand() {
super("loader");
}
private boolean loaderNullCheck(Loader loader, Player p) {
if (loader == null) {
BauSystem.MESSAGE.send("LOADER_NO_LOADER", p);
return true;
}
return false;
}
@Register(value = "setup", description = "LOADER_HELP_SETUP")
public void setupLoader(@Validator Player player) {
if (Loader.getLoader(player) != null) {
BauSystem.MESSAGE.send("LOADER_SETUP_STOP_FIRST", player);
return;
}
Loader.newLoader(player);
BauSystem.MESSAGE.send("LOADER_NEW", player);
BauSystem.MESSAGE.send("LOADER_HOW_TO_START", player);
}
@Register(value = "start", description = "LOADER_HELP_START")
public void startLoader(@Validator Player player) {
Loader loader = Loader.getLoader(player);
if (loaderNullCheck(loader, player)) return;
loader.start();
BauSystem.MESSAGE.send("LOADER_ACTIVE", player);
}
@Register(value = "stop", description = "LOADER_HELP_STOP")
public void stopLoader(@Validator Player player) {
Loader loader = Loader.getLoader(player);
if (loaderNullCheck(loader, player)) return;
loader.stop();
BauSystem.MESSAGE.send("LOADER_STOP", player);
}
@Register(value = "pause", description = "LOADER_HELP_PAUSE")
public void pauseLoader(@Validator Player player) {
Loader loader = Loader.getLoader(player);
if (loaderNullCheck(loader, player)) return;
loader.pause();
BauSystem.MESSAGE.send("LOADER_PAUSED", player);
}
@Register(value = "gui", description = "LOADER_HELP_GUI")
public void guiLoader(@Validator Player player) {
Loader loader = Loader.getLoader(player);
if (loaderNullCheck(loader, player)) return;
loader.gui(Loader.SettingsSorting.ALL);
}
@Register(value = "wait", description = "LOADER_HELP_WAIT")
public void shotDelayLoader(@Validator Player p, @Min(intValue = 1) @ErrorMessage("LOADER_SMALL_TIME") int delay) {
Loader loader = Loader.getLoader(p);
if (loaderNullCheck(loader, p)) return;
if (loader.setTicksBetweenShots(delay)) {
BauSystem.MESSAGE.send("LOADER_NEW_TIME", p, delay);
} else {
BauSystem.MESSAGE.send("LOADER_SMALL_TIME", p);
}
}
@Register(value = "speed", description = "LOADER_HELP_SPEED")
public void speedLoader(@Validator Player p, @Min(intValue = 0) @ErrorMessage("LOADER_SMALL_TIME") int delay) {
Loader loader = Loader.getLoader(p);
if (loaderNullCheck(loader, p)) return;
BauSystem.MESSAGE.send("LOADER_NEW_LOAD_TIME", p, delay);
loader.setTicksBetweenBlocks(delay);
}
@Register(value = "single", description = "LOADER_HELP_SINGLE")
public void singleLoader(@Validator Player p) {
Loader loader = Loader.getLoader(p);
if (loaderNullCheck(loader, p)) return;
loader.single();
BauSystem.MESSAGE.send("LOADER_SINGLE_CMD", p);
}
@EventHandler
public void onBauMemberUpdate(BauMemberUpdateEvent event) {
event.getNewSpectator().forEach(player -> {
Loader loader = Loader.getLoader(player);
if (loader == null) return;
loader.stop();
});
}
}
@@ -0,0 +1,231 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.loader.elements.LoaderElement;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.impl.*;
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.EquipmentSlot;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class LoaderRecorder implements Listener {
private Player player;
private List<LoaderElement> loaderElementList;
private long lastInteraction = TPSUtils.currentRealTick.get();
public LoaderRecorder(Player player, List<LoaderElement> loaderElementList) {
this.player = player;
this.loaderElementList = loaderElementList;
Bukkit.getPluginManager().registerEvents(this, BauSystem.getInstance());
}
public void stop() {
addWaitTime(true);
HandlerList.unregisterAll(this);
player = null;
blockSet.clear();
}
private void addWaitTime(boolean last) {
if (loaderElementList.isEmpty()) {
lastInteraction = TPSUtils.currentRealTick.get();
return;
}
if (loaderElementList.get(loaderElementList.size() - 1) instanceof LoaderWait) {
return;
}
long diff = TPSUtils.currentRealTick.get() - lastInteraction;
if (last) diff = 120;
lastInteraction = TPSUtils.currentRealTick.get();
loaderElementList.add(new LoaderWait(diff));
}
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
if (event.getPlayer() != player) return;
if (event.getBlock().getType() != Material.TNT) return;
addWaitTime(false);
loaderElementList.add(new LoaderTNT(event.getBlock().getLocation()));
message("LOADER_BUTTON_TNT");
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (event.getPlayer() != player) return;
boolean removedOne = false;
for (int i = 0; i < loaderElementList.size(); i++) {
LoaderElement element = loaderElementList.get(i);
if (!(element instanceof LoaderInteractionElement)) continue;
LoaderInteractionElement interactionElement = (LoaderInteractionElement) element;
if (interactionElement.getLocation().equals(event.getBlock().getLocation())) {
loaderElementList.remove(i);
if (i > 0) {
loaderElementList.remove(i - 1);
}
removedOne = true;
break;
}
}
if (removedOne) {
if (event.getBlock().getType() != Material.TNT) {
event.setCancelled(true);
}
SWUtils.sendToActionbar(player, BauSystem.MESSAGE.parse("LOADER_MESSAGE_UNINTERACT", player));
}
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getPlayer() != player) return;
if (player.isSneaking()) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.PHYSICAL) return;
if (event.getClickedBlock().getType() == Material.OBSERVER) return;
if (event.getHand() == EquipmentSlot.OFF_HAND) return;
addWaitTime(false);
Block block = event.getClickedBlock();
getLoaderInteractionElement(block, (loaderInteractionElement, s) -> {
loaderElementList.add(loaderInteractionElement);
message(s);
});
}
private Map<Location, Long> blockSet = new HashMap<>();
private Map<Location, LoaderMovement> movementSet = new HashMap<>();
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if (event.getPlayer() != player) return;
Block fromBlock = event.getFrom().getBlock();
Block toBlock = event.getTo().getBlock();
calcMovementBlocks(fromBlock, toBlock);
fromBlock = fromBlock.getRelative(0, 1, 0);
toBlock = toBlock.getRelative(0, 1, 0);
calcMovementBlocks(fromBlock, toBlock);
}
private void calcMovementBlocks(Block fromBlock, Block toBlock) {
if (!blockSet.containsKey(toBlock.getLocation())) {
Long startTime = blockSet.remove(fromBlock.getLocation());
LoaderMovement loaderMovement = movementSet.remove(fromBlock.getLocation());
if (loaderMovement != null && startTime != null) {
loaderMovement.setInitialTicks(TPSUtils.currentRealTick.get() - startTime);
}
blockSet.put(toBlock.getLocation(), TPSUtils.currentRealTick.get());
addWaitTime(false);
loaderMovement = null;
Material type = toBlock.getType();
switch (type) {
case TRIPWIRE:
loaderMovement = new LoaderMovement(toBlock.getLocation(), "LOADER_BUTTON_TRIPWIRE", Material.STRING);
message("LOADER_BUTTON_TRIPWIRE");
break;
case LIGHT_WEIGHTED_PRESSURE_PLATE:
case HEAVY_WEIGHTED_PRESSURE_PLATE:
loaderMovement = new LoaderMovement(toBlock.getLocation(), "LOADER_BUTTON_WEIGHTED_PRESSURE_PLATE", type);
message("LOADER_BUTTON_WEIGHTED_PRESSURE_PLATE");
break;
default:
if (type.name().endsWith("PRESSURE_PLATE")) {
loaderMovement = new LoaderMovement(toBlock.getLocation(), "LOADER_BUTTON_PRESSURE_PLATE", type);
message("LOADER_BUTTON_PRESSURE_PLATE");
}
break;
}
if (loaderMovement != null) {
movementSet.put(toBlock.getLocation(), loaderMovement);
loaderElementList.add(loaderMovement);
}
}
}
public static void getLoaderInteractionElement(Block block, BiConsumer<LoaderInteractionElement<?>, String> consumer) {
Material type = block.getType();
switch (type) {
case COMPARATOR:
consumer.accept(new LoaderComparator(block.getLocation()), "LOADER_BUTTON_COMPARATOR");
break;
case REPEATER:
consumer.accept(new LoaderRepeater(block.getLocation()), "LOADER_BUTTON_REPEATER");
break;
case NOTE_BLOCK:
consumer.accept(new LoaderNoteBlock(block.getLocation()), "LOADER_BUTTON_NOTEBLOCK");
break;
case LEVER:
consumer.accept(new LoaderLever(block.getLocation()), "LOADER_BUTTON_SWITCH");
break;
case DAYLIGHT_DETECTOR:
consumer.accept(new LoaderDaylightDetector(block.getLocation()), "LOADER_BUTTON_DAYLIGHT_DETECTOR");
break;
case LECTERN:
consumer.accept(new LoaderLectern(block.getLocation()), "LOADER_BUTTON_LECTERN");
break;
case IRON_TRAPDOOR:
case IRON_DOOR:
break;
default:
if (type.name().endsWith("_TRAPDOOR")) {
consumer.accept(new LoaderOpenable(block.getLocation(), "LOADER_BUTTON_TRAPDOOR", type), "LOADER_BUTTON_TRAPDOOR");
} else if (type.name().endsWith("_DOOR")) {
consumer.accept(new LoaderOpenable(block.getLocation(), "LOADER_BUTTON_DOOR", type), "LOADER_BUTTON_DOOR");
} else if (type.name().endsWith("FENCE_GATE")) {
consumer.accept(new LoaderOpenable(block.getLocation(), "LOADER_BUTTON_FENCEGATE", type), "LOADER_BUTTON_FENCEGATE");
} else if (type.name().endsWith("STONE_BUTTON")) {
consumer.accept(new LoaderTicks(block.getLocation(), "LOADER_BUTTON_STONE_BUTTON", type, 20), "LOADER_BUTTON_STONE_BUTTON");
} else if (type.name().endsWith("BUTTON")) {
consumer.accept(new LoaderTicks(block.getLocation(), "LOADER_BUTTON_WOOD_BUTTON", type, 30), "LOADER_BUTTON_WOOD_BUTTON");
}
break;
}
}
private void message(String type) {
SWUtils.sendToActionbar(player, BauSystem.MESSAGE.parse("LOADER_MESSAGE_INTERACT", player, BauSystem.MESSAGE.parse(type, player), loaderElementList.size()));
}
}
@@ -0,0 +1,55 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class LoaderScoreboardElement implements ScoreboardElement {
@Override
public ScoreboardGroup getGroup() {
return ScoreboardGroup.OTHER;
}
@Override
public int order() {
return 2;
}
@Override
public String get(Region region, Player p) {
if(!Permission.BUILD.hasPermission(p)) return null;
Loader loader = Loader.getLoader(p);
if (loader == null) return null;
if (loader.getStage() == Loader.Stage.RUNNING) {
return "§e" + BauSystem.MESSAGE.parse("SCOREBOARD_LOADER", p) + "§8: §a" + loader.getProgress();
} else if (loader.getStage() == Loader.Stage.PAUSE) {
return "§e" + BauSystem.MESSAGE.parse("SCOREBOARD_LOADER", p) + "§8: §c" + loader.getProgress();
} else {
return "§e" + BauSystem.MESSAGE.parse("SCOREBOARD_LOADER", p) + "§8: " + BauSystem.MESSAGE.parse(loader.getStage().getChatValue(), p);
}
}
}
@@ -0,0 +1,31 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements;
import de.steamwar.inventory.SWItem;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public interface LoaderElement {
SWItem menu(Player player);
void execute(Consumer<Long> delay);
void click(Player player, Runnable backAction);
}
@@ -0,0 +1,310 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.inventory.SWAnvilInv;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.*;
import org.bukkit.block.data.type.Door;
import org.bukkit.block.data.type.Switch;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public abstract class LoaderInteractionElement<T extends Enum<T> & LoaderSettingsEnum> implements LoaderElement {
@Getter
protected final Location location;
protected int currentShot = 0;
protected T defaultSetting;
protected T newSetting;
protected T[] allSettings;
protected List<T> elements = new ArrayList<>();
protected List<Integer> extraPower = new ArrayList<>();
protected List<Long> extraTicks = new ArrayList<>();
protected int settingsGuiSize = 0;
protected LoaderInteractionElement(Location location, T defaultSetting, T newSetting, T[] allSettings) {
this.location = location;
this.defaultSetting = defaultSetting;
this.newSetting = newSetting;
this.allSettings = allSettings;
elements.add(defaultSetting);
extraPower.add(0);
extraTicks.add(0L);
for (T element : allSettings) {
settingsGuiSize = Math.max(element.getPos(), settingsGuiSize);
}
while (settingsGuiSize % 9 != 0) settingsGuiSize++;
settingsGuiSize += 9;
}
@Override
public void execute(Consumer<Long> delay) {
if (currentShot >= elements.size()) currentShot = 0;
if (checkBlockInWorld()) {
BlockData blockData = location.getBlock().getBlockData();
elements.get(currentShot).execute(location, blockData, this, extraPower.get(currentShot), extraTicks.get(currentShot), delay);
}
currentShot++;
if (currentShot >= elements.size()) currentShot = 0;
}
@Override
public void click(Player player, Runnable backAction) {
List<SWListInv.SWListEntry<Integer>> entries = new ArrayList<>();
Runnable updateRunnable = () -> {
entries.clear();
for (int i = 0; i < elements.size(); i++) {
SWItem swItem = elements.get(i).menu(player, this, extraPower.get(i), extraTicks.get(i));
swItem.setLore(Arrays.asList(BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", player)));
entries.add(new SWListInv.SWListEntry<>(swItem, i));
}
};
updateRunnable.run();
SWListInv<Integer> listInv = new SWListInv<>(player, "Interaction Settings", false, entries, (clickType, entry) -> {});
listInv.setCallback((clickType, entry) -> {
openIndividualSettingsMenu(player, entry, () -> {
updateRunnable.run();
listInv.open();
}, () -> {
elements.remove((int) entry);
extraPower.remove((int) entry);
extraTicks.remove((int) entry);
if (elements.isEmpty()) {
elements.add(newSetting);
extraPower.add(0);
extraTicks.add(1L);
}
click(player, backAction);
});
});
listInv.setItem(48, new SWItem(Material.ARROW, "§7Back", clickType -> {
backAction.run();
}));
listInv.setItem(50, new SWItem(Material.GHAST_SPAWN_EGG, "§7Insert another Setting", clickType -> {
elements.add(defaultSetting);
extraPower.add(0);
extraTicks.add(1L);
openIndividualSettingsMenu(player, elements.size() - 1, () -> {
updateRunnable.run();
listInv.open();
}, () -> {
elements.remove(elements.size() - 1);
extraPower.remove(extraPower.size() - 1);
extraTicks.remove(extraTicks.size() - 1);
if (elements.isEmpty()) {
elements.add(newSetting);
extraPower.add(0);
extraTicks.add(1L);
}
click(player, backAction);
});
}));
listInv.open();
}
private void openIndividualSettingsMenu(Player player, int index, Runnable back, Runnable delete) {
T currentElement = elements.get(index);
int guiSize = settingsGuiSize;
int powerStart = guiSize - 9;
if (currentElement.hasPower(this)) {
guiSize += 18;
}
int ticksStart = guiSize - 9;
if (currentElement.hasTicks(this)) {
guiSize += 9;
}
SWInventory swInventory = new SWInventory(player, guiSize, BauSystem.MESSAGE.parse("LOADER_GUI_SETTINGS_TITLE", player));
for (int i = guiSize - 9; i < guiSize; i++) swInventory.setItem(i, new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", clickType -> {}));
swInventory.setItem(guiSize - 9, new SWItem(Material.ARROW, BauSystem.MESSAGE.parse("LOADER_GUI_SETTINGS_BACK", player)).getItemStack(), clickType -> back.run());
swInventory.setItem(guiSize - 5, new SWItem(Material.WOODEN_AXE, BauSystem.MESSAGE.parse("LOADER_GUI_SETTINGS_COPY", player)).getItemStack(), clickType -> {
SWAnvilInv swAnvilInv = new SWAnvilInv(player, BauSystem.MESSAGE.parse("LOADER_GUI_COPY_TITLE", player), "1");
swAnvilInv.setCallback(s -> {
try {
int count = Integer.parseInt(s);
if (count < 1) count = 1;
if (count > 65536) count = 65536;
int power = extraPower.get(index);
long ticks = extraTicks.get(index);
for (int i = 0; i < count; i++) {
elements.add(currentElement);
extraPower.add(power);
extraTicks.add(ticks);
}
if (count == 1) {
openIndividualSettingsMenu(player, elements.size() - 1, back, delete);
} else {
back.run();
}
} catch (NumberFormatException e) {
back.run();
}
});
swAnvilInv.open();
});
swInventory.setItem(guiSize - 1, new SWItem(Material.BARRIER, BauSystem.MESSAGE.parse("LOADER_GUI_SETTINGS_DELETE", player)).getItemStack(), clickType -> delete.run());
for (T element : allSettings) {
SWItem item = element.menu(player, this, extraPower.get(index), extraTicks.get(index));
if (element == currentElement) {
item.setEnchanted(true);
}
swInventory.setItem(element.getPos(), item.getItemStack(), clickType -> {
elements.set(index, element);
openIndividualSettingsMenu(player, index, back, delete);
});
}
if (currentElement.hasPower(this)) {
for (int power = 0; power < 16; power++) {
int finalPowerPosition = power;
if (power >= 9) finalPowerPosition++;
SWItem powerItem = new SWItem(Material.REDSTONE, BauSystem.MESSAGE.parse("LOADER_SETTING_POWER", player, power), Arrays.asList(), false, clickType -> {});
powerItem.getItemStack().setAmount(Math.max(power, 1));
if (extraPower.get(index) == power) powerItem.setEnchanted(true);
int finalPower = power;
swInventory.setItem(finalPowerPosition + powerStart, powerItem.getItemStack(), clickType -> {
extraPower.set(index, finalPower);
openIndividualSettingsMenu(player, index, back, delete);
});
}
}
if (currentElement.hasTicks(this)) {
swInventory.setItem(ticksStart + 3, new SWItem(SWItem.getDye(1), BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_REMOVE_ONE", player), Arrays.asList(BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_REMOVE_ONE_SHIFT", player)), false, clickType -> {}).getItemStack(), clickType -> {
long ticks = extraTicks.get(index);
ticks -= clickType.isShiftClick() ? 5 : 1;
if (ticks < 1) ticks = 1;
extraTicks.set(index, ticks);
openIndividualSettingsMenu(player, index, back, delete);
});
SWItem ticksItem = new SWItem(Material.CLOCK, BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS", player, extraTicks.get(index)), Arrays.asList(BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", player)), false, clickType -> {});
ticksItem.getItemStack().setAmount((int) Math.min(extraTicks.get(index), 64));
swInventory.setItem(ticksStart + 4, ticksItem.getItemStack(), clickType -> {
SWAnvilInv swAnvilInv = new SWAnvilInv(player, BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_NAME", player), extraTicks.get(index) + "");
swAnvilInv.setCallback(s -> {
try {
long ticks = Long.parseLong(s);
if (ticks < 1) ticks = 1;
extraTicks.set(index, ticks);
} catch (NumberFormatException ignored) {
}
openIndividualSettingsMenu(player, index, back, delete);
});
swAnvilInv.open();
});
swInventory.setItem(ticksStart + 5, new SWItem(SWItem.getDye(10), BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_ADD_ONE", player), Arrays.asList(BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_ADD_ONE_SHIFT", player)), false, clickType -> {}).getItemStack(), clickType -> {
long ticks = extraTicks.get(index);
ticks += clickType.isShiftClick() ? 5 : 1;
extraTicks.set(index, ticks);
openIndividualSettingsMenu(player, index, back, delete);
});
}
swInventory.open();
}
protected void update(BlockData blockData) {
Block block = location.getBlock();
if (blockData instanceof Door) {
Door door = (Door) blockData;
block.setBlockData(door);
if (door.getHalf() == Bisected.Half.BOTTOM) {
updateBlock(block.getRelative(BlockFace.UP), door.isOpen() ? 15 : 0);
} else {
updateBlock(block.getRelative(BlockFace.DOWN), door.isOpen() ? 15 : 0);
}
} else if (blockData instanceof Switch) {
Switch sw = (Switch) blockData;
updateBlock(block, sw);
FaceAttachable.AttachedFace face = sw.getAttachedFace();
if (face == FaceAttachable.AttachedFace.FLOOR) {
updateBlock(block.getRelative(BlockFace.DOWN), sw.isPowered() ? 15 : 0);
} else if (face == FaceAttachable.AttachedFace.CEILING) {
updateBlock(block.getRelative(BlockFace.UP), sw.isPowered() ? 15 : 0);
} else {
updateBlock(block.getRelative(sw.getFacing().getOppositeFace()), sw.isPowered() ? 15 : 0);
}
} else if (blockData instanceof Powerable) {
updateBlock(block, blockData);
updateBlock(block.getRelative(BlockFace.DOWN), ((Powerable) blockData).isPowered() ? 15 : 0);
} else if (blockData instanceof AnaloguePowerable) {
updateBlock(block, blockData);
updateBlock(block.getRelative(BlockFace.DOWN), ((AnaloguePowerable) blockData).getPower());
} else {
updateBlock(block, blockData);
}
}
private void updateBlock(Block block, int powered) {
BlockData data = block.getBlockData();
if (data instanceof Door) {
Door door = (Door) data;
door.setOpen(powered > 0);
block.setBlockData(door);
return;
} else if (data instanceof Powerable) {
Powerable powerable = (Powerable) data;
powerable.setPowered(powered > 0);
} else if (data instanceof AnaloguePowerable) {
AnaloguePowerable analoguePowerable = (AnaloguePowerable) data;
analoguePowerable.setPower(powered);
}
updateBlock(block, data);
}
private void updateBlock(Block block, BlockData data) {
block.setType(Material.BARRIER);
block.setBlockData(data);
}
public abstract boolean checkBlockInWorld();
protected final String translateItemName(String name, Player player) {
return BauSystem.MESSAGE.parse("LOADER_SETTING_NAME", player, BauSystem.MESSAGE.parse(name, player));
}
public int size() {
return elements.size();
}
}
@@ -0,0 +1,44 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public interface LoaderSettingsEnum<T, P extends LoaderInteractionElement<E>, E extends Enum<E> & LoaderSettingsEnum<T, P, E>> {
int getPos();
SWItem menu(Player player, P parent, int power, long ticks);
default boolean hasPower(P parent) {
return false;
}
default boolean hasTicks(P parent) {
return false;
}
void execute(Location location, T blockData, P parent, int power, long ticks, Consumer<Long> delay);
default String translateItemName(String name, String mode, Player player, Object... args) {
return BauSystem.MESSAGE.parse("LOADER_GUI_ITEM_NAME", player, BauSystem.MESSAGE.parse(name, player), BauSystem.MESSAGE.parse(mode, player, args));
}
}
@@ -0,0 +1,122 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.type.Comparator;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.function.Consumer;
public class LoaderComparator extends LoaderInteractionElement<LoaderComparator.ComparatorSettingsEnum> {
public LoaderComparator(Location location) {
super(location, ComparatorSettingsEnum.INTERACT, ComparatorSettingsEnum.NOOP, ComparatorSettingsEnum.values());
}
public enum ComparatorSettingsEnum implements LoaderSettingsEnum<Comparator, LoaderComparator, ComparatorSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderComparator parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_COMPARATOR", "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, Comparator blockData, LoaderComparator parent, int power, long ticks, Consumer<Long> delay) {
}
},
INTERACT {
@Override
public int getPos() {
return 3;
}
@Override
public SWItem menu(Player player, LoaderComparator parent, int power, long ticks) {
return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_COMPARATOR", "LOADER_INTERACTION_INTERACT", player));
}
@Override
public void execute(Location location, Comparator blockData, LoaderComparator parent, int power, long ticks, Consumer<Long> delay) {
blockData.setMode(blockData.getMode() == Comparator.Mode.COMPARE ? Comparator.Mode.SUBTRACT : Comparator.Mode.COMPARE);
parent.update(blockData);
}
},
COMPARE {
@Override
public int getPos() {
return 5;
}
@Override
public SWItem menu(Player player, LoaderComparator parent, int power, long ticks) {
return new SWItem(Material.COMPARATOR, translateItemName("LOADER_BUTTON_COMPARATOR", "LOADER_INTERACTION_COMPARE", player));
}
@Override
public void execute(Location location, Comparator blockData, LoaderComparator parent, int power, long ticks, Consumer<Long> delay) {
blockData.setMode(Comparator.Mode.COMPARE);
parent.update(blockData);
}
},
SUBTRACT {
@Override
public int getPos() {
return 6;
}
@Override
public SWItem menu(Player player, LoaderComparator parent, int power, long ticks) {
return new SWItem(Material.COMPARATOR, translateItemName("LOADER_BUTTON_COMPARATOR", "LOADER_INTERACTION_SUBTRACT", player));
}
@Override
public void execute(Location location, Comparator blockData, LoaderComparator parent, int power, long ticks, Consumer<Long> delay) {
blockData.setMode(Comparator.Mode.SUBTRACT);
parent.update(blockData);
}
}
}
@Override
public SWItem menu(Player player) {
SWItem swItem = new SWItem(Material.COMPARATOR, "§7Comparator");
swItem.setLore(Arrays.asList("§7Modes§8: §e" + elements.size(), "§8", "§7Click to edit"));
return swItem;
}
@Override
public boolean checkBlockInWorld() {
return location.getBlock().getType() == Material.COMPARATOR;
}
}
@@ -0,0 +1,126 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.type.DaylightDetector;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public class LoaderDaylightDetector extends LoaderInteractionElement<LoaderDaylightDetector.DaylightSettingsEnum> {
public LoaderDaylightDetector(Location location) {
super(location, DaylightSettingsEnum.INTERACT, DaylightSettingsEnum.NOOP, DaylightSettingsEnum.values());
}
public enum DaylightSettingsEnum implements LoaderSettingsEnum<DaylightDetector, LoaderDaylightDetector, DaylightSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderDaylightDetector parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_DAYLIGHT_DETECTOR", "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, DaylightDetector blockData, LoaderDaylightDetector parent, int power, long ticks, Consumer<Long> delay) {
}
},
INTERACT {
@Override
public int getPos() {
return 3;
}
@Override
public SWItem menu(Player player, LoaderDaylightDetector parent, int power, long ticks) {
return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_DAYLIGHT_DETECTOR", "LOADER_INTERACTION_INTERACT", player));
}
@Override
public void execute(Location location, DaylightDetector blockData, LoaderDaylightDetector parent, int power, long ticks, Consumer<Long> delay) {
blockData.setInverted(!blockData.isInverted());
blockData.setPower(15 - blockData.getPower());
parent.update(blockData);
}
},
DAY_MODE {
@Override
public int getPos() {
return 5;
}
@Override
public SWItem menu(Player player, LoaderDaylightDetector parent, int power, long ticks) {
return new SWItem(Material.DAYLIGHT_DETECTOR, translateItemName("LOADER_BUTTON_DAYLIGHT_DETECTOR", "LOADER_INTERACTION_UNPOWERED", player));
}
@Override
public void execute(Location location, DaylightDetector blockData, LoaderDaylightDetector parent, int power, long ticks, Consumer<Long> delay) {
if (blockData.isInverted()) {
blockData.setInverted(false);
blockData.setPower(15 - blockData.getPower());
parent.update(blockData);
}
}
},
NIGHT_MODE {
@Override
public int getPos() {
return 6;
}
@Override
public SWItem menu(Player player, LoaderDaylightDetector parent, int power, long ticks) {
return new SWItem(Material.DAYLIGHT_DETECTOR, translateItemName("LOADER_BUTTON_DAYLIGHT_DETECTOR", "LOADER_INTERACTION_POWERED", player));
}
@Override
public void execute(Location location, DaylightDetector blockData, LoaderDaylightDetector parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.isInverted()) {
blockData.setInverted(true);
blockData.setPower(15 - blockData.getPower());
parent.update(blockData);
}
}
}
}
@Override
public SWItem menu(Player player) {
return new SWItem(Material.DAYLIGHT_DETECTOR, translateItemName("LOADER_BUTTON_DAYLIGHT_DETECTOR", player));
}
@Override
public boolean checkBlockInWorld() {
return location.getBlock().getType() == Material.DAYLIGHT_DETECTOR;
}
}
@@ -0,0 +1,520 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Lectern;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import java.util.function.Consumer;
public class LoaderLectern extends LoaderInteractionElement<LoaderLectern.LecternSettingsEnum> {
public LoaderLectern(Location location) {
super(location, LecternSettingsEnum.NEXT_PAGE, LecternSettingsEnum.NOOP, LecternSettingsEnum.values());
}
public enum LecternSettingsEnum implements LoaderSettingsEnum<org.bukkit.block.data.type.Lectern, LoaderLectern, LecternSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
}
},
PREVIOUS_PAGE {
@Override
public int getPos() {
return 5;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE_PREV", player));
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
if (lectern.getPage() > 0) {
lectern.setPage(lectern.getPage() - 1);
parent.update(blockData);
}
}
},
NEXT_PAGE {
@Override
public int getPos() {
return 6;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE_NEXT", player));
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (lectern.getPage() < pages) {
lectern.setPage(lectern.getPage() + 1);
parent.update(blockData);
}
}
},
PAGE_1 {
@Override
public int getPos() {
return 9;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 1));
swItem.getItemStack().setAmount(1);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (1 <= pages) {
lectern.setPage(1);
parent.update(blockData);
}
}
},
PAGE_2 {
@Override
public int getPos() {
return 10;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 2));
swItem.getItemStack().setAmount(2);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (2 <= pages) {
lectern.setPage(2);
parent.update(blockData);
}
}
},
PAGE_3 {
@Override
public int getPos() {
return 11;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 3));
swItem.getItemStack().setAmount(3);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (3 <= pages) {
lectern.setPage(3);
parent.update(blockData);
}
}
},
PAGE_4 {
@Override
public int getPos() {
return 12;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 4));
swItem.getItemStack().setAmount(4);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (4 <= pages) {
lectern.setPage(4);
parent.update(blockData);
}
}
},
PAGE_5 {
@Override
public int getPos() {
return 13;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 5));
swItem.getItemStack().setAmount(5);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (5 <= pages) {
lectern.setPage(5);
parent.update(blockData);
}
}
},
PAGE_6 {
@Override
public int getPos() {
return 14;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 6));
swItem.getItemStack().setAmount(6);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (6 <= pages) {
lectern.setPage(6);
parent.update(blockData);
}
}
},
PAGE_7 {
@Override
public int getPos() {
return 15;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 7));
swItem.getItemStack().setAmount(7);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (7 <= pages) {
lectern.setPage(7);
parent.update(blockData);
}
}
},
PAGE_8 {
@Override
public int getPos() {
return 16;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 8));
swItem.getItemStack().setAmount(8);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (8 <= pages) {
lectern.setPage(8);
parent.update(blockData);
}
}
},
PAGE_9 {
@Override
public int getPos() {
return 17;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 9));
swItem.getItemStack().setAmount(9);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (9 <= pages) {
lectern.setPage(9);
parent.update(blockData);
}
}
},
PAGE_10 {
@Override
public int getPos() {
return 19;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 10));
swItem.getItemStack().setAmount(10);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (10 <= pages) {
lectern.setPage(10);
parent.update(blockData);
}
}
},
PAGE_11 {
@Override
public int getPos() {
return 20;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 11));
swItem.getItemStack().setAmount(11);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (11 <= pages) {
lectern.setPage(11);
parent.update(blockData);
}
}
},
PAGE_12 {
@Override
public int getPos() {
return 21;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 12));
swItem.getItemStack().setAmount(12);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (12 <= pages) {
lectern.setPage(12);
parent.update(blockData);
}
}
},
PAGE_13 {
@Override
public int getPos() {
return 23;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 13));
swItem.getItemStack().setAmount(13);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (13 <= pages) {
lectern.setPage(13);
parent.update(blockData);
}
}
},
PAGE_14 {
@Override
public int getPos() {
return 24;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 14));
swItem.getItemStack().setAmount(14);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (14 <= pages) {
lectern.setPage(14);
parent.update(blockData);
}
}
},
PAGE_15 {
@Override
public int getPos() {
return 25;
}
@Override
public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 15));
swItem.getItemStack().setAmount(15);
return swItem;
}
@Override
public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
if (!blockData.hasBook()) return;
Lectern lectern = (Lectern) location.getBlock().getState();
ItemStack itemStack = lectern.getInventory().getItem(0);
if (itemStack == null) return;
int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
if (15 <= pages) {
lectern.setPage(15);
parent.update(blockData);
}
}
}
}
@Override
public SWItem menu(Player player) {
return new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", player));
}
@Override
public boolean checkBlockInWorld() {
return location.getBlock().getType() == Material.LECTERN;
}
}
@@ -0,0 +1,119 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.type.Switch;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public class LoaderLever extends LoaderInteractionElement<LoaderLever.LeverSettingsEnum> {
public LoaderLever(Location location) {
super(location, LeverSettingsEnum.INTERACT, LeverSettingsEnum.NOOP, LeverSettingsEnum.values());
}
public enum LeverSettingsEnum implements LoaderSettingsEnum<Switch, LoaderLever, LeverSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderLever parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_SWITCH", "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, Switch blockData, LoaderLever parent, int power, long ticks, Consumer<Long> delay) {
}
},
INTERACT {
@Override
public int getPos() {
return 3;
}
@Override
public SWItem menu(Player player, LoaderLever parent, int power, long ticks) {
return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_SWITCH", "LOADER_INTERACTION_INTERACT", player));
}
@Override
public void execute(Location location, Switch blockData, LoaderLever parent, int power, long ticks, Consumer<Long> delay) {
blockData.setPowered(!blockData.isPowered());
parent.update(blockData);
}
},
POWER_OFF {
@Override
public int getPos() {
return 5;
}
@Override
public SWItem menu(Player player, LoaderLever parent, int power, long ticks) {
return new SWItem(Material.LEVER, translateItemName("LOADER_BUTTON_SWITCH", "LOADER_INTERACTION_INACTIVE", player));
}
@Override
public void execute(Location location, Switch blockData, LoaderLever parent, int power, long ticks, Consumer<Long> delay) {
blockData.setPowered(false);
parent.update(blockData);
}
},
POWER_ON {
@Override
public int getPos() {
return 6;
}
@Override
public SWItem menu(Player player, LoaderLever parent, int power, long ticks) {
return new SWItem(Material.LEVER, translateItemName("LOADER_BUTTON_SWITCH", "LOADER_INTERACTION_ACTIVE", player));
}
@Override
public void execute(Location location, Switch blockData, LoaderLever parent, int power, long ticks, Consumer<Long> delay) {
blockData.setPowered(true);
}
}
}
@Override
public SWItem menu(Player player) {
return new SWItem(Material.LEVER, translateItemName("LOADER_BUTTON_LECTERN", player));
}
@Override
public boolean checkBlockInWorld() {
return location.getBlock().getType() == Material.LEVER;
}
}
@@ -0,0 +1,165 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.AnaloguePowerable;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Powerable;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public class LoaderMovement extends LoaderInteractionElement<LoaderMovement.MovementSettingsEnum> {
private String name;
private Material material;
private boolean analogue;
public LoaderMovement(Location location, String name, Material material) {
super(location, MovementSettingsEnum.NO_WAIT_FOR, MovementSettingsEnum.NOOP, MovementSettingsEnum.values());
this.name = name;
this.material = material;
this.analogue = location.getBlock().getBlockData() instanceof AnaloguePowerable;
}
public void setInitialTicks(long ticks) {
if (ticks < 1) ticks = 1;
extraTicks.set(currentShot, ticks);
}
public enum MovementSettingsEnum implements LoaderSettingsEnum<BlockData, LoaderMovement, MovementSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderMovement parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName(parent.name, "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, BlockData blockData, LoaderMovement parent, int power, long ticks, Consumer<Long> delay) {
}
},
NO_WAIT_FOR {
@Override
public int getPos() {
return 5;
}
@Override
public SWItem menu(Player player, LoaderMovement parent, int power, long ticks) {
SWItem swItem = new SWItem(parent.material, translateItemName(parent.name, "LOADER_INTERACTION_WAIT_FOR", player));
swItem.getItemStack().setAmount((int) Math.min(ticks, 64));
return swItem;
}
@Override
public boolean hasPower(LoaderMovement parent) {
return parent.analogue;
}
@Override
public boolean hasTicks(LoaderMovement parent) {
return true;
}
@Override
public void execute(Location location, BlockData blockData, LoaderMovement parent, int power, long ticks, Consumer<Long> delay) {
if (blockData instanceof AnaloguePowerable) {
AnaloguePowerable analoguePowerable = (AnaloguePowerable) location.getBlock().getBlockData();
analoguePowerable.setPower(power);
parent.update(analoguePowerable);
} else if (blockData instanceof Powerable) {
Powerable powerable = (Powerable) location.getBlock().getBlockData();
if (ticks < 0) {
powerable.setPowered(power > 0);
} else {
powerable.setPowered(true);
}
parent.update(powerable);
}
BauSystem.runTaskLater(BauSystem.getInstance(), () -> {
if (blockData instanceof AnaloguePowerable) {
AnaloguePowerable analoguePowerable = (AnaloguePowerable) blockData;
analoguePowerable.setPower(0);
parent.update(analoguePowerable);
} else {
Powerable powerable = (Powerable) blockData;
powerable.setPowered(false);
parent.update(powerable);
}
}, ticks);
}
},
WAIT_FOR {
@Override
public int getPos() {
return 6;
}
@Override
public SWItem menu(Player player, LoaderMovement parent, int power, long ticks) {
SWItem swItem = new SWItem(parent.material, translateItemName(parent.name, "LOADER_INTERACTION_NO_WAIT_FOR", player));
swItem.getItemStack().setAmount((int) Math.min(ticks, 64));
return swItem;
}
@Override
public boolean hasPower(LoaderMovement parent) {
return parent.analogue;
}
@Override
public boolean hasTicks(LoaderMovement parent) {
return true;
}
@Override
public void execute(Location location, BlockData blockData, LoaderMovement parent, int power, long ticks, Consumer<Long> delay) {
NO_WAIT_FOR.execute(location, blockData, parent, power, ticks, delay);
delay.accept(ticks);
}
}
}
@Override
public SWItem menu(Player player) {
return new SWItem(material, translateItemName(name, player));
}
@Override
public boolean checkBlockInWorld() {
if (material == Material.STRING && location.getBlock().getType() == Material.TRIPWIRE) return true;
return location.getBlock().getType() == material;
}
}
@@ -0,0 +1,87 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Instrument;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.type.NoteBlock;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public class LoaderNoteBlock extends LoaderInteractionElement<LoaderNoteBlock.NoteBlockSettingsEnum> {
public LoaderNoteBlock(Location location) {
super(location, NoteBlockSettingsEnum.INTERACT, NoteBlockSettingsEnum.NOOP, NoteBlockSettingsEnum.values());
}
public enum NoteBlockSettingsEnum implements LoaderSettingsEnum<NoteBlock, LoaderNoteBlock, NoteBlockSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderNoteBlock parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_NOTEBLOCK", "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, NoteBlock blockData, LoaderNoteBlock parent, int power, long ticks, Consumer<Long> delay) {
}
},
INTERACT {
@Override
public int getPos() {
return 3;
}
@Override
public SWItem menu(Player player, LoaderNoteBlock parent, int power, long ticks) {
return new SWItem(Material.NOTE_BLOCK, translateItemName("LOADER_BUTTON_NOTEBLOCK", "LOADER_INTERACTION_INTERACT", player));
}
@Override
public void execute(Location location, NoteBlock blockData, LoaderNoteBlock parent, int power, long ticks, Consumer<Long> delay) {
if (blockData.getInstrument() == Instrument.BANJO) blockData.setInstrument(Instrument.BIT);
else blockData.setInstrument(Instrument.BANJO);
parent.update(blockData);
}
}
}
@Override
public SWItem menu(Player player) {
return new SWItem(Material.NOTE_BLOCK, translateItemName("LOADER_BUTTON_NOTEBLOCK", player));
}
@Override
public boolean checkBlockInWorld() {
return location.getBlock().getType() == Material.NOTE_BLOCK;
}
}
@@ -0,0 +1,125 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.Openable;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public class LoaderOpenable extends LoaderInteractionElement<LoaderOpenable.OpenableSettingsEnum> {
private String name;
private Material material;
public LoaderOpenable(Location location, String name, Material material) {
super(location, OpenableSettingsEnum.INTERACT, OpenableSettingsEnum.NOOP, OpenableSettingsEnum.values());
this.name = name;
this.material = material;
}
public enum OpenableSettingsEnum implements LoaderSettingsEnum<Openable, LoaderOpenable, OpenableSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderOpenable parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName(parent.name, "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, Openable blockData, LoaderOpenable parent, int power, long ticks, Consumer<Long> delay) {
}
},
INTERACT {
@Override
public int getPos() {
return 3;
}
@Override
public SWItem menu(Player player, LoaderOpenable parent, int power, long ticks) {
return new SWItem(Material.STICK, translateItemName(parent.name, "LOADER_INTERACTION_INTERACT", player));
}
@Override
public void execute(Location location, Openable blockData, LoaderOpenable parent, int power, long ticks, Consumer<Long> delay) {
blockData.setOpen(!blockData.isOpen());
parent.update(blockData);
}
},
CLOSED {
@Override
public int getPos() {
return 5;
}
@Override
public SWItem menu(Player player, LoaderOpenable parent, int power, long ticks) {
return new SWItem(parent.material, translateItemName(parent.name, "LOADER_INTERACTION_CLOSED", player));
}
@Override
public void execute(Location location, Openable blockData, LoaderOpenable parent, int power, long ticks, Consumer<Long> delay) {
blockData.setOpen(false);
parent.update(blockData);
}
},
OPEN {
@Override
public int getPos() {
return 6;
}
@Override
public SWItem menu(Player player, LoaderOpenable parent, int power, long ticks) {
return new SWItem(parent.material, translateItemName(parent.name, "LOADER_INTERACTION_OPEN", player));
}
@Override
public void execute(Location location, Openable blockData, LoaderOpenable parent, int power, long ticks, Consumer<Long> delay) {
blockData.setOpen(true);
parent.update(blockData);
}
}
}
@Override
public SWItem menu(Player player) {
return new SWItem(material, translateItemName(name, player));
}
@Override
public boolean checkBlockInWorld() {
return location.getBlock().getType() == material;
}
}
@@ -0,0 +1,169 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.type.Repeater;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public class LoaderRepeater extends LoaderInteractionElement<LoaderRepeater.RepeaterSettingsEnum> {
public LoaderRepeater(Location location) {
super(location, RepeaterSettingsEnum.INTERACT, RepeaterSettingsEnum.NOOP, RepeaterSettingsEnum.values());
}
public enum RepeaterSettingsEnum implements LoaderSettingsEnum<Repeater, LoaderRepeater, RepeaterSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 1;
}
@Override
public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_REPEATER", "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
}
},
INTERACT {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_REPEATER", "LOADER_INTERACTION_INTERACT", player));
}
@Override
public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
int repeaterDelay = blockData.getDelay();
repeaterDelay++;
if (repeaterDelay > 4) repeaterDelay = 1;
blockData.setDelay(repeaterDelay);
parent.update(blockData);
}
},
DELAY_1 {
@Override
public int getPos() {
return 4;
}
@Override
public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_SETTING_REPEATER", player, 1));
swItem.getItemStack().setAmount(1);
return swItem;
}
@Override
public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
blockData.setDelay(1);
parent.update(blockData);
}
},
DELAY_2 {
@Override
public int getPos() {
return 5;
}
@Override
public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_SETTING_REPEATER", player, 2));
swItem.getItemStack().setAmount(2);
return swItem;
}
@Override
public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
blockData.setDelay(2);
parent.update(blockData);
}
},
DELAY_3 {
@Override
public int getPos() {
return 6;
}
@Override
public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_SETTING_REPEATER", player, 3));
swItem.getItemStack().setAmount(3);
return swItem;
}
@Override
public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
blockData.setDelay(3);
parent.update(blockData);
}
},
DELAY_4 {
@Override
public int getPos() {
return 7;
}
@Override
public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
SWItem swItem = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_SETTING_REPEATER", player, 4));
swItem.getItemStack().setAmount(4);
return swItem;
}
@Override
public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
blockData.setDelay(4);
parent.update(blockData);
}
}
}
@Override
public SWItem menu(Player player) {
return new SWItem(Material.REPEATER, translateItemName("LOADER_BUTTON_REPEATER", player));
}
@Override
public boolean checkBlockInWorld() {
return location.getBlock().getType() == Material.REPEATER;
}
}
@@ -0,0 +1,98 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.function.Consumer;
public class LoaderTNT extends LoaderInteractionElement<LoaderTNT.TNTSettingsEnum> {
public LoaderTNT(Location location) {
super(location, TNTSettingsEnum.PLACE, TNTSettingsEnum.NOOP, TNTSettingsEnum.values());
}
public enum TNTSettingsEnum implements LoaderSettingsEnum<BlockData, LoaderTNT, TNTSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderTNT parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_TNT", "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, BlockData blockData, LoaderTNT parent, int power, long ticks, Consumer<Long> delay) {
}
},
PLACE {
@Override
public int getPos() {
return 3;
}
@Override
public SWItem menu(Player player, LoaderTNT parent, int power, long ticks) {
return new SWItem(Material.TNT, translateItemName("LOADER_BUTTON_TNT", "LOADER_INTERACTION_PLACE", player));
}
@Override
public void execute(Location location, BlockData blockData, LoaderTNT parent, int power, long ticks, Consumer<Long> delay) {
location.getBlock().setType(Material.TNT);
}
}
}
@Override
public SWItem menu(Player player) {
SWItem item = new SWItem(Material.TNT, BauSystem.MESSAGE.parse("LOADER_SETTING_TNT_NAME", player));
item.setLore(Arrays.asList(BauSystem.MESSAGE.parse("LOADER_SETTING_TNT_X", player, location.getBlockX()), BauSystem.MESSAGE.parse("LOADER_SETTING_TNT_Y", player, location.getBlockY()), BauSystem.MESSAGE.parse("LOADER_SETTING_TNT_Z", player, location.getBlockZ())));
return item;
}
@Override
public void execute(Consumer<Long> delay) {
Block block = location.getBlock();
if (block.getType() != Material.AIR && block.getType() != Material.WATER) {
delay.accept(1L);
return;
}
super.execute(delay);
}
@Override
public boolean checkBlockInWorld() {
return true;
}
}
@@ -0,0 +1,116 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.Powerable;
import org.bukkit.entity.Player;
import java.util.function.Consumer;
public class LoaderTicks extends LoaderInteractionElement<LoaderTicks.TicksSettingsEnum> {
private String name;
private Material material;
private int ticks;
public LoaderTicks(Location location, String name, Material material, int ticks) {
super(location, TicksSettingsEnum.NO_WAIT_FOR, TicksSettingsEnum.NOOP, TicksSettingsEnum.values());
this.name = name;
this.material = material;
this.ticks = ticks;
}
public enum TicksSettingsEnum implements LoaderSettingsEnum<Powerable, LoaderTicks, TicksSettingsEnum> {
NOOP {
@Override
public int getPos() {
return 2;
}
@Override
public SWItem menu(Player player, LoaderTicks parent, int power, long ticks) {
return new SWItem(Material.STRUCTURE_VOID, translateItemName(parent.name, "LOADER_INTERACTION_NOOP", player));
}
@Override
public void execute(Location location, Powerable blockData, LoaderTicks parent, int power, long ticks, Consumer<Long> delay) {
}
},
NO_WAIT_FOR {
@Override
public int getPos() {
return 5;
}
@Override
public SWItem menu(Player player, LoaderTicks parent, int power, long ticks) {
return new SWItem(parent.material, translateItemName(parent.name, "LOADER_INTERACTION_NO_WAIT_FOR", player, ticks));
}
@Override
public void execute(Location location, Powerable blockData, LoaderTicks parent, int power, long ticks, Consumer<Long> delay) {
Powerable powerable = (Powerable) location.getBlock().getBlockData();
powerable.setPowered(true);
parent.update(powerable);
BauSystem.runTaskLater(BauSystem.getInstance(), () -> {
powerable.setPowered(false);
parent.update(powerable);
}, parent.ticks);
}
},
WAIT_FOR {
@Override
public int getPos() {
return 6;
}
@Override
public SWItem menu(Player player, LoaderTicks parent, int power, long ticks) {
return new SWItem(parent.material, translateItemName(parent.name, "LOADER_INTERACTION_WAIT_FOR", player, ticks));
}
@Override
public void execute(Location location, Powerable blockData, LoaderTicks parent, int power, long ticks, Consumer<Long> delay) {
NO_WAIT_FOR.execute(location, blockData, parent, power, ticks, delay);
delay.accept((long) parent.ticks);
}
}
}
@Override
public SWItem menu(Player player) {
return new SWItem(material, translateItemName(name, player));
}
@Override
public boolean checkBlockInWorld() {
return location.getBlock().getType() == material;
}
}
@@ -0,0 +1,89 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.loader.elements.impl;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.loader.elements.LoaderElement;
import de.steamwar.inventory.SWAnvilInv;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.function.Consumer;
public class LoaderWait implements LoaderElement {
@Getter
@Setter
private long delay;
public LoaderWait(long delay) {
this.delay = delay;
}
@Override
public SWItem menu(Player player) {
SWItem swItem = new SWItem(Material.CLOCK, BauSystem.MESSAGE.parse("LOADER_SETTING_WAIT", player, delay));
swItem.getItemStack().setAmount((int) Math.max(Math.min(delay, 64), 1));
if (delay == 0) swItem.setEnchanted(true);
swItem.setLore(Arrays.asList(BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", player)));
return swItem;
}
@Override
public void execute(Consumer<Long> wait) {
wait.accept(delay);
}
@Override
public void click(Player player, Runnable backAction) {
SWInventory swInventory = new SWInventory(player, 18, BauSystem.MESSAGE.parse("LOADER_GUI_WAIT_TITLE", player));
for (int i = 9; i < 18; i++) swInventory.setItem(i, new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7"));
swInventory.setItem(9, new SWItem(Material.ARROW, BauSystem.MESSAGE.parse("LOADER_GUI_WAIT_BACK", player)).getItemStack(), clickType -> backAction.run());
swInventory.setItem(3, new SWItem(SWItem.getDye(1), BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_REMOVE_ONE", player), Arrays.asList(BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_REMOVE_ONE_SHIFT", player)), false, clickType -> {}).getItemStack(), clickType -> {
delay -= clickType.isShiftClick() ? 5 : 1;
if (delay < 0) delay = 0;
swInventory.setItem(4, menu(player));
});
swInventory.setItem(4, menu(player).getItemStack(), clickType -> {
SWAnvilInv swAnvilInv = new SWAnvilInv(player, BauSystem.MESSAGE.parse("LOADER_SETTING_WAIT_NAME", player), delay + "");
swAnvilInv.setCallback(s -> {
try {
delay = Long.parseLong(s);
if (delay < 0) delay = 0;
} catch (NumberFormatException ignored) {
}
click(player, backAction);
});
swAnvilInv.open();
});
swInventory.setItem(5, new SWItem(SWItem.getDye(10), BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_ADD_ONE", player), Arrays.asList(BauSystem.MESSAGE.parse("LOADER_SETTING_TICKS_ADD_ONE_SHIFT", player)), false, clickType -> {}).getItemStack(), clickType -> {
delay += clickType.isShiftClick() ? 5 : 1;
swInventory.setItem(4, menu(player));
});
swInventory.open();
}
}
@@ -0,0 +1,234 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.loadtimer;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scheduler.BukkitTask;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class Loadtimer implements Listener {
@Getter
private static final Map<Region, Loadtimer> timers = new HashMap<>();
private final Region region;
private final Map<Player, Integer> tntPlaced = new HashMap<>();
private final BukkitTask task;
private final Map<Player, BossBar> bars = new HashMap<>();
private Stage stage;
private final boolean finishOnActive;
private long start = -1;
private long activate = -1;
private long ignite = -1;
private long explode = -1;
public Loadtimer(Region region, boolean finishOnActive) {
this.finishOnActive = finishOnActive;
this.region = region;
this.stage = Stage.WAITING;
Bukkit.getPluginManager().registerEvents(this, BauSystem.getInstance());
task = BauSystem.runTaskTimer(BauSystem.getInstance(), () -> {
if (stage == Stage.COUNTING) {
long timeSinceStart = TPSUtils.currentRealTick.get() - start;
long timeSinceHalf = timeSinceStart / 2;
double timeSec = (timeSinceStart / 20d);
String sec = new DecimalFormat("#.#").format(timeSec);
AtomicInteger tnt = new AtomicInteger();
tntPlaced.forEach((player, integer) -> tnt.addAndGet(integer));
sendBarToRegion(BarStyle.SEGMENTED_20, "LOADTIMER_BOSSBAR", BarColor.GREEN, (timeSinceStart % 20d) / 20, timeSinceStart, timeSinceHalf, sec, tnt);
} else if (stage == Stage.WAITING) {
sendBarToRegion(BarStyle.SOLID, "LOADTIMER_WAITING", BarColor.GREEN, 1);
} else if (stage == Stage.ACTIVATED || stage == Stage.IGNITION) {
sendBarToRegion(BarStyle.SOLID, stage == Stage.ACTIVATED ? "LOADTIMER_ACTIVATED" : "LOADTIMER_IGNITION", BarColor.YELLOW, 1);
}
}, 1, 1);
}
private void sendBarToRegion(BarStyle style, String message, BarColor color, double progress, Object... objects) {
Bukkit.getOnlinePlayers().forEach(player -> {
if (Region.getRegion(player.getLocation()).equals(region)) {
BossBar bar = getOrDefault(player);
bar.setStyle(style);
bar.setTitle(BauSystem.MESSAGE.parse(message, player, objects));
bar.setColor(color);
bar.setProgress(progress);
} else if (bars.containsKey(player)) {
bars.remove(player).removeAll();
}
});
}
public static boolean hasTimer(Region r) {
return timers.containsKey(r);
}
public static Loadtimer getTimer(Region r) {
return timers.get(r);
}
public static Loadtimer createLoadtimer(Region r, boolean finishOnActive) {
return timers.computeIfAbsent(r, region1 -> new Loadtimer(region1, finishOnActive));
}
private BossBar getOrDefault(Player player) {
return bars.computeIfAbsent(player, player1 -> {
BossBar bar1 = Bukkit.createBossBar("%PLACEHOLDER%", BarColor.GREEN, BarStyle.SEGMENTED_20);
bar1.addPlayer(player1);
return bar1;
});
}
public void onTntPlace(BlockPlaceEvent event) {
if (stage == Stage.WAITING) {
this.stage = Stage.COUNTING;
this.start = TPSUtils.currentRealTick.get();
}
if (stage == Stage.COUNTING) {
tntPlaced.put(event.getPlayer(), tntPlaced.getOrDefault(event.getPlayer(), 0) + 1);
}
}
public void onInteract(PlayerInteractEvent event) {
if (stage == Stage.COUNTING) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Material type = event.getClickedBlock().getType();
if (type == Material.IRON_TRAPDOOR) {
return;
}
if (type.name().contains("_BUTTON") || type == Material.LEVER || type.name().contains("_TRAPDOOR") || type == Material.NOTE_BLOCK) {
setActivate();
}
} else if (event.getAction() == Action.PHYSICAL) {
setActivate();
}
}
}
public void onTntSpawn() {
if ((stage == Stage.COUNTING || stage == Stage.ACTIVATED)) {
stage = Stage.IGNITION;
ignite = TPSUtils.currentRealTick.get();
if (activate == -1)
activate = TPSUtils.currentRealTick.get();
if (finishOnActive) {
stage = Stage.END;
print();
delete();
}
}
}
public void onTntExplode(EntityExplodeEvent event) {
if (region.inRegion(event.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION) && stage == Stage.IGNITION) {
stage = Stage.END;
explode = TPSUtils.currentRealTick.get();
print();
delete();
}
}
private void setActivate() {
activate = TPSUtils.currentRealTick.get();
stage = Stage.ACTIVATED;
if (finishOnActive) {
print();
delete();
}
}
public void print() {
long loadTime = activate - start;
int allTnt = 0;
for (Map.Entry<Player, Integer> e : tntPlaced.entrySet()) {
allTnt += e.getValue();
}
long ignTime = ignite - activate;
long explTime = explode - ignTime - activate;
if (explTime < 0)
explTime = loadTime;
int finalAllTnt = allTnt;
long finalExplTime = explTime;
Bukkit.getOnlinePlayers().forEach(player -> {
if (Region.getRegion(player.getLocation()).equals(region)) {
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_HEAD", player);
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_PLAYERTABLE_HEAD", player);
for (Map.Entry<Player, Integer> e : tntPlaced.entrySet()) {
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_PLAYERTABLE_PLAYER", player, e.getKey().getName(), e.getValue(), new DecimalFormat("#.#").format(e.getValue() / (loadTime / 20D)));
}
if (tntPlaced.size() > 1) {
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_PLAYERTABLE_PLAYER", player, BauSystem.MESSAGE.parse("LOADTIMER_SUMARY_PLAYERTABLE_ALL", player), finalAllTnt, new DecimalFormat("#.#").format(finalAllTnt / (loadTime / 20D)));
}
player.sendMessage("");
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_TIMES_HEAD", player);
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_TIMES_START", player);
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_TIMES_ACTIVATION", player, new DecimalFormat("#.#").format((loadTime / 20D)), loadTime);
if (!finishOnActive) {
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_TIMES_IGNITION", player, new DecimalFormat("#.#").format((ignTime / 20D)), ignTime);
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_TIMES_EXPLOSION", player, new DecimalFormat("#.#").format((finalExplTime / 20D)), finalExplTime);
}
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_TIMES_LAST", player);
player.sendMessage("");
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_STATS_HEAD", player);
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_STATS_TNT", player, finalAllTnt);
BauSystem.MESSAGE.sendPrefixless("LOADTIMER_SUMARY_STATS_FREQ", player, 60D / (loadTime / 20D), 60D / ((finalExplTime + Math.max(ignTime, 0) + loadTime) / 20D));
}
});
}
public void delete() {
HandlerList.unregisterAll(this);
timers.remove(region, this);
bars.forEach((player, bossBar) -> bossBar.removeAll());
bars.clear();
task.cancel();
}
private enum Stage {
WAITING,
COUNTING,
ACTIVATED,
IGNITION,
END
}
}
@@ -0,0 +1,59 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.loadtimer;
import de.steamwar.bausystem.region.Region;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class LoadtimerCommand extends SWCommand {
public LoadtimerCommand() {
super("loadtimer", "lt", "stopuhr");
addDefaultHelpMessage("LOADTIMER_HELP_OVERVIEW");
}
@Register(value = "start", description = "LOADTIMER_HELP_START_1")
public void start(@Validator Player p) {
start(p, TimerMode.HALF);
}
@Register(value = "start", description = {"LOADTIMER_HELP_START_2", "LOADTIMER_HELP_START_3"})
public void start(@Validator Player p, TimerMode mode) {
Region r = Region.getRegion(p.getLocation());
if (r.isGlobal()) return;
if (!Loadtimer.hasTimer(r))
Loadtimer.createLoadtimer(r, mode == TimerMode.HALF);
}
@Register(value = "stop", description = "LOADTIMER_HELP_STOP")
public void stop(@Validator Player p) {
Region r = Region.getRegion(p.getLocation());
if (r.isGlobal()) return;
if (Loadtimer.hasTimer(r))
Loadtimer.getTimer(r).delete();
}
public enum TimerMode {
FULL,
HALF
}
}
@@ -0,0 +1,76 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.loadtimer;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class LoadtimerGuiItem extends BauGuiItem {
public LoadtimerGuiItem() {
super(28);
}
@Override
public Permission permission() {
return Permission.MEMBER;
}
@Override
public ItemStack getItem(Player player) {
Region r = Region.getRegion(player.getLocation());
if (r.isGlobal())
return new SWItem(Material.BOWL, BauSystem.MESSAGE.parse("LOADTIMER_GUI_GLOBAL", player)).getItemStack();
if (Loadtimer.hasTimer(r)) {
return new SWItem(Material.BOW, BauSystem.MESSAGE.parse("LOADTIMER_GUI_STOP", player)).getItemStack();
} else {
return new SWItem(Material.BOW, BauSystem.MESSAGE.parse("LOADTIMER_GUI_START", player)).getItemStack();
}
}
@Override
public boolean click(ClickType click, Player p) {
Region r = Region.getRegion(p.getLocation());
if (r.isGlobal()) return false;
if (Loadtimer.hasTimer(r)) {
p.performCommand("lt stop");
} else {
SWInventory inv = new SWInventory(p, 9, BauSystem.MESSAGE.parse("LOADTIMER_GUI_TITLE", p));
inv.setItem(1, Material.OAK_PLANKS, BauSystem.MESSAGE.parse("LOADTIMER_GUI_FULL", p), clickType -> {
p.performCommand("lt start full");
});
inv.setItem(7, Material.OAK_SLAB, BauSystem.MESSAGE.parse("LOADTIMER_GUI_HALF", p), clickType -> {
p.performCommand("lt start half");
});
inv.open();
}
return true;
}
}
@@ -0,0 +1,77 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.loadtimer;
import de.steamwar.bausystem.region.Region;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import static de.steamwar.bausystem.features.loadtimer.Loadtimer.*;
@Linked
public class LoadtimerListener implements Listener {
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
if (!getTimers().isEmpty() && event.getBlockPlaced().getType() == Material.TNT) {
Region r = Region.getRegion(event.getBlock().getLocation());
if (hasTimer(r)) {
getTimer(r).onTntPlace(event);
}
}
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (!getTimers().isEmpty()) {
Region r = Region.getRegion(event.getPlayer().getLocation());
if (hasTimer(r)) {
getTimer(r).onInteract(event);
}
}
}
@EventHandler
public void onEntitySpawn(EntitySpawnEvent event) {
if (!getTimers().isEmpty() && event.getEntityType() == EntityType.PRIMED_TNT) {
Region r = Region.getRegion(event.getLocation());
if (hasTimer(r)) {
getTimer(r).onTntSpawn();
}
}
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
if (!getTimers().isEmpty() && event.getEntityType() == EntityType.PRIMED_TNT) {
Region r = Region.getRegion(event.getLocation());
if (hasTimer(r)) {
getTimer(r).onTntExplode(event);
}
}
}
}
@@ -0,0 +1,272 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.observer;
import com.comphenix.tinyprotocol.Reflection;
import de.steamwar.bausystem.region.Point;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Bisected;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Observer;
import org.bukkit.block.data.type.*;
import org.bukkit.entity.Player;
import java.util.*;
public class ObserverTracer {
private static final Set<BlockFace> ALLOWED = EnumSet.of(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST, BlockFace.UP, BlockFace.DOWN);
private Player player;
private Block block;
Set<Point> seen = new HashSet<>();
private List<Block> blockList = new ArrayList<>();
public ObserverTracer(Player player, Block block) {
this.player = player;
this.block = block;
}
public void show() {
if (!ObserverTracerListener.enabled.contains(player)) {
return;
}
for (Point p : seen) {
Location location = p.toLocation(player);
spawnParticle(player, location, location.getBlock());
}
}
public boolean trace() {
int size = seen.size();
seen.clear();
blockList.clear();
if (block.getType() != Material.OBSERVER) {
return seen.size() != size;
}
blockList.add(block);
while (!blockList.isEmpty()) {
Block b = blockList.remove(0);
Point point = Point.fromLocation(b.getLocation());
if (seen.contains(point)) {
continue;
}
seen.add(point);
spawnParticle(player, b.getLocation(), b);
if (b.getType() == Material.OBSERVER) {
calculateObserver(b);
continue;
}
BlockData blockData = b.getBlockData();
if (blockData instanceof Fence) {
calculateSimple(b);
continue;
}
if (blockData instanceof Door) {
if (((Door) blockData).getHalf() == Bisected.Half.BOTTOM) {
blockList.add(b.getLocation().add(0, 1, 0).getBlock());
} else {
blockList.add(b.getLocation().add(0, -1, 0).getBlock());
}
}
if (checkAllowed(b, blockData)) {
calculateSpecial(b);
continue;
}
if (b.getType().isBlock() && b.getType().isSolid() && b.getType().isOccluding()) {
calculateSolidBlock(b);
}
}
return seen.size() != size;
}
private void spawnParticle(Player player, Location location, Block block) {
player.spawnParticle(Particle.FLAME, location.clone().add(0.5, 0.5, 0.5), 1, 0, 0, 0, 0);
if (block.getType() == Material.OBSERVER) {
BlockFace blockFace = ((Observer) block.getBlockData()).getFacing();
blockFace = blockFace.getOppositeFace();
player.spawnParticle(Particle.FLAME, location.clone().add(0.5 + blockFace.getModX() / 5.0, 0.5 + blockFace.getModY() / 5.0, 0.5 + blockFace.getModZ() / 5.0), 1, 0, 0, 0, 0);
}
}
private void calculateObserver(Block block) {
// TODO: Implement single Repeater, single Redstone, DetectorRail, PoweredRail, Diagonal Piston
Observer observer = (Observer) block.getBlockData();
for (BlockFace blockFace : ALLOWED) {
Location location = block.getLocation().add(blockFace.getModX(), blockFace.getModY(), blockFace.getModZ());
Block b = location.getBlock();
if (blockFace == observer.getFacing().getOppositeFace() && !b.getType().isAir() && b.getType() != Material.OBSERVER) {
blockList.add(b);
if (checkMaterial(b)) {
calculateSolidBlock(b);
}
continue;
}
if (b.getType() != Material.OBSERVER) {
continue;
}
if (((Observer) b.getBlockData()).getFacing() == blockFace.getOppositeFace()) {
blockList.add(b);
}
}
if (observer.getFacing() == BlockFace.UP) {
Location location = block.getLocation();
location.add(0, -2, 0);
if (location.getY() >= 0) {
Block b = location.getBlock();
if (b.getType() == Material.DROPPER || b.getType() == Material.DISPENSER) {
blockList.add(b);
}
}
}
if (observer.getFacing() != BlockFace.UP && observer.getFacing() != BlockFace.DOWN) {
Location location = block.getLocation();
BlockFace blockFace = observer.getFacing().getOppositeFace();
location.add(blockFace.getModX(), -1, blockFace.getModZ());
if (location.getY() >= 0) {
Block b = location.getBlock();
if (b.getType() == Material.DROPPER || b.getType() == Material.DISPENSER) {
blockList.add(b);
}
}
}
}
private void calculateSolidBlock(Block block) {
for (BlockFace blockFace : ALLOWED) {
Location location = block.getLocation().add(blockFace.getModX(), blockFace.getModY(), blockFace.getModZ());
Block b = location.getBlock();
if (checkAllowed(b, b.getBlockData())) {
blockList.add(b);
}
}
}
private static final Class<?> craftPoweredRail = Reflection.getClass("{obc}.block.impl.CraftPoweredRail");
private boolean checkAllowed(Block block, BlockData blockData) {
if (checkMaterial(block)) return true;
if (block.getType() == Material.BELL) {
return true;
}
return blockData instanceof Door
|| blockData instanceof Gate
|| craftPoweredRail.isInstance(blockData)
|| blockData instanceof TrapDoor
|| blockData instanceof GlassPane;
}
private boolean checkMaterial(Block block) {
if (block.getType() == Material.DROPPER) {
return true;
}
if (block.getType() == Material.HOPPER) {
return true;
}
if (block.getType() == Material.DISPENSER) {
return true;
}
if (block.getType() == Material.REDSTONE_LAMP) {
return true;
}
if (block.getType() == Material.NOTE_BLOCK) {
return true;
}
return false;
}
private void calculateSpecial(Block block) {
BlockData blockData = block.getBlockData();
Location blockLocation = block.getLocation();
if (blockData instanceof TrapDoor) {
TrapDoor trapDoor = (TrapDoor) blockData;
fence(trapDoor.getFacing().getOppositeFace(), blockLocation);
}
if (blockData instanceof Door) {
Door door = (Door) blockData;
fence(door.getFacing().getOppositeFace(), blockLocation);
fence(doorHinge(door.getFacing(), door.getHinge()), blockLocation);
}
calculateSimple(block);
}
private void fence(BlockFace current, Location location) {
Block b = location.clone().add(current.getModX(), current.getModY(), current.getModZ()).getBlock();
if (b.getBlockData() instanceof Fence || b.getBlockData() instanceof GlassPane) {
blockList.add(b);
}
}
private BlockFace doorHinge(BlockFace blockFace, Door.Hinge hinge) {
switch (blockFace) {
case WEST:
if (hinge == Door.Hinge.RIGHT) {
return BlockFace.NORTH;
} else {
return BlockFace.SOUTH;
}
case SOUTH:
if (hinge == Door.Hinge.RIGHT) {
return BlockFace.WEST;
} else {
return BlockFace.EAST;
}
case EAST:
if (hinge == Door.Hinge.RIGHT) {
return BlockFace.SOUTH;
} else {
return BlockFace.NORTH;
}
case NORTH:
if (hinge == Door.Hinge.RIGHT) {
return BlockFace.EAST;
} else {
return BlockFace.WEST;
}
default:
throw new SecurityException();
}
}
private void calculateSimple(Block block) {
Location blockLocation = block.getLocation();
for (BlockFace blockFace : ALLOWED) {
Location location = blockLocation.clone().add(blockFace.getModX(), blockFace.getModY(), blockFace.getModZ());
Block b = location.getBlock();
if (b.getType() == Material.OBSERVER) {
Observer current = (Observer) b.getBlockData();
if (current.getFacing() == blockFace.getOppositeFace()) {
blockList.add(b);
}
}
}
}
}
@@ -0,0 +1,64 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.observer;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class ObserverTracerCommand extends SWCommand {
public ObserverTracerCommand() {
super("observertracer", "observer");
addDefaultHelpMessage("OBSERVER_HELP");
}
@Register(value = "enable", description = "OBSERVER_HELP_ENABLE")
public void enable(Player p) {
ObserverTracerListener.enabled.add(p);
BauSystem.MESSAGE.send("OBSERVER_ENABLE", p);
}
@Register(value = "disable", description = "OBSERVER_HELP_DISABLE")
public void disable(Player p) {
ObserverTracerListener.enabled.remove(p);
ObserverTracerListener.observerTracerMap.remove(p);
BauSystem.MESSAGE.send("OBSERVER_DISABLE", p);
}
@Register(value = "delete", description = "OBSERVER_HELP_DELETE")
public void delete(@Validator Player p) {
ObserverTracerListener.observerTracerMap.remove(p);
BauSystem.MESSAGE.send("OBSERVER_DELETE", p);
}
@Register(value = "retrace", description = "OBSERVER_HELP_RETRACE")
public void retrace(@Validator Player p) {
if (ObserverTracerListener.observerTracerMap.containsKey(p)) {
BauSystem.MESSAGE.send("OBSERVER_RETRACE_NO_TRACE", p);
return;
}
ObserverTracerListener.observerTracerMap.get(p).trace();
BauSystem.MESSAGE.send("OBSERVER_RETRACE_DONE", p);
}
}
@@ -0,0 +1,108 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.observer;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Linked
public class ObserverTracerListener implements Listener {
static Set<Player> enabled = new HashSet<>();
static Map<Player, ObserverTracer> observerTracerMap = new HashMap<>();
public ObserverTracerListener() {
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
observerTracerMap.forEach((player, observerTracer) -> {
if (player.getGameMode() == GameMode.SPECTATOR) {
observerTracer.show();
}
});
}, 15L, 15L);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
if (!enabled.contains(event.getPlayer())) {
return;
}
if (event.getClickedBlock() == null) {
return;
}
if (observerTracerMap.containsKey(event.getPlayer())) {
ObserverTracer observerTracer = observerTracerMap.get(event.getPlayer());
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
if (!observerTracer.trace()) {
createNew(event);
}
observerTracerMap.forEach((player, o) -> {
o.trace();
});
}, 1L);
} else {
createNew(event);
}
}
private void createNew(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
if (event.getClickedBlock().getType() == Material.OBSERVER) {
ObserverTracer observerTracer = new ObserverTracer(event.getPlayer(), event.getClickedBlock());
observerTracer.trace();
observerTracerMap.put(event.getPlayer(), observerTracer);
}
}
@EventHandler
public void onBauMemberUpdate(BauMemberUpdateEvent event) {
event.getNewSpectator().forEach(observerTracerMap::remove);
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
enabled.add(event.getPlayer());
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
enabled.remove(event.getPlayer());
observerTracerMap.remove(event.getPlayer());
}
}
@@ -0,0 +1,96 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.config.BauServer;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ColorMode;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.entity.Player;
import java.io.IOException;
@Linked
public class ColorCommand extends SWCommand {
@LinkedInstance
public BauServer bauServer;
public ColorCommand() {
super("color");
}
@Register(description = "REGION_COLOR_HELP_COLOR")
public void genericColor(Player p, ColorMode color) {
genericColorSet(p, color, ColorizationType.LOCAL);
}
@Register(description = "REGION_COLOR_HELP_COLOR_TYPE")
public void genericColorSet(@Validator Player p, ColorMode color, ColorizationType colorizationType) {
if (colorizationType == ColorizationType.GLOBAL) {
Region.setGlobal(Flag.COLOR, color);
BauSystem.MESSAGE.send("REGION_COLOR_GLOBAL", p, BauSystem.MESSAGE.parse(color.getChatValue(), p));
return;
}
Region region = Region.getRegion(p.getLocation());
if (region.isGlobal()) {
BauSystem.MESSAGE.send("REGION_COLOR_NO_REGION", p);
return;
}
region.set(Flag.COLOR, color);
try {
PasteBuilder pasteBuilder = new PasteBuilder(new PasteBuilder.FileProvider(region.getResetFile(RegionType.NORMAL)))
.ignoreAir(true)
.onlyColors(true)
.color(color.getColor());
region.reset(pasteBuilder, RegionType.NORMAL, RegionExtensionType.NORMAL);
RegionUtils.message(region, "REGION_REGION_COLORED");
RegionUtils.message(region, "REGION_REGION_COLORED_FAILED");
} catch (SecurityException e) {
BauSystem.MESSAGE.send("REGION_REGION_FAILED_COLORED", p);
}
}
@Register
public void genericColorSet(Player p, ColorizationType colorizationType, ColorMode color) {
genericColorSet(p, color, colorizationType);
}
@ClassValidator(value = Player.class, local = true)
public TypeValidator<Player> validator() {
return (commandSender, player, messageSender) -> {
return !messageSender.send(!bauServer.getOwner().equals(player.getUniqueId()), "NO_PERMISSION");
};
}
public enum ColorizationType {
LOCAL,
GLOBAL
}
}
@@ -0,0 +1,68 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.FireMode;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class FireCommand extends SWCommand {
public FireCommand() {
super("fire");
}
@Register(description = "REGION_FIRE_HELP")
public void toggleCommand(@Validator Player p) {
Region region = Region.getRegion(p.getLocation());
if (toggle(region)) {
RegionUtils.actionBar(region, getEnableMessage());
} else {
RegionUtils.actionBar(region, getDisableMessage());
}
}
private String getEnableMessage() {
return "REGION_FIRE_ENABLED";
}
private String getDisableMessage() {
return "REGION_FIRE_DISABLED";
}
private boolean toggle(Region region) {
switch (region.getPlain(Flag.FIRE, FireMode.class)) {
case ALLOW:
region.set(Flag.FIRE, FireMode.DENY);
return true;
default:
case DENY:
region.set(Flag.FIRE, FireMode.ALLOW);
return false;
}
}
}
@@ -0,0 +1,43 @@
package de.steamwar.bausystem.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.FireMode;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockSpreadEvent;
@Linked
public class FireListener implements Listener, ScoreboardElement {
@EventHandler
public void onFireDamage(BlockBurnEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.DENY) e.setCancelled(true);
}
@EventHandler
public void onFireSpread(BlockSpreadEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.DENY) e.setCancelled(true);
}
@Override
public ScoreboardGroup getGroup() {
return ScoreboardGroup.REGION;
}
@Override
public int order() {
return 1;
}
@Override
public String get(Region region, Player p) {
if (region.get(Flag.FIRE) == Flag.FIRE.getDefaultValue()) return null;
return "§e" + BauSystem.MESSAGE.parse(Flag.FIRE.getChatValue(), p) + "§8: " + BauSystem.MESSAGE.parse(region.get(Flag.FIRE).getChatValue(), p);
}
}
@@ -0,0 +1,68 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class FreezeCommand extends SWCommand {
public FreezeCommand() {
super("freeze", "stoplag", "sl");
}
@Register(description = "REGION_FREEZE_HELP")
public void toggleCommand(@Validator Player p) {
Region region = Region.getRegion(p.getLocation());
if (toggle(region)) {
RegionUtils.actionBar(region, getEnableMessage());
} else {
RegionUtils.actionBar(region, getDisableMessage());
}
}
private String getEnableMessage(){
return "REGION_FREEZE_ENABLED";
}
private String getDisableMessage(){
return "REGION_FREEZE_DISABLED";
}
private boolean toggle(Region region) {
switch (region.getPlain(Flag.FREEZE, FreezeMode.class)) {
case ACTIVE:
region.set(Flag.FREEZE, FreezeMode.INACTIVE);
return false;
default:
case INACTIVE:
region.set(Flag.FREEZE, FreezeMode.ACTIVE);
return true;
}
}
}
@@ -0,0 +1,205 @@
package de.steamwar.bausystem.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.core.Core;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.type.Switch;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
@Linked
public class FreezeListener implements Listener, ScoreboardElement {
@EventHandler
public void onEntitySpawn(EntitySpawnEvent e) {
if (Region.getRegion(e.getLocation()).get(Flag.FREEZE) == FreezeMode.INACTIVE) {
return;
}
e.setCancelled(true);
if (e.getEntityType() == EntityType.PRIMED_TNT) {
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
e.getLocation().getBlock().setType(Material.TNT, false);
}, 1L);
}
}
@EventHandler
public void onBlockCanBuild(BlockCanBuildEvent e) {
if (!e.isBuildable()) return;
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.INACTIVE)
return;
if (e.getMaterial() == Material.TNT) {
e.setBuildable(false);
e.getBlock().setType(Material.TNT, false);
}
}
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onPhysicsEvent(BlockPhysicsEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onPistonExtend(BlockPistonExtendEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onPistonRetract(BlockPistonRetractEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onBlockGrow(BlockGrowEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onRedstoneEvent(BlockRedstoneEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setNewCurrent(e.getOldCurrent());
}
}
@EventHandler
public void onBlockDispense(BlockDispenseEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onInventoryMoveEvent(InventoryMoveItemEvent e) {
if (e.getDestination().getLocation() != null && Region.getRegion(e.getDestination().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
} else if (e.getSource().getLocation() != null && Region.getRegion(e.getSource().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onBlockBreak(BlockBreakEvent e) {
if (Core.getVersion() < 19) return;
if (e.getPlayer().getInventory().getItemInMainHand().getType() == Material.DEBUG_STICK) return;
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
if (e.isCancelled()) return;
e.setCancelled(true);
e.getBlock().setType(Material.AIR, false);
}
}
/*
@EventHandler
public void onBlockPlace(BlockPlaceEvent e) {
if (Core.getVersion() < 19) return;
if (!e.getItemInHand().getType().isBlock()) return;
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
e.getBlock().setType(e.getItemInHand().getType(), false);
}, 1L);
}
}
*/
@EventHandler
public void onFluidLevelChange(FluidLevelChangeEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onBlockSpread(BlockSpreadEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onBlockFromTo(BlockFromToEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onSpongeAbsorb(SpongeAbsorbEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onBlockForm(BlockFormEvent e) {
if (Region.getRegion(e.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
e.setCancelled(true);
}
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
if (Region.getRegion(e.getClickedBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
Block block = e.getClickedBlock();
if (block.getType() == Material.LEVER) {
Switch data = ((Switch) block.getBlockData());
data.setPowered(!data.isPowered());
block.setBlockData(data, false);
}
}
}
@EventHandler
public void onBlockFade(BlockFadeEvent event) {
if (Region.getRegion(event.getBlock().getLocation()).get(Flag.FREEZE) == FreezeMode.ACTIVE) {
event.setCancelled(true);
}
}
@Override
public ScoreboardGroup getGroup() {
return ScoreboardGroup.REGION;
}
@Override
public int order() {
return 2;
}
@Override
public String get(Region region, Player p) {
if (region.get(Flag.FREEZE) == Flag.FREEZE.getDefaultValue()) return null;
return "§e" + BauSystem.MESSAGE.parse(Flag.FREEZE.getChatValue(), p) + "§8: " + BauSystem.MESSAGE.parse(region.get(Flag.FREEZE).getChatValue(), p);
}
}
@@ -0,0 +1,71 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.region;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.GlobalRegion;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ItemMode;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.MinVersion;
import org.bukkit.entity.Player;
@Linked
@MinVersion(19)
public class ItemsCommand extends SWCommand {
public ItemsCommand() {
super("items");
}
@Register(description = "REGION_ITEMS_HELP")
public void toggleCommand(@Validator Player p) {
Region region = Region.getRegion(p.getLocation());
if (toggle(region)) {
RegionUtils.actionBar(region, getEnableMessage());
} else {
RegionUtils.actionBar(region, getDisableMessage());
}
}
private String getEnableMessage(){
return "REGION_ITEMS_ENABLED";
}
private String getDisableMessage(){
return "REGION_ITEMS_DISABLED";
}
private boolean toggle(Region region) {
switch (region.getPlain(Flag.ITEMS, ItemMode.class)) {
case ACTIVE:
region.set(Flag.ITEMS, ItemMode.INACTIVE);
return false;
default:
case INACTIVE:
region.set(Flag.ITEMS, ItemMode.ACTIVE);
return true;
}
}
}
@@ -0,0 +1,64 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ItemMode;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.MinVersion;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ItemSpawnEvent;
@Linked
@MinVersion(19)
public class ItemsListener implements Listener, ScoreboardElement {
private static ItemMode getMode(Region region) {
return region.getPlain(Flag.ITEMS, ItemMode.class);
}
@EventHandler
public void onItemSpawn(ItemSpawnEvent event) {
if (getMode(Region.getRegion(event.getLocation())) == ItemMode.INACTIVE) {
event.setCancelled(true);
}
}
@Override
public ScoreboardGroup getGroup() {
return ScoreboardGroup.REGION;
}
@Override
public int order() {
return 3;
}
@Override
public String get(Region region, Player p) {
if (region.get(Flag.ITEMS) == Flag.ITEMS.getDefaultValue()) return null;
return "§e" + BauSystem.MESSAGE.parse(Flag.ITEMS.getChatValue(), p) + "§8: " + BauSystem.MESSAGE.parse(region.get(Flag.ITEMS).getChatValue(), p);
}
}
@@ -0,0 +1,66 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 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.features.region;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.NoGravityMode;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class NoGravityCommand extends SWCommand {
public NoGravityCommand() {
super("nogravity");
}
@Register(description = "REGION_NO_GRAVITY_HELP")
public void toggleCommand(@Validator Player p) {
Region region = Region.getRegion(p.getLocation());
if (toggle(region)) {
RegionUtils.actionBar(region, getEnableMessage());
} else {
RegionUtils.actionBar(region, getDisableMessage());
}
}
private String getEnableMessage() {
return "REGION_NO_GRAVITY_ENABLED";
}
private String getDisableMessage() {
return "REGION_NO_GRAVITY_DISABLED";
}
private boolean toggle(Region region) {
switch (region.getPlain(Flag.NO_GRAVITY, NoGravityMode.class)) {
case ACTIVE:
region.set(Flag.NO_GRAVITY, NoGravityMode.INACTIVE);
return false;
default:
case INACTIVE:
region.set(Flag.NO_GRAVITY, NoGravityMode.ACTIVE);
return true;
}
}
}
@@ -0,0 +1,64 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.NoGravityMode;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntitySpawnEvent;
@Linked
public class NoGravityListener implements Listener, ScoreboardElement {
private static NoGravityMode getMode(Region region) {
return region.getPlain(Flag.NO_GRAVITY, NoGravityMode.class);
}
@EventHandler
public void onEntitySpawn(EntitySpawnEvent event) {
if (event.getEntityType() == EntityType.PLAYER) return;
if (getMode(Region.getRegion(event.getLocation())) == NoGravityMode.ACTIVE) {
event.getEntity().setGravity(false);
}
}
@Override
public ScoreboardGroup getGroup() {
return ScoreboardGroup.REGION;
}
@Override
public int order() {
return 4;
}
@Override
public String get(Region region, Player p) {
if (region.get(Flag.NO_GRAVITY) == Flag.NO_GRAVITY.getDefaultValue()) return null;
return "§e" + BauSystem.MESSAGE.parse(Flag.NO_GRAVITY.getChatValue(), p) + "§8: " + BauSystem.MESSAGE.parse(region.get(Flag.NO_GRAVITY).getChatValue(), p);
}
}
@@ -0,0 +1,65 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class ProtectCommand extends SWCommand {
public ProtectCommand() {
super("protect");
}
@Register(description = "REGION_PROTECT_HELP")
public void genericProtectCommand(@Validator Player p) {
Region region = regionCheck(p);
if (region == null) return;
switch (region.getPlain(Flag.PROTECT, ProtectMode.class)) {
case ACTIVE:
region.set(Flag.PROTECT, ProtectMode.INACTIVE);
RegionUtils.actionBar(region, "REGION_PROTECT_DISABLE");
break;
default:
case INACTIVE:
region.set(Flag.PROTECT, ProtectMode.ACTIVE);
RegionUtils.actionBar(region, "REGION_PROTECT_ENABLE");
break;
}
}
private Region regionCheck(Player player) {
Region region = Region.getRegion(player.getLocation());
if (region.getFloorLevel() == 0) {
BauSystem.MESSAGE.send("REGION_PROTECT_FALSE_REGION", player);
return null;
}
return region;
}
}
@@ -0,0 +1,59 @@
package de.steamwar.bausystem.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.linkage.Linked;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.List;
@Linked
public class ProtectListener implements Listener, ScoreboardElement {
private void explode(List<Block> blockList, Location location) {
Region region = Region.getRegion(location);
if (region.getFloorLevel() == 0) {
return;
}
if (region.getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.INACTIVE) {
return;
}
blockList.removeIf(block -> block.getY() < region.getFloorLevel());
}
@EventHandler
public void onBlockExplode(BlockExplodeEvent event) {
explode(event.blockList(), event.getBlock().getLocation());
}
@EventHandler
public void onExplode(EntityExplodeEvent event) {
explode(event.blockList(), event.getLocation());
}
@Override
public ScoreboardGroup getGroup() {
return ScoreboardGroup.REGION;
}
@Override
public int order() {
return 2;
}
@Override
public String get(Region region, Player p) {
if (region.getFloorLevel() == 0) return null;
if (region.get(Flag.PROTECT) != Flag.PROTECT.getDefaultValue()) return null;
return "§e" + BauSystem.MESSAGE.parse(Flag.PROTECT.getChatValue(), p) + "§8: " + BauSystem.MESSAGE.parse(region.get(Flag.PROTECT).getChatValue(), p);
}
}
@@ -0,0 +1,327 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.session.ClipboardHolder;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.util.SelectCommand;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ColorMode;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.shared.Pair;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.bausystem.utils.WorldEditUtils;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import de.steamwar.sql.SchematicNode;
import net.md_5.bungee.api.chat.ClickEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;
@Linked
public class RegionCommand extends SWCommand {
@LinkedInstance
public SelectCommand selectCommand;
@LinkedInstance
public ColorCommand colorCommand;
public RegionCommand() {
super("region", "rg");
}
static boolean checkGlobalRegion(Region region, Player p) {
if (region.isGlobal()) {
BauSystem.MESSAGE.send("REGION_REGION_NO_REGION", p);
return true;
}
return false;
}
@Register(value = "undo", description = "REGION_REGION_HELP_UNDO")
public void undoCommand(@Validator Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) return;
if (region.undo()) {
RegionUtils.message(region, "REGION_REGION_UNDID");
} else {
BauSystem.MESSAGE.send("REGION_REGION_NOTHING_UNDO", p);
}
}
@Register(value = "redo", description = "REGION_REGION_HELP_REDO")
public void redoCommand(@Validator Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
if (region.redo()) {
RegionUtils.message(region, "REGION_REGION_REDID");
} else {
BauSystem.MESSAGE.send("REGION_REGION_NOTHING_REDO", p);
}
}
@Register(value = "restore", description = "REGION_REGION_HELP_RESTORE")
public void genericRestoreCommand(@Validator Player p) {
Region region = Region.getRegion(p.getLocation());
if(checkGlobalRegion(region, p)) return;
try {
PasteBuilder pasteBuilder = new PasteBuilder(new PasteBuilder.FileProvider(region.getResetFile(RegionType.NORMAL)))
.ignoreAir(true)
.color(region.getPlain(Flag.COLOR, ColorMode.class).getColor());
region.reset(pasteBuilder, RegionType.NORMAL, RegionExtensionType.NORMAL);
RegionUtils.message(region, "REGION_REGION_RESTORED");
} catch (SecurityException e) {
BauSystem.MESSAGE.send("REGION_REGION_FAILED_RESTORE", p);
Bukkit.getLogger().log(Level.WARNING, "Failed restore", e);
}
}
@Register(value = "restore", description = "REGION_REGION_HELP_RESTORE_SCHEMATIC")
public void schematicRestoreCommand(@Validator Player p, SchematicNode node) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) return;
if(node.isDir()) {
BauSystem.MESSAGE.send("ONLY_SCHEMS", p);
return;
}
try {
PasteBuilder pasteBuilder = new PasteBuilder(new PasteBuilder.SchematicProvider(node))
.ignoreAir(true)
.color(region.getPlain(Flag.COLOR, ColorMode.class).getColor());
region.reset(pasteBuilder, RegionType.NORMAL, RegionExtensionType.NORMAL);
RegionUtils.message(region, "REGION_REGION_RESTORED");
} catch (SecurityException e) {
BauSystem.MESSAGE.send("REGION_REGION_FAILED_RESTORE", p);
Bukkit.getLogger().log(Level.WARNING, "Failed restore", e);
}
}
@Register(value = "copypoint", description = "REGION_REGION_HELP_COPYPOINT")
public void copyPointCommand(Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
if (region.getCopyPoint() == null) {
BauSystem.MESSAGE.send("REGION_REGION_TP_UNKNOWN", p);
return;
}
p.teleport(region.getCopyPoint().toLocation(p, 0.5, 0, 0.5), PlayerTeleportEvent.TeleportCause.COMMAND);
BauSystem.MESSAGE.send("REGION_REGION_TP_COPY", p);
}
@Register(value = "testblockpoint", description = "REGION_REGION_HELP_TESTBLOCKPOINT")
@Register("tbpoint")
public void testBlockPointCommand(Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
if (region.getTestBlockPoint() == null) {
BauSystem.MESSAGE.send("REGION_REGION_TP_UNKNOWN", p);
return;
}
p.teleport(region.getTestBlockPoint().toLocation(p, 0.5, 0, 0.5), PlayerTeleportEvent.TeleportCause.COMMAND);
BauSystem.MESSAGE.send("REGION_REGION_TP_TEST_BLOCK", p);
}
@Register(value = "changeskin", description = "REGION_REGION_HELP_CHANGESKIN_INFO")
@Register("skin")
public void changeSkinCommand(Player p) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
BauSystem.MESSAGE.send("REGION_REGION_CHANGESKIN_INFO", p, region.getSkin());
String creator = region.getPrototype().getSkinMap().get(region.getSkin()).getCreator();
if (creator != null) {
BauSystem.MESSAGE.send("REGION_REGION_CHANGESKIN_INFO_CREATOR", p, creator);
}
}
@Register(value = "changeskin", description = "REGION_REGION_HELP_CHANGESKIN")
@Register("skin")
public void changeSkinCommand(@Validator Player p, @Mapper("skinTypeMapper") String s) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
if (!region.getPrototype().getSkinMap().containsKey(s)) {
BauSystem.MESSAGE.send("REGION_REGION_CHANGESKIN_UNKNOWN", p);
} else {
if (region.setSkin(s)) {
BauSystem.MESSAGE.send("REGION_REGION_CHANGESKIN_CHANGE", p, s);
BauSystem.MESSAGE.send("REGION_REGION_CHANGESKIN_CHANGE_UPDATE", p, BauSystem.MESSAGE.parse("REGION_REGION_CHANGESKIN_CHANGE_UPDATE_HOVER", p), new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/reset"));
} else {
BauSystem.MESSAGE.send("REGION_REGION_CHANGESKIN_INVALID", p);
}
}
}
@Register(value = "copy", description = "REGION_REGION_HELP_COPY")
public void copyCommand(@Validator Player p, @OptionalValue("") @StaticValue(value = {"", "-e", "-s"}, allowISE = true) int option) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
if (!region.hasType(RegionType.BUILD)) {
BauSystem.MESSAGE.send("REGION_REGION_NO_BUILD", p);
return;
}
if (region.getCopyPoint() == null) {
BauSystem.MESSAGE.send("REGION_REGION_TP_UNKNOWN", p);
return;
}
Point minPoint = region.getMinPoint(RegionType.BUILD, RegionExtensionType.NORMAL);
Point maxPoint = region.getMaxPoint(RegionType.BUILD, RegionExtensionType.NORMAL);
switch (option) {
case 0:
break;
case 1:
minPoint = region.getMinPoint(RegionType.BUILD, RegionExtensionType.EXTENSION);
maxPoint = region.getMaxPoint(RegionType.BUILD, RegionExtensionType.EXTENSION);
break;
case 2:
Pair<Location, Location> selection = WorldEditUtils.getSelection(p);
minPoint = Point.fromLocation(selection.getKey());
maxPoint = Point.fromLocation(selection.getValue());
break;
default:
break;
}
Clipboard clipboard = FlatteningWrapper.impl.copy(minPoint, maxPoint, region.getCopyPoint());
WorldEdit.getInstance()
.getSessionManager()
.get(BukkitAdapter.adapt(p))
.setClipboard(new ClipboardHolder(clipboard));
BauSystem.MESSAGE.send("REGION_REGION_COPY_DONE", p);
}
@Register(value = "paste", description = "REGION_REGION_HELP_PASTE")
public void pasteCommand(@Validator Player p, @OptionalValue("") @StaticValue(value = {"", "-a", "-s", "-as", "-sa"}, allowISE = true) int options) {
Region region = Region.getRegion(p.getLocation());
if (checkGlobalRegion(region, p)) {
return;
}
if (!region.hasType(RegionType.BUILD)) {
BauSystem.MESSAGE.send("REGION_REGION_NO_BUILD", p);
return;
}
if (region.getCopyPoint() == null) {
BauSystem.MESSAGE.send("REGION_REGION_TP_UNKNOWN", p);
return;
}
ClipboardHolder clipboardHolder = WorldEdit.getInstance()
.getSessionManager()
.get(BukkitAdapter.adapt(p))
.getClipboard();
boolean selectPasted = false;
boolean ignoreAir = false;
switch (options) {
case 0:
break;
case 1:
ignoreAir = true;
break;
case 2:
selectPasted = true;
break;
case 3:
case 4:
selectPasted = true;
ignoreAir = true;
break;
default:
break;
}
try (EditSession e = WorldEditUtils.getEditSession(p)) {
Operations.completeBlindly(clipboardHolder.createPaste(e).ignoreAirBlocks(ignoreAir).to(toBlockVector3(region.getCopyPoint())).build());
WorldEditUtils.addToPlayer(p, e);
if (selectPasted) {
Clipboard clipboard = clipboardHolder.getClipboards().get(0);
BlockVector3 minPointSelection = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(toBlockVector3(region.getCopyPoint()));
BlockVector3 maxPointSelection = clipboard.getRegion().getMaximumPoint().subtract(clipboard.getOrigin()).add(toBlockVector3(region.getCopyPoint()));
FlatteningWrapper.impl.setSelection(p, Point.fromBlockVector3(minPointSelection), Point.fromBlockVector3(maxPointSelection));
}
BauSystem.MESSAGE.send("REGION_REGION_PASTE_DONE", p);
}
}
private BlockVector3 toBlockVector3(Point point) {
return BlockVector3.at(point.getX(), point.getY(), point.getZ());
}
@Mapper(value = "skinTypeMapper", local = true)
private TypeMapper<String> skinTypeMapper() {
return new TypeMapper<String>() {
@Override
public List<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
Player p = (Player) commandSender;
Region region = Region.getRegion(p.getLocation());
if (region.isGlobal()) {
return Collections.emptyList();
}
return region.getPrototype().getSkinMap().keySet().stream().map(c -> c.replace(' ', '_')).collect(Collectors.toList());
}
@Override
public String map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
return s.replace('_', ' ');
}
};
}
}
@@ -0,0 +1,192 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.tags.Tag;
import de.steamwar.linkage.Linked;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scheduler.BukkitRunnable;
@Linked
public class RegionListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockFromTo(final BlockFromToEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
RegionListener.tagChangedRegion(event.getToBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockIgnite(final BlockIgniteEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBurn(final BlockBurnEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPhysics(final BlockPhysicsEvent event) {
if (event.getBlock().getType() != event.getChangedType()) RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onLeavesDecay(final LeavesDecayEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBLockForm(final BlockFormEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockSpread(final BlockSpreadEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockExplode(final BlockExplodeEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityExplode(final EntityExplodeEvent event) {
RegionListener.tagChangedRegion(event.getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onExplosionPrime(final ExplosionPrimeEvent event) {
RegionListener.tagChangedRegion(event.getEntity().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockMultiPlace(final BlockMultiPlaceEvent event) {
new BukkitRunnable() {
@Override
public void run() {
event.getReplacedBlockStates().forEach(blockState -> RegionListener.tagChangedRegion(blockState.getLocation()));
}
}.runTaskAsynchronously(BauSystem.getInstance());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCanBuild(final BlockCanBuildEvent event) {
if (event.isBuildable()) RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(final BlockBreakEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInteract(final PlayerInteractEvent event) {
if (event.getClickedBlock() != null) RegionListener.tagChangedRegion(event.getClickedBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntitySpawn(final EntitySpawnEvent event) {
RegionListener.tagChangedRegion(event.getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityChangeBlock(final EntityChangeBlockEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPistonExtend(final BlockPistonExtendEvent event) {
new BukkitRunnable() {
@Override
public void run() {
event.getBlocks().forEach(block -> RegionListener.tagChangedRegion(block.getLocation()));
}
}.runTaskAsynchronously(BauSystem.getInstance());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPistonRetract(final BlockPistonRetractEvent event) {
new BukkitRunnable() {
@Override
public void run() {
event.getBlocks().forEach(block -> RegionListener.tagChangedRegion(block.getLocation()));
}
}.runTaskAsynchronously(BauSystem.getInstance());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockGrow(final BlockGrowEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockRedstone(final BlockRedstoneEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockDispense(final BlockDispenseEvent event) {
RegionListener.tagChangedRegion(event.getBlock().getLocation());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryMoveItemMonitor(final InventoryMoveItemEvent event) {
final Location source = event.getSource().getLocation();
final Location destination = event.getDestination().getLocation();
if (source != null) {
RegionListener.tagChangedRegion(source);
}
if (destination != null) {
RegionListener.tagChangedRegion(destination);
}
}
private static void tagChangedRegion(final Location location) {
new BukkitRunnable() {
@Override
public void run() {
Region.getRegion(location).set(Tag.CHANGED);
}
}.runTaskAsynchronously(BauSystem.getInstance());
}
}
@@ -0,0 +1,47 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.GlobalRegion;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class RegionScoreboardElement implements ScoreboardElement {
@Override
public ScoreboardGroup getGroup() {
return ScoreboardGroup.HEADER;
}
@Override
public int order() {
return 1;
}
@Override
public String get(Region region, Player p) {
if (GlobalRegion.getInstance() == region) return null;
return "§e" + BauSystem.MESSAGE.parse("SCOREBOARD_REGION", p) + "§8: §7" + region.getDisplayName();
}
}
@@ -0,0 +1,108 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.config.BauServer;
import de.steamwar.bausystem.region.GlobalRegion;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ColorMode;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import de.steamwar.sql.Punishment;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.logging.Level;
@Linked
public class ResetCommand extends SWCommand {
@LinkedInstance
public BauServer bauServer;
public ResetCommand() {
super("reset");
}
@Register(description = "REGION_RESET_HELP_RESET")
public void genericResetCommand(@Validator Player p) {
Region region = regionCheck(p);
if (region == null) return;
try {
PasteBuilder pasteBuilder = new PasteBuilder(new PasteBuilder.FileProvider(region.getResetFile(RegionType.NORMAL)))
.color(region.getPlain(Flag.COLOR, ColorMode.class).getColor());
region.reset(pasteBuilder, RegionType.NORMAL, RegionExtensionType.NORMAL);
RegionUtils.message(region, "REGION_RESET_RESETED");
} catch (SecurityException e) {
BauSystem.MESSAGE.send("REGION_RESET_ERROR", p);
Bukkit.getLogger().log(Level.WARNING, "Failed testblock", e);
}
}
@Register(description = "REGION_RESET_HELP_SCHEMATIC")
public void schematicResetCommand(@Validator Player p, SchematicNode node) {
Region region = regionCheck(p);
if (region == null) return;
if (!p.getUniqueId().equals(bauServer.getOwner())) {
if (Punishment.isPunished(SteamwarUser.get(bauServer.getOwner()), Punishment.PunishmentType.NoSchemReceiving, punishment -> BauSystem.MESSAGE.send("REGION_TB_NO_SCHEMRECEIVING", p, punishment.getEndTime()))) {
return;
}
if (Punishment.isPunished(SteamwarUser.get(p.getUniqueId()), Punishment.PunishmentType.NoSchemSharing, punishment -> BauSystem.MESSAGE.send("REGION_TB_NO_SCHEMSHARING", p, punishment.getEndTime()))) {
return;
}
}
if (node.isDir()) {
BauSystem.MESSAGE.send("ONLY_SCHEMS", p);
return;
}
try {
PasteBuilder pasteBuilder = new PasteBuilder(new PasteBuilder.SchematicProvider(node))
.color(region.getPlain(Flag.COLOR, ColorMode.class).getColor());
region.reset(pasteBuilder, RegionType.NORMAL, RegionExtensionType.NORMAL);
RegionUtils.message(region, "REGION_RESET_RESETED");
} catch (SecurityException e) {
BauSystem.MESSAGE.send("REGION_RESET_ERROR", p);
Bukkit.getLogger().log(Level.WARNING, "Failed reset", e);
}
}
private Region regionCheck(Player player) {
Region region = Region.getRegion(player.getLocation());
if (region == GlobalRegion.getInstance()) {
BauSystem.MESSAGE.send("REGION_RESET_NO_REGION", player);
return null;
}
return region;
}
}
@@ -0,0 +1,160 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.TNTMode;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Linked
public class TNTCommand extends SWCommand {
public TNTCommand() {
super("tnt");
}
@Register(help = true)
public void genericHelp(Player p, String... args) {
BauSystem.MESSAGE.sendPrefixless("COMMAND_HELP_HEAD", p, "Tnt");
BauSystem.MESSAGE.sendPrefixless("REGION_TNT_HELP", p);
BauSystem.MESSAGE.sendPrefixless("REGION_TNT_HELP_MODE", p);
}
@Register(description = "REGION_TNT_HELP")
public void toggleCommand(@Validator Player p) {
Region region = Region.getRegion(p.getLocation());
tntToggle(region, null, null);
}
@Register(description = "REGION_TNT_HELP_MODE")
public void setCommand(@Validator Player p, TNTMode tntMode) {
Region region = Region.getRegion(p.getLocation());
String requestedMessage = null;
switch (tntMode) {
case ALLOW:
requestedMessage = getEnableMessage();
break;
case DENY:
requestedMessage = getDisableMessage();
break;
case ONLY_TB:
requestedMessage = getTestblockEnableMessage();
break;
case ONLY_BUILD:
requestedMessage = getBuildEnableMessage();
break;
}
tntToggle(region, tntMode, requestedMessage);
}
@ClassMapper(value = TNTMode.class, local = true)
public TypeMapper<TNTMode> tntModeTypeMapper() {
Map<String, TNTMode> tntModeMapReduced = new HashMap<>();
tntModeMapReduced.put("an", TNTMode.ALLOW);
tntModeMapReduced.put("on", TNTMode.ALLOW);
tntModeMapReduced.put("aus", TNTMode.DENY);
tntModeMapReduced.put("off", TNTMode.DENY);
Map<String, TNTMode> tntModeMap = new HashMap<>(tntModeMapReduced);
tntModeMap.put("testblock", TNTMode.ONLY_TB);
tntModeMap.put("tb", TNTMode.ONLY_TB);
tntModeMap.put("build", TNTMode.ONLY_BUILD);
return new TypeMapper<TNTMode>() {
@Override
public List<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
Region region = Region.getRegion(((Player) sender).getLocation());
if (region.hasType(RegionType.TESTBLOCK) && region.hasType(RegionType.BUILD)) {
return new ArrayList<>(tntModeMap.keySet());
} else {
return new ArrayList<>(tntModeMapReduced.keySet());
}
}
@Override
public TNTMode map(CommandSender sender, PreviousArguments previousArguments, String s) {
Region region = Region.getRegion(((Player) sender).getLocation());
if (region.hasType(RegionType.TESTBLOCK) && region.hasType(RegionType.BUILD)) {
return tntModeMap.getOrDefault(s, null);
} else {
return tntModeMapReduced.getOrDefault(s, null);
}
}
};
}
private String getEnableMessage() {
return "REGION_TNT_ON";
}
private String getDisableMessage() {
return "REGION_TNT_OFF";
}
private String getTestblockEnableMessage() {
return "REGION_TNT_TB";
}
private String getBuildEnableMessage() {
return "REGION_TNT_BUILD";
}
private void tntToggle(Region region, TNTMode requestedMode, String requestedMessage) {
if (requestedMode != null && region.hasType(RegionType.TESTBLOCK) && region.hasType(RegionType.BUILD)) {
region.set(Flag.TNT, requestedMode);
RegionUtils.actionBar(region, requestedMessage);
return;
}
switch (region.getPlain(Flag.TNT, TNTMode.class)) {
case ALLOW:
case ONLY_TB:
case ONLY_BUILD:
region.set(Flag.TNT, TNTMode.DENY);
RegionUtils.actionBar(region, getDisableMessage());
break;
case DENY:
if (region.hasType(RegionType.TESTBLOCK) && region.hasType(RegionType.BUILD)) {
region.set(Flag.TNT, TNTMode.ONLY_TB);
RegionUtils.actionBar(region, getTestblockEnableMessage());
} else {
region.set(Flag.TNT, TNTMode.ALLOW);
RegionUtils.actionBar(region, getEnableMessage());
}
break;
}
}
}
@@ -0,0 +1,98 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.TNTMode;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.linkage.Linked;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@Linked
public class TNTListener implements Listener, ScoreboardElement {
private void explode(List<Block> blockList) {
blockList.removeIf(block -> {
Region region = Region.getRegion(block.getLocation());
TNTMode value = region.getPlain(Flag.TNT);
if (value == TNTMode.ALLOW) {
return false;
} else if (value == TNTMode.ONLY_TB) {
if (region.hasType(RegionType.BUILD) && region.inRegion(block.getLocation(), RegionType.BUILD, RegionExtensionType.NORMAL)) {
RegionUtils.actionBar(region, "REGION_TNT_BUILD_DESTROY");
return true;
}
if (region.hasType(RegionType.BUILD) && region.inRegion(block.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION)) {
RegionUtils.actionBar(region, "REGION_TNT_BUILD_DESTROY");
return true;
}
} else if (value == TNTMode.ONLY_BUILD) {
if (region.hasType(RegionType.TESTBLOCK) && region.inRegion(block.getLocation(), RegionType.TESTBLOCK, RegionExtensionType.NORMAL)) {
RegionUtils.actionBar(region, "REGION_TNT_TB_DESTROY");
return true;
}
if (region.hasType(RegionType.TESTBLOCK) && region.inRegion(block.getLocation(), RegionType.TESTBLOCK, RegionExtensionType.EXTENSION)) {
RegionUtils.actionBar(region, "REGION_TNT_TB_DESTROY");
return true;
}
}
return value == TNTMode.DENY;
});
}
@EventHandler
public void onBlockExplode(BlockExplodeEvent event) {
explode(event.blockList());
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onExplode(EntityExplodeEvent event) {
explode(event.blockList());
}
@Override
public ScoreboardGroup getGroup() {
return ScoreboardGroup.REGION;
}
@Override
public int order() {
return 0;
}
@Override
public String get(Region region, Player p) {
return "§e" + BauSystem.MESSAGE.parse(Flag.TNT.getChatValue(), p) + "§8: " + BauSystem.MESSAGE.parse(region.get(Flag.TNT).getChatValue(), p);
}
}
@@ -0,0 +1,219 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.config.BauServer;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ColorMode;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import de.steamwar.sql.Punishment;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.stream.Collectors;
@Linked
public class TestblockCommand extends SWCommand {
@LinkedInstance
public BauServer bauServer;
public TestblockCommand() {
super("testblock", "tb", "dummy");
}
@Register
public void schematicTestblockCommand(@Validator Player p, TestblockParameterType... testblockParameterTypes) {
schematicTestblockCommand(p, null, testblockParameterTypes);
}
@Register
public void schematicTestblockCommand(@Validator Player p, @Mapper("withPublic") SchematicNode node, TestblockParameterType... testblockParameterTypes) {
Set<TestblockParameterType> testblockParameterTypesSet = new HashSet<>(Arrays.asList(testblockParameterTypes));
boolean isExtension = testblockParameterTypesSet.contains(TestblockParameterType.EXTENSION);
boolean isIgnoreAir = testblockParameterTypesSet.contains(TestblockParameterType.IGNORE_AIR);
boolean isOnlyColor = testblockParameterTypesSet.contains(TestblockParameterType.ONLY_COLOR);
boolean replaceTNT = testblockParameterTypesSet.contains(TestblockParameterType.TNT);
boolean replaceWater = testblockParameterTypesSet.contains(TestblockParameterType.WATER);
resetRegion(p, node, isExtension ? RegionExtensionType.EXTENSION : RegionExtensionType.NORMAL, isIgnoreAir, isOnlyColor, replaceTNT, replaceWater);
}
private void resetRegion(Player p, SchematicNode node, RegionExtensionType regionExtensionType, boolean ignoreAir, boolean onlyColors, boolean removeTNT, boolean removeWater) {
Region region = regionCheck(p);
if (region == null) return;
if (node != null) {
if (node.isDir()) {
BauSystem.MESSAGE.send("ONLY_SCHEMS", p);
return;
}
}
if (!p.getUniqueId().equals(bauServer.getOwner())) {
if (Punishment.isPunished(SteamwarUser.get(bauServer.getOwner()), Punishment.PunishmentType.NoSchemReceiving, punishment -> BauSystem.MESSAGE.send("REGION_TB_NO_SCHEMRECEIVING", p, punishment.getEndTime()))) {
return;
}
if (Punishment.isPunished(SteamwarUser.get(p.getUniqueId()), Punishment.PunishmentType.NoSchemSharing, punishment -> BauSystem.MESSAGE.send("REGION_TB_NO_SCHEMSHARING", p, punishment.getEndTime()))) {
return;
}
}
try {
PasteBuilder.ClipboardProvider clipboardProvider = node == null ? new PasteBuilder.FileProvider(region.getResetFile(RegionType.TESTBLOCK)) : new PasteBuilder.SchematicProvider(node);
PasteBuilder pasteBuilder = new PasteBuilder(clipboardProvider)
.ignoreAir(ignoreAir)
.onlyColors(onlyColors)
.removeTNT(removeTNT)
.removeWater(removeWater)
.color(region.getPlain(Flag.COLOR, ColorMode.class).getColor());
region.reset(pasteBuilder, RegionType.TESTBLOCK, regionExtensionType);
RegionUtils.message(region, "REGION_TB_DONE");
} catch (SecurityException e) {
BauSystem.MESSAGE.send("REGION_TB_ERROR", p);
Bukkit.getLogger().log(Level.WARNING, "Failed testblock", e);
}
}
@ClassMapper(value = TestblockParameterType.class, local = true)
public TypeMapper<TestblockParameterType> testblockParameterTypeMapper() {
Map<TestblockParameterType, List<String>> testblockParameterTypeListMap = new EnumMap<>(TestblockParameterType.class);
for (TestblockParameterType value : TestblockParameterType.values()) {
testblockParameterTypeListMap.put(value, value.getTabCompletes());
}
Map<String, TestblockParameterType> testblockParameterTypesMap = new HashMap<>();
testblockParameterTypeListMap.forEach((k, v) -> v.forEach(s -> testblockParameterTypesMap.put(s, k)));
return new TypeMapper<TestblockParameterType>() {
@Override
public TestblockParameterType map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
return testblockParameterTypesMap.get(s);
}
@Override
public List<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
Set<TestblockParameterType> testblockParameterTypeSet = new HashSet<>();
previousArguments.getAll(TestblockParameterType.class).forEach(showModeType -> {
testblockParameterTypeSet.addAll(Arrays.asList(showModeType.getRemoved().get()));
});
Arrays.stream(previousArguments.userArgs).map(testblockParameterTypesMap::get).forEach(testblockParameterTypeSet::add);
testblockParameterTypeSet.remove(null);
Set<TestblockParameterType> removed = testblockParameterTypeSet.stream()
.map(TestblockParameterType::getRemoved)
.map(Supplier::get)
.flatMap(Arrays::stream)
.collect(Collectors.toSet());
List<String> tabCompletes = new ArrayList<>();
for (Map.Entry<TestblockParameterType, List<String>> entry : testblockParameterTypeListMap.entrySet()) {
if (removed.contains(entry.getKey()) || testblockParameterTypeSet.contains(entry.getKey())) {
continue;
}
tabCompletes.addAll(entry.getValue());
}
return tabCompletes;
}
};
}
private Region regionCheck(Player player) {
Region region = Region.getRegion(player.getLocation());
if (!region.hasType(RegionType.TESTBLOCK)) {
BauSystem.MESSAGE.send("REGION_TB_NO_REGION", player);
return null;
}
return region;
}
@Mapper("withPublic")
public TypeMapper<SchematicNode> nodeWithPublic() {
return new TypeMapper<SchematicNode>() {
@Override
public List<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
List<String> stringList = new ArrayList<>(SchematicNode.getNodeTabcomplete(SteamwarUser.get(((Player) commandSender).getUniqueId()), s));
stringList.addAll(SchematicNode.getNodeTabcomplete(SteamwarUser.get(0), s));
return stringList;
}
@Override
public SchematicNode map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
SchematicNode node = SchematicNode.getNodeFromPath(SteamwarUser.get(((Player) commandSender).getUniqueId()), s);
if (node == null) {
node = SchematicNode.getNodeFromPath(SteamwarUser.get(0), s);
}
return node;
}
};
}
public enum TestblockParameterType {
EXTENSION(Arrays.asList("-e", "-extension")),
TNT(Arrays.asList("-t", "-tnt")),
WATER(Arrays.asList("-w", "-water")),
IGNORE_AIR(Arrays.asList("-ig", "-ignore_air")),
ONLY_COLOR(Arrays.asList("-o", "-color", "-only_color"));
@Getter
private List<String> tabCompletes;
@Getter
private final Supplier<TestblockParameterType[]> removed;
private AtomicReference<TestblockParameterType[]> cached = new AtomicReference<>();
TestblockParameterType(List<String> tabCompletes, String... removed) {
this.tabCompletes = tabCompletes;
this.removed = () -> {
if (cached.get() == null) {
TestblockParameterType[] showModeParameterTypes = new TestblockParameterType[removed.length];
for (int i = 0; i < removed.length; i++) {
showModeParameterTypes[i] = TestblockParameterType.valueOf(removed[i]);
}
cached.set(showModeParameterTypes);
return showModeParameterTypes;
}
return cached.get();
};
}
}
}
@@ -0,0 +1,114 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region.items;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.region.Color;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ColorMode;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Linked
public class ColorBauGuiItem extends BauGuiItem {
public ColorBauGuiItem() {
super(2);
}
private static Material mapColor(Color color) {
switch (color) {
case RED:
return Material.RED_CONCRETE;
case CYAN:
return Material.CYAN_CONCRETE;
case BLACK:
return Material.BLACK_CONCRETE;
case BLUE:
return Material.BLUE_CONCRETE;
case BROWN:
return Material.BROWN_CONCRETE;
case GRAY:
return Material.GRAY_CONCRETE;
case GREEN:
return Material.GREEN_CONCRETE;
case LIGHT_BLUE:
return Material.LIGHT_BLUE_CONCRETE;
case LIGHT_GRAY:
return Material.LIGHT_GRAY_CONCRETE;
case LIME:
return Material.LIME_CONCRETE;
case MAGENTA:
return Material.MAGENTA_CONCRETE;
case ORANGE:
return Material.ORANGE_CONCRETE;
case PINK:
return Material.PINK_CONCRETE;
case PURPLE:
return Material.PURPLE_CONCRETE;
case WHITE:
return Material.WHITE_CONCRETE;
default:
return Material.YELLOW_CONCRETE;
}
}
@Override
public ItemStack getItem(Player player) {
Region region = Region.getRegion(player.getLocation());
ColorMode mode = region.getPlain(Flag.COLOR, ColorMode.class);
return new SWItem(mapColor(mode.getColor()), BauSystem.MESSAGE.parse("REGION_ITEM_COLOR", player, BauSystem.MESSAGE.parse(mode.getChatValue(), player))).getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
p.closeInventory();
ColorMode current = Region.getRegion(p.getLocation()).getPlain(Flag.COLOR, ColorMode.class);
List<SWListInv.SWListEntry<ColorMode>> items = new ArrayList<>();
for (ColorMode value : ColorMode.values()) {
items.add(new SWListInv.SWListEntry<>(new SWItem(mapColor(value.getColor()), (byte) 0, "§f" + BauSystem.MESSAGE.parse(value.getChatValue(), p), Collections.emptyList(), value == current, clickType -> {
}), value));
}
SWListInv<ColorMode> inv = new SWListInv<>(p, BauSystem.MESSAGE.parse("REGION_ITEM_COLOR_CHOOSE", p), items, (clickType, colorMode) -> {
p.closeInventory();
p.performCommand("color " + colorMode.getChatValue());
update();
});
inv.open();
return false;
}
@Override
public Permission permission() {
return Permission.OWNER;
}
}
@@ -0,0 +1,62 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region.items;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.FireMode;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class FireBauGuiItem extends BauGuiItem {
public FireBauGuiItem() {
super(3);
}
@Override
public ItemStack getItem(Player player) {
Region region = Region.getRegion(player.getLocation());
if (region.getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW) {
return new SWItem(Material.FIRE_CHARGE, BauSystem.MESSAGE.parse("REGION_ITEM_FIRE_ALLOW", player)).getItemStack();
} else {
return new SWItem(Material.FIREWORK_STAR, BauSystem.MESSAGE.parse("REGION_ITEM_FIRE_DISALLOW", player)).getItemStack();
}
}
@Override
public boolean click(ClickType click, Player p) {
p.performCommand("fire");
return true;
}
@Override
public Permission permission() {
return Permission.BUILD;
}
}
@@ -0,0 +1,62 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region.items;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class FreezeBauGuiItem extends BauGuiItem {
public FreezeBauGuiItem() {
super(1);
}
@Override
public ItemStack getItem(Player player) {
Region region = Region.getRegion(player.getLocation());
if (region.getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) {
return new SWItem(Material.GUNPOWDER, BauSystem.MESSAGE.parse("REGION_ITEM_FREEZE_ALLOW", player)).getItemStack();
} else {
return new SWItem(Material.REDSTONE, BauSystem.MESSAGE.parse("REGION_ITEM_FREEZE_DISALLOW", player)).getItemStack();
}
}
@Override
public boolean click(ClickType click, Player p) {
p.performCommand("freeze");
return true;
}
@Override
public Permission permission() {
return Permission.BUILD;
}
}
@@ -0,0 +1,63 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region.items;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class ProtectBauGuiItem extends BauGuiItem {
public ProtectBauGuiItem() {
super(4);
}
@Override
public ItemStack getItem(Player player) {
Region region = Region.getRegion(player.getLocation());
if (region.getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE) {
return SWUtils.setCustomModelData(new SWItem(Material.OBSIDIAN, BauSystem.MESSAGE.parse("REGION_ITEM_PROTECT_ALLOW", player)), 1).getItemStack();
} else {
return SWUtils.setCustomModelData(new SWItem(Material.STONE, BauSystem.MESSAGE.parse("REGION_ITEM_PROTECT_DISALLOW", player)), 1).getItemStack();
}
}
@Override
public boolean click(ClickType click, Player p) {
p.performCommand("protect");
return true;
}
@Override
public Permission permission() {
return Permission.BUILD;
}
}
@@ -0,0 +1,70 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region.items;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.region.ResetCommand;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SchematicSelector;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class ResetBauGuiItem extends BauGuiItem {
@LinkedInstance
public ResetCommand resetCommand;
public ResetBauGuiItem() {
super(6);
}
@Override
public ItemStack getItem(Player player) {
return SWUtils.setCustomModelData(new SWItem(Material.HEAVY_WEIGHTED_PRESSURE_PLATE, BauSystem.MESSAGE.parse("REGION_ITEM_RESET", player)), 1).getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
if (click == ClickType.LEFT) {
p.closeInventory();
resetCommand.genericResetCommand(p);
} else {
SchematicSelector selector = new SchematicSelector(p, SchematicSelector.selectSchematic(), node -> {
p.closeInventory();
resetCommand.schematicResetCommand(p, node);
});
selector.open();
}
return false;
}
@Override
public Permission permission() {
return Permission.BUILD;
}
}
@@ -0,0 +1,69 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region.items;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.features.region.TestblockCommand;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SchematicSelector;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class TestblockBauGuiItem extends BauGuiItem {
@LinkedInstance
public TestblockCommand testblockCommand;
public TestblockBauGuiItem() {
super(5);
}
@Override
public ItemStack getItem(Player player) {
return new SWItem(Material.END_STONE, BauSystem.MESSAGE.parse("REGION_ITEM_TESTBLOCK", player)).getItemStack();
}
@Override
public boolean click(ClickType click, Player p) {
if (click == ClickType.LEFT) {
p.closeInventory();
testblockCommand.schematicTestblockCommand(p);
} else {
SchematicSelector selector = new SchematicSelector(p, SchematicSelector.selectSchematic(), node -> {
p.closeInventory();
testblockCommand.schematicTestblockCommand(p, node);
});
selector.open();
}
return false;
}
@Override
public Permission permission() {
return Permission.BUILD;
}
}
@@ -0,0 +1,121 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.features.region.items;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.TNTMode;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@Linked
public class TntBauGuiItem extends BauGuiItem {
public TntBauGuiItem() {
super(0);
}
@Override
public ItemStack getItem(Player player) {
switch (Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class)) {
case DENY:
return new SWItem(Material.MINECART, BauSystem.MESSAGE.parse("REGION_ITEM_TNT_OFF", player)).getItemStack();
case ONLY_TB:
return new SWItem(Material.TNT_MINECART, BauSystem.MESSAGE.parse("REGION_ITEM_TNT_ONLY_TB", player)).getItemStack();
case ONLY_BUILD:
return new SWItem(Material.OBSIDIAN, BauSystem.MESSAGE.parse("REGION_ITEM_TNT_ONLY_BUILD", player)).getItemStack();
default:
return new SWItem(Material.TNT, BauSystem.MESSAGE.parse("REGION_ITEM_TNT_ON", player)).getItemStack();
}
}
@Override
public boolean click(ClickType click, Player p) {
if (click == ClickType.LEFT) {
switch (Region.getRegion(p.getLocation()).getPlain(Flag.TNT, TNTMode.class)) {
case DENY:
updateTntMode(TNTMode.ALLOW, p);
break;
case ALLOW:
if (Region.getRegion(p.getLocation()).hasType(RegionType.BUILD) && Region.getRegion(p.getLocation()).hasType(RegionType.TESTBLOCK)) {
updateTntMode(TNTMode.ONLY_TB, p);
break;
}
default:
updateTntMode(TNTMode.DENY, p);
}
} else {
p.closeInventory();
SWInventory selector = new SWInventory(p, 9, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_TITLE", p));
selector.setItem(1, new SWItem(Material.TNT, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_ON", p), clickType -> {
updateTntMode(TNTMode.ALLOW, p);
p.closeInventory();
}));
if (Region.getRegion(p.getLocation()).hasType(RegionType.BUILD) && Region.getRegion(p.getLocation()).hasType(RegionType.TESTBLOCK)) {
selector.setItem(3, new SWItem(Material.TNT_MINECART, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_ONLY_TB", p), clickType -> {
updateTntMode(TNTMode.ONLY_TB, p);
p.closeInventory();
}));
selector.setItem(5, new SWItem(Material.OBSIDIAN, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_ONLY_BUILD", p), clickType -> {
updateTntMode(TNTMode.ONLY_BUILD, p);
p.closeInventory();
}));
}
selector.setItem(7, new SWItem(Material.MINECART, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_OFF", p), clickType -> {
updateTntMode(TNTMode.DENY, p);
p.closeInventory();
}));
selector.open();
}
return false;
}
private void updateTntMode(TNTMode mode, Player p) {
switch (mode) {
case ONLY_TB:
p.performCommand("tnt tb");
break;
case ONLY_BUILD:
p.performCommand("tnt build");
break;
case ALLOW:
p.performCommand("tnt on");
break;
default:
p.performCommand("tnt off");
break;
}
update();
}
@Override
public Permission permission() {
return Permission.BUILD;
}
}
@@ -0,0 +1,77 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.script;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
@AllArgsConstructor
@Builder
@EqualsAndHashCode
public class Hotkey {
private final int charcode;
private final boolean ctrl;
private final boolean shift;
private final boolean alt;
private final boolean meta;
public static Hotkey fromString(String string) {
String[] parts = string.split("\\+");
HotkeyBuilder builder = Hotkey.builder();
for (String part : parts) {
switch (part.toLowerCase()) {
case "ctrl":
builder.ctrl(true);
break;
case "shift":
builder.shift(true);
break;
case "alt":
builder.alt(true);
break;
case "meta":
builder.meta(true);
break;
default:
if (part.length() == 1) {
builder.charcode(Character.toLowerCase(part.charAt(0)));
} else {
throw new IllegalArgumentException("Invalid hotkey: " + string);
}
}
}
return builder.build();
}
public static Hotkey fromChar(int c, int mods) {
return Hotkey.builder()
.charcode(Character.toLowerCase(c))
.shift((mods & 1) != 0)
.ctrl((mods & 2) != 0)
.alt((mods & 4) != 0)
.meta((mods & 8) != 0)
.build();
}
}
@@ -0,0 +1,37 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.script;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
@Linked
public class ScriptCommand extends SWCommand {
public ScriptCommand() {
super("script");
}
@Register
public void genericCommand(@Validator Player player) {
ScriptGUI.open(player);
}
}
@@ -0,0 +1,141 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.script;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.script.lua.SteamWarPlatform;
import de.steamwar.bausystem.utils.ItemUtils;
import de.steamwar.inventory.SWAnvilInv;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.linkage.Linked;
import de.steamwar.sql.Script;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.luaj.vm2.Globals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@Linked
public class ScriptGUI implements Listener {
public static void open(Player player) {
open(player, null);
}
private static void open(Player player, ItemStack setCursor) {
SteamwarUser user = SteamwarUser.get(player.getUniqueId());
List<SWListInv.SWListEntry<Script>> entries = new ArrayList<>();
List<String> lore = new ArrayList<>();
Globals globals = SteamWarPlatform.createGlobalParser(
(eventType, luaFunction) -> lore.add(BauSystem.MESSAGE.parse("SCRIPT_EVENT_ITEM_NAME", player, eventType.name())),
(s, luaFunction) -> lore.add(BauSystem.MESSAGE.parse("SCRIPT_HOTKEY_ITEM_NAME", player, s)),
commandRegister -> lore.add(BauSystem.MESSAGE.parse("SCRIPT_COMMAND_ITEM_NAME", player, commandRegister.getName()))
);
Script.list(user).forEach(script -> {
try {
globals.load(script.getCode()).call();
} catch (Exception e) {
String[] sp = e.getMessage().split(":");
lore.add(BauSystem.MESSAGE.parse("SCRIPT_ERROR_GUI", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))));
}
if(!lore.isEmpty()) {
lore.add("");
}
lore.add(BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_ITEM_LORE_1", player));
lore.add(BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_ITEM_LORE_2", player));
lore.add(BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_ITEM_LORE_3", player));
lore.add(BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_ITEM_LORE_4", player));
entries.add(new SWListInv.SWListEntry<>(new SWItem(Material.ENCHANTED_BOOK, script.getName(), new ArrayList<>(lore), false, clickType -> {}), script));
lore.clear();
});
SWListInv<Script> inv = new SWListInv<>(player, BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_NAME", player), false, entries, (clickType, script) -> {
ItemStack itemStack = ScriptHelper.getScriptItem(script, clickType.isRightClick());
if(clickType == ClickType.MIDDLE) {
player.openBook(itemStack);
} else if(!clickType.isShiftClick()) {
script.delete();
ScriptRunner.updateGlobalScript(player);
open(player, itemStack);
} else {
player.getOpenInventory().setCursor(itemStack);
}
});
inv.setItem(49, Material.HOPPER, BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_ITEM_ADD_NAME", player), Collections.singletonList(BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_ITEM_ADD_LORE", player)), false, click -> {
if(player.getOpenInventory().getCursor() != null) {
ItemStack cursor = player.getOpenInventory().getCursor();
if(!(cursor.getItemMeta() instanceof BookMeta)) {
return;
}
BookMeta meta = (BookMeta) cursor.getItemMeta();
if(meta == null) {
return;
}
saveWithName(player, meta, meta.getTitle());
}
});
inv.open();
if(setCursor != null) {
player.getOpenInventory().setCursor(setCursor);
}
}
private static void saveWithName(Player player, BookMeta meta, String name) {
SteamwarUser user = SteamwarUser.get(player.getUniqueId());
if(name != null && Script.list(user).stream().noneMatch(script -> script.getName().equalsIgnoreCase(name))) {
Script.create(user, name, ScriptHelper.getScriptString(meta.getPages()));
player.getOpenInventory().setCursor(null);
ScriptRunner.updateGlobalScript(player);
open(player);
} else {
SWAnvilInv inv = new SWAnvilInv(player, BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_ENTER_NAME", player), name == null ? "" : name);
AtomicBoolean saved = new AtomicBoolean(false);
ItemStack itemStack = player.getOpenInventory().getCursor();
inv.setCallback(s -> {
saveWithName(player, meta, s);
saved.set(true);
});
inv.addCloseCallback(() -> {
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
if (saved.get()) return;
SWUtils.giveItemToPlayer(player, itemStack);
}, 1);
});
inv.open();
}
}
}
@@ -0,0 +1,55 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.script;
import de.steamwar.sql.Script;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ScriptHelper {
private static final String PAGE_SEPARATOR = "\n\0\n";
public static ItemStack getScriptItem(Script script, boolean writeable) {
ItemStack itemStack = new ItemStack(writeable ? Material.WRITABLE_BOOK : Material.WRITTEN_BOOK);
BookMeta meta = (BookMeta) itemStack.getItemMeta();
if(!writeable) {
meta.setTitle(script.getName());
meta.setAuthor(SteamwarUser.get(script.getUserId()).getUserName());
}
meta.setPages(getScriptPages(script));
itemStack.setItemMeta(meta);
return itemStack;
}
public static List<String> getScriptPages(Script script) {
return Arrays.stream(script.getCode().split(PAGE_SEPARATOR)).collect(Collectors.toList());
}
public static String getScriptString(List<String> pages) {
return String.join(PAGE_SEPARATOR, pages);
}
}
@@ -0,0 +1,84 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.script;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import java.util.HashSet;
import java.util.Set;
@Linked
public class ScriptListener implements Listener {
private final Set<Player> playerSet = new HashSet<>();
@EventHandler(priority = EventPriority.HIGH)
public void onLeftClick(PlayerInteractEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
ItemStack item = event.getItem();
if (item == null || FlatteningWrapper.impl.isNoBook(item) || item.getItemMeta() == null) {
return;
}
if (event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) {
if (event.getAction() == Action.RIGHT_CLICK_AIR) {
playerSet.add(event.getPlayer());
}
return;
}
if (playerSet.remove(event.getPlayer())) {
return;
}
event.setCancelled(true);
ScriptRunner.runScript(((BookMeta) item.getItemMeta()).getPages().stream().reduce((s, s2) -> s + "\n" + s2).orElse(null), event.getPlayer());
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
ScriptRunner.remove(event.getPlayer());
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
ScriptRunner.updateGlobalScript(event.getPlayer());
}
@EventHandler
public void onBauMemberUpdate(BauMemberUpdateEvent event) {
event.getNewSpectator().forEach(ScriptRunner::remove);
event.getNewBuilder().forEach(ScriptRunner::updateGlobalScript);
}
}
@@ -0,0 +1,211 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 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.features.script;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.lua.CommandRegister;
import de.steamwar.bausystem.features.script.lua.SteamWarGlobalLuaPlugin;
import de.steamwar.bausystem.features.script.lua.SteamWarPlatform;
import de.steamwar.sql.Script;
import de.steamwar.sql.SteamwarUser;
import lombok.experimental.UtilityClass;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.luaj.vm2.*;
import org.luaj.vm2.lib.OneArgFunction;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
@UtilityClass
public class ScriptRunner {
// Script Table
// User
// Key -> bau-script-<BUCH NAME>
// Value -> <LUA Script>
private static final Map<Player, Map<SteamWarGlobalLuaPlugin.EventType, List<LuaFunction>>> EVENT_MAP = new HashMap<>();
private static final Map<Player, Map<Hotkey, List<LuaFunction>>> HOTKEY_MAP = new HashMap<>();
private static final Map<Player, Map<String, CommandRegister>> COMMAND_MAP = new HashMap<>();
public Set<String> getCommandsOfPlayer(Player player) {
return COMMAND_MAP.getOrDefault(player, new HashMap<>()).keySet();
}
public static void runScript(String script, Player player) {
Globals globals = SteamWarPlatform.createClickGlobals(player);
catchScript("SCRIPT_ERROR_CLICK", player, () -> globals.load(script).call());
}
public static void updateGlobalScript(Player player) {
SteamwarUser user = SteamwarUser.get(player.getUniqueId());
ScriptRunner.createGlobalScript(Script.list(user).stream().map(Script::getCode).collect(Collectors.toList()), player);
}
public static void createGlobalScript(List<String> scripts, Player player) {
remove(player);
Globals globals = SteamWarPlatform.createGlobalGlobals(player,
(s, luaFunction) -> EVENT_MAP.computeIfAbsent(player, player1 -> new EnumMap<>(SteamWarGlobalLuaPlugin.EventType.class)).computeIfAbsent(s, s1 -> new ArrayList<>()).add(luaFunction),
(s, luaFunction) -> HOTKEY_MAP.computeIfAbsent(player, player1 -> new HashMap<>()).computeIfAbsent(Hotkey.fromString(s), s1 -> new ArrayList<>()).add(luaFunction),
commandRegister -> COMMAND_MAP.computeIfAbsent(player, player1 -> new HashMap<>()).put(commandRegister.getName(), commandRegister));
for (String script : scripts) {
catchScript("SCRIPT_ERROR_GLOBAL", player, () -> globals.load(script).call());
}
}
public static void remove(Player player) {
EVENT_MAP.remove(player);
COMMAND_MAP.remove(player);
HOTKEY_MAP.remove(player);
}
public static void callEvent(Player player, SteamWarGlobalLuaPlugin.EventType event, LuaValue eventValue, Event wrappedEvent) {
List<LuaFunction> luaFunctions = EVENT_MAP.getOrDefault(player, Collections.emptyMap()).getOrDefault(event, Collections.emptyList());
if (luaFunctions.isEmpty()) {
if(event == SteamWarGlobalLuaPlugin.EventType.DoubleSwap) {
player.performCommand("gui");
}
return;
}
if (eventValue == LuaValue.NIL) {
eventValue = LuaValue.tableOf();
}
AtomicBoolean cancelled = new AtomicBoolean(false);
if (wrappedEvent instanceof Cancellable) {
eventValue.set("setCancelled", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
cancelled.set(arg.checkboolean());
return valueOf(cancelled.get());
}
});
} else {
eventValue.set("setCancelled", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
throw new LuaError("Event is not cancellable");
}
});
}
final LuaValue finalEventValue = eventValue;
for (LuaFunction luaFunction : luaFunctions) {
catchScript("SCRIPT_ERROR_GLOBAL", player, () -> luaFunction.call(finalEventValue));
}
if (wrappedEvent instanceof Cancellable) {
((Cancellable) wrappedEvent).setCancelled(cancelled.get());
}
}
public static boolean callCommand(Player player, String command, String[] argsArray) {
CommandRegister commandRegister = COMMAND_MAP.getOrDefault(player, Collections.emptyMap()).get(command);
if (commandRegister == null) {
return false;
}
LuaValue[] values = new LuaValue[argsArray.length - 1];
for (int i = 1; i < argsArray.length; i++) {
values[i - 1] = LuaValue.valueOf(argsArray[i]);
}
LuaTable args = LuaValue.listOf(values);
args.set("alias", command);
args.set("hasShortFlag", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
String s = arg.checkjstring();
if (!s.matches("-?[a-zA-Z]")) {
throw new LuaError("Short Flag must be one character");
}
String flag = s.charAt(s.length() - 1) + "";
boolean hasFlag = false;
for (String arg1 : argsArray) {
if (arg1.startsWith("-") && arg1.contains(flag)) {
hasFlag = true;
break;
}
}
return valueOf(hasFlag);
}
});
args.set("removeShortFlag", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
String s = arg.checkjstring();
if (!s.matches("-?[a-zA-Z]")) {
throw new LuaError("Short Flag must be one character");
}
String flag = s.charAt(s.length() - 1) + "";
boolean hasFlag = false;
for (int i = 0; i < argsArray.length; i++) {
String arg1 = argsArray[i];
if (arg1.startsWith("-") && arg1.contains(flag)) {
String newArg = arg1.replace(flag, "");
if (newArg.equals("-")) {
args.remove(i);
} else {
args.set(i, newArg);
}
hasFlag = true;
break;
}
}
return valueOf(hasFlag);
}
});
catchScript("SCRIPT_ERROR_GLOBAL", player, () -> commandRegister.getFunction().call(args));
return true;
}
public static void callHotkey(int mods, int key, Player player, boolean pressed) {
Hotkey hotkey = Hotkey.fromChar(key, mods);
catchScript("SCRIPT_ERROR_GLOBAL", player, () -> HOTKEY_MAP.getOrDefault(player, Collections.emptyMap()).getOrDefault(hotkey, Collections.emptyList()).forEach(luaFunction -> luaFunction.call(LuaValue.valueOf(pressed))));
}
public static void catchScript(String errorMsg, Player player, Runnable run) {
try {
run.run();
} catch (Exception e) {
String[] sp = e.getMessage().split(":");
int index = 0;
for (int i = sp.length - 1; i >= 0; i--) {
String[] ss = sp[i].split(" ");
boolean num = ss[0].chars().mapToObj(Character::isDigit).allMatch(b -> b);
if (num && !ss[0].isEmpty()) {
index = i;
break;
}
}
BauSystem.MESSAGE.send(errorMsg, player, String.join(":", Arrays.copyOfRange(sp, index, sp.length)));
}
}
}

Some files were not shown because too many files have changed in this diff Show More