From 7598aded284c0ae278a8494e029e48da074902ad Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sat, 16 Nov 2024 12:37:12 +0100 Subject: [PATCH] wip --- .forgejo/workflows/build.yml | 18 +++ .../java/eu/m724/tweaks/TweaksPlugin.java | 22 +++ .../eu/m724/tweaks/auth/AuthListener.java | 20 +++ .../java/eu/m724/tweaks/auth/AuthManager.java | 11 ++ .../{utils => tweaks}/chat/ChatCommands.java | 135 +++++++++++------- .../{utils => tweaks}/chat/ChatListener.java | 2 +- .../{utils => tweaks}/chat/ChatManager.java | 2 +- .../m724/{utils => tweaks}/chat/ChatRoom.java | 18 ++- .../chat/ChatRoomLoader.java | 4 +- .../eu/m724/tweaks/door/DoorListener.java | 113 +++++++++++++++ src/main/java/eu/m724/utils/UtilsPlugin.java | 16 --- src/main/resources/plugin.yml | 4 +- 12 files changed, 283 insertions(+), 82 deletions(-) create mode 100644 .forgejo/workflows/build.yml create mode 100644 src/main/java/eu/m724/tweaks/TweaksPlugin.java create mode 100644 src/main/java/eu/m724/tweaks/auth/AuthListener.java create mode 100644 src/main/java/eu/m724/tweaks/auth/AuthManager.java rename src/main/java/eu/m724/{utils => tweaks}/chat/ChatCommands.java (52%) rename src/main/java/eu/m724/{utils => tweaks}/chat/ChatListener.java (98%) rename src/main/java/eu/m724/{utils => tweaks}/chat/ChatManager.java (99%) rename src/main/java/eu/m724/{utils => tweaks}/chat/ChatRoom.java (73%) rename src/main/java/eu/m724/{utils => tweaks}/chat/ChatRoomLoader.java (96%) create mode 100644 src/main/java/eu/m724/tweaks/door/DoorListener.java delete mode 100644 src/main/java/eu/m724/utils/UtilsPlugin.java diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml new file mode 100644 index 0000000..9136ecb --- /dev/null +++ b/.forgejo/workflows/build.yml @@ -0,0 +1,18 @@ +on: [push] +jobs: + deploy: + runs-on: docker + container: debian:sid + steps: + - name: Prepare for installation + run: apt update + - name: Install JDK + run: apt install --no-install-recommends -y openjdk-21-jdk-headless maven git nodejs + - name: Clone repository + run: git clone https://git.m724.eu/Minecon724/mutils.git . + - name: Build + run: mvn clean package + - name: Upload artifacts + uses: https://github.com/actions/upload-artifact@v3 + with: + path: target/mutils-*.jar \ No newline at end of file diff --git a/src/main/java/eu/m724/tweaks/TweaksPlugin.java b/src/main/java/eu/m724/tweaks/TweaksPlugin.java new file mode 100644 index 0000000..312199f --- /dev/null +++ b/src/main/java/eu/m724/tweaks/TweaksPlugin.java @@ -0,0 +1,22 @@ +package eu.m724.tweaks; + +import eu.m724.tweaks.chat.ChatCommands; +import eu.m724.tweaks.chat.ChatManager; +import eu.m724.tweaks.door.DoorListener; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.Objects; + +public class TweaksPlugin extends JavaPlugin { + @Override + public void onEnable() { + ChatManager chatManager = new ChatManager(this); + chatManager.init(); + + ChatCommands chatCommands = new ChatCommands(chatManager); + Objects.requireNonNull(getCommand("chat")).setExecutor(chatCommands); + Objects.requireNonNull(getCommand("chatmanage")).setExecutor(chatCommands); + + getServer().getPluginManager().registerEvents(new DoorListener(), this); + } +} diff --git a/src/main/java/eu/m724/tweaks/auth/AuthListener.java b/src/main/java/eu/m724/tweaks/auth/AuthListener.java new file mode 100644 index 0000000..7949ea2 --- /dev/null +++ b/src/main/java/eu/m724/tweaks/auth/AuthListener.java @@ -0,0 +1,20 @@ +package eu.m724.tweaks.auth; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLoginEvent; + +public class AuthListener implements Listener { + private final AuthManager authManager; + + public AuthListener(AuthManager authManager) { + this.authManager = authManager; + } + + @EventHandler + public void onPlayerLogin(PlayerLoginEvent event) { + Player player = event.getPlayer(); + String hostname = event.getHostname(); + } +} diff --git a/src/main/java/eu/m724/tweaks/auth/AuthManager.java b/src/main/java/eu/m724/tweaks/auth/AuthManager.java new file mode 100644 index 0000000..572201a --- /dev/null +++ b/src/main/java/eu/m724/tweaks/auth/AuthManager.java @@ -0,0 +1,11 @@ +package eu.m724.tweaks.auth; + +import org.bukkit.plugin.Plugin; + +public class AuthManager { + private final Plugin plugin; + + public AuthManager(Plugin plugin) { + this.plugin = plugin; + } +} diff --git a/src/main/java/eu/m724/utils/chat/ChatCommands.java b/src/main/java/eu/m724/tweaks/chat/ChatCommands.java similarity index 52% rename from src/main/java/eu/m724/utils/chat/ChatCommands.java rename to src/main/java/eu/m724/tweaks/chat/ChatCommands.java index dede001..c00286a 100644 --- a/src/main/java/eu/m724/utils/chat/ChatCommands.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatCommands.java @@ -1,4 +1,4 @@ -package eu.m724.utils.chat; +package eu.m724.tweaks.chat; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; @@ -57,6 +57,8 @@ public class ChatCommands implements CommandExecutor { component = new ComponentBuilder("Invalid password").color(ChatColor.RED) .create(); } + } else { + authenticated = true; } } else { component = new ComponentBuilder("No room named ").color(ChatColor.RED) @@ -79,66 +81,92 @@ public class ChatCommands implements CommandExecutor { } else if (command.getName().equals("chatmanage")) { Player player = (Player) sender; ChatRoom chatRoom = manager.getPlayerChatRoom(player); - - if (!chatRoom.owner.equals(player)) { - sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id)); - return true; - } + boolean isOwner = player.equals(chatRoom.owner); if (args.length > 1) { String action = args[0]; String argument = args[1]; - if (action.equals("create")) { - try { - ChatRoom newRoom = manager.createChatRoom(argument, null, player); - sender.sendMessage("Created a chat room. Join it: /c " + newRoom.id); - sender.sendMessage("You might also want to protect it with a password: /cm setpassword"); - } catch (ChatManager.InvalidIdException e) { - sender.sendMessage("ID is invalid: " + e.getMessage()); - } catch (ChatManager.ChatRoomExistsException e) { - sender.sendMessage("Room %s already exists".formatted(argument)); - } catch (IOException e) { - sender.sendMessage("Failed to create room"); - e.printStackTrace(); - } - } else if (action.equals("delete")) { - // TODO - } else if (action.equals("setowner")) { - Player newOwner = Bukkit.getPlayer(argument); - if (newOwner != null && newOwner.isOnline()) { - chatRoom.owner = newOwner; + + switch (action) { + case "create" -> { try { - manager.saveChatRoom(chatRoom); - sender.sendMessage("Owner changed to " + newOwner.getName()); + ChatRoom newRoom = manager.createChatRoom(argument, null, player); + sender.sendMessage("Created a chat room. Join it: /c " + newRoom.id); + sender.sendMessage("You might also want to protect it with a password: /cm setpassword"); + } catch (ChatManager.InvalidIdException e) { + sender.sendMessage("ID is invalid: " + e.getMessage()); + } catch (ChatManager.ChatRoomExistsException e) { + sender.sendMessage("Room %s already exists".formatted(argument)); } catch (IOException e) { - sender.sendMessage("Failed to change owner"); + sender.sendMessage("Failed to create room"); e.printStackTrace(); } - } else { - sender.sendMessage("Player must be online"); } - } else if (action.equals("setpassword")) { - chatRoom.password = Arrays.stream(args).skip(1).collect(Collectors.joining(" ")).strip(); - try { - manager.saveChatRoom(chatRoom); - sender.sendMessage("Password changed"); - } catch (IOException e) { - sender.sendMessage("Failed to change password"); - e.printStackTrace(); - } - } else if (action.equals("setcolor")) { - ChatColor newColor = ChatColor.of(argument); - if (newColor != null) { - chatRoom.color = newColor; - try { - manager.saveChatRoom(chatRoom); - sender.sendMessage("Message color changed to " + newColor.getName()); - } catch (IOException e) { - sender.sendMessage("Failed to change color"); - e.printStackTrace(); + case "delete" -> { + if (argument.equals(chatRoom.id)) { + if (isOwner) { + // TODO + } else { + sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id)); + } + } else { + sender.sendMessage("Pass %s as an argument to confirm".formatted(chatRoom.id)); } - } else { - sender.sendMessage("Invalid color"); + } + case "setowner" -> { + if (isOwner) { + Player newOwner = Bukkit.getPlayer(argument); + if (newOwner != null && newOwner.isOnline()) { + chatRoom.owner = newOwner; + try { + manager.saveChatRoom(chatRoom); + sender.sendMessage("Owner changed to " + newOwner.getName()); + } catch (IOException e) { + sender.sendMessage("Failed to change owner"); + e.printStackTrace(); + } + } else { + sender.sendMessage("Player must be online"); + } + } else { + sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id)); + } + } + case "setpassword" -> { + if (isOwner) { + chatRoom.password = Arrays.stream(args).skip(1).collect(Collectors.joining(" ")).strip(); + try { + manager.saveChatRoom(chatRoom); + sender.sendMessage("Password changed"); + } catch (IOException e) { + sender.sendMessage("Failed to change password"); + e.printStackTrace(); + } + } else { + sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id)); + } + } + case "setcolor" -> { + if (isOwner) { + ChatColor newColor = ChatColor.of(argument); + if (newColor != null) { + chatRoom.color = newColor; + try { + manager.saveChatRoom(chatRoom); + sender.sendMessage("Message color changed to " + newColor.getName()); + } catch (IOException e) { + sender.sendMessage("Failed to change color"); + e.printStackTrace(); + } + } else { + sender.sendMessage("Invalid color"); + } + } else { + sender.sendMessage("You're not the owner of %s, please enter the room you want to make changes in".formatted(chatRoom.id)); + } + } + default -> { + sender.sendMessage("Actions: create, delete, setowner, setpassword, setcolor"); } } } else if (args.length > 0) { @@ -153,10 +181,11 @@ public class ChatCommands implements CommandExecutor { sender.sendMessage("To change the password of room %s, pass the new password as an argument for this action.".formatted(chatRoom)); case "setcolor" -> sender.sendMessage("To change the message color of room %s, pass the new color as an argument for this action. #hex or color name.".formatted(chatRoom)); - default -> sender.sendMessage("create, delete, setowner, setpassword"); + default -> + sender.sendMessage("Actions: create, delete, setowner, setpassword, setcolor"); } } else { - sender.sendMessage("create, delete, setowner, setpassword, setcolor"); + sender.sendMessage("Actions: create, delete, setowner, setpassword, setcolor"); } } diff --git a/src/main/java/eu/m724/utils/chat/ChatListener.java b/src/main/java/eu/m724/tweaks/chat/ChatListener.java similarity index 98% rename from src/main/java/eu/m724/utils/chat/ChatListener.java rename to src/main/java/eu/m724/tweaks/chat/ChatListener.java index 543ab0a..a22305c 100644 --- a/src/main/java/eu/m724/utils/chat/ChatListener.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatListener.java @@ -1,4 +1,4 @@ -package eu.m724.utils.chat; +package eu.m724.tweaks.chat; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; diff --git a/src/main/java/eu/m724/utils/chat/ChatManager.java b/src/main/java/eu/m724/tweaks/chat/ChatManager.java similarity index 99% rename from src/main/java/eu/m724/utils/chat/ChatManager.java rename to src/main/java/eu/m724/tweaks/chat/ChatManager.java index 96d59f3..1a3025a 100644 --- a/src/main/java/eu/m724/utils/chat/ChatManager.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatManager.java @@ -1,4 +1,4 @@ -package eu.m724.utils.chat; +package eu.m724.tweaks.chat; import org.bukkit.NamespacedKey; import org.bukkit.OfflinePlayer; diff --git a/src/main/java/eu/m724/utils/chat/ChatRoom.java b/src/main/java/eu/m724/tweaks/chat/ChatRoom.java similarity index 73% rename from src/main/java/eu/m724/utils/chat/ChatRoom.java rename to src/main/java/eu/m724/tweaks/chat/ChatRoom.java index b9c3017..c963e6d 100644 --- a/src/main/java/eu/m724/utils/chat/ChatRoom.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatRoom.java @@ -1,4 +1,4 @@ -package eu.m724.utils.chat; +package eu.m724.tweaks.chat; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; @@ -6,6 +6,7 @@ import net.md_5.bungee.api.chat.ComponentBuilder; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -37,14 +38,17 @@ public class ChatRoom { if (owner != null) builder = builder.append("\nOwner: ").color(ChatColor.GOLD) .append(owner.getName()).color(ChatColor.AQUA); - builder = builder.append("\nOnline (%d): ".formatted(players.size())).color(ChatColor.GOLD); - List playersList = players.stream().sorted().toList(); - builder = builder.append(playersList.removeFirst().getName()).color(ChatColor.GRAY); + if (!players.isEmpty()) { + builder = builder.append("\nOnline (%d): ".formatted(players.size())).color(ChatColor.GOLD); - for (Player player : playersList) { - builder = builder.append(", ").color(ChatColor.GRAY) - .append(player.getName()).color(ChatColor.AQUA); + List playersList = new ArrayList<>(players); + builder = builder.append(playersList.removeFirst().getName()).color(ChatColor.GRAY); + + for (Player player : playersList) { + builder = builder.append(", ").color(ChatColor.GRAY) + .append(player.getName()).color(ChatColor.AQUA); + } } return builder.create(); diff --git a/src/main/java/eu/m724/utils/chat/ChatRoomLoader.java b/src/main/java/eu/m724/tweaks/chat/ChatRoomLoader.java similarity index 96% rename from src/main/java/eu/m724/utils/chat/ChatRoomLoader.java rename to src/main/java/eu/m724/tweaks/chat/ChatRoomLoader.java index d4fa146..8a93ee7 100644 --- a/src/main/java/eu/m724/utils/chat/ChatRoomLoader.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatRoomLoader.java @@ -1,4 +1,4 @@ -package eu.m724.utils.chat; +package eu.m724.tweaks.chat; import net.md_5.bungee.api.ChatColor; import org.bukkit.Bukkit; @@ -37,7 +37,7 @@ public class ChatRoomLoader { return 2; } else if (!id.equals(id.toLowerCase())) { return 3; - } else if (id.chars().allMatch(Character::isLetterOrDigit)) { + } else if (!id.chars().allMatch(Character::isLetterOrDigit)) { return 4; } diff --git a/src/main/java/eu/m724/tweaks/door/DoorListener.java b/src/main/java/eu/m724/tweaks/door/DoorListener.java new file mode 100644 index 0000000..7bddda7 --- /dev/null +++ b/src/main/java/eu/m724/tweaks/door/DoorListener.java @@ -0,0 +1,113 @@ +package eu.m724.tweaks.door; + +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.data.type.Door; +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.BlockDamageEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.util.RayTraceResult; + +import java.util.concurrent.ThreadLocalRandom; + +public class DoorListener implements Listener { + @EventHandler + public void onBlockDamage(BlockDamageEvent event) { + Block block = event.getBlock(); + if (!(block.getBlockData() instanceof Door door)) return; + + World world = block.getLocation().getWorld(); + Player player = event.getPlayer(); + + // TODO maybe it would be faster to check just player and the door + RayTraceResult result = player.rayTraceBlocks(5); + if (result == null) return; + + Location hitLocation = result.getHitPosition().toLocation(world); + double distance = player.getEyeLocation().distanceSquared(hitLocation); + if (distance > 12) return; + + Sound sound = block.getType() == Material.IRON_DOOR ? Sound.ENTITY_ZOMBIE_ATTACK_IRON_DOOR : Sound.ENTITY_ZOMBIE_ATTACK_WOODEN_DOOR; + float volume = player.isSneaking() ? 0.4f : 1f; + float pitch = player.getFallDistance() > 0 ? 1f : 1.5f; + + if (player.hasPotionEffect(PotionEffectType.NAUSEA)) { + pitch = ThreadLocalRandom.current().nextFloat(0.5f, 0.7f); + } + + PotionEffect weakness = player.getPotionEffect(PotionEffectType.WEAKNESS); + PotionEffect fatigue = player.getPotionEffect(PotionEffectType.MINING_FATIGUE); + int level = (weakness != null ? weakness.getAmplifier() : 0) + (fatigue != null ? fatigue.getAmplifier() : 0); + + if (weakness != null || fatigue != null) { + volume /= level / 3f; + pitch /= level; + } + + volume *= (float) ((10.0 - Math.min(distance - 2, 10.0)) / 10.0); + + world.playSound(hitLocation, sound, volume, pitch); + world.spawnParticle(Particle.BLOCK, hitLocation, (int) (10 * volume), door); + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + // make sure the player is opening a door + if (!event.hasBlock()) return; + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; + Block block = event.getClickedBlock(); + if (!(block.getBlockData() instanceof Door door)) return; + + // check if the doors are in like one line + boolean right = door.getHinge() == Door.Hinge.RIGHT; + Location location = block.getLocation(); + BlockFace expectedFacing = door.getFacing(); + + switch (door.getFacing()) { + case NORTH -> location.add(right ? -1 : 1, 0, 0); + case SOUTH -> location.add(right ? 1 : -1, 0, 0); + case EAST -> location.add(0, 0, right ? -1 : 1); + case WEST -> location.add(0, 0, right ? 1 : -1); + } + + // if not, check if the doors are a corner + if (location.getBlock().isEmpty()) { + location = block.getLocation(); + + switch (door.getFacing()) { + case NORTH -> { + location.add(right ? -1 : 1, 0, 1); + expectedFacing = right ? BlockFace.WEST : BlockFace.EAST; + } + case SOUTH -> { + location.add(right ? 1 : -1, 0, -1); + expectedFacing = right ? BlockFace.EAST : BlockFace.WEST; + } + case EAST -> { + location.add(-1, 0, right ? -1 : 1); + expectedFacing = right ? BlockFace.NORTH : BlockFace.SOUTH; + } + case WEST -> { + location.add(1, 0, right ? 1 : -1); + expectedFacing = right ? BlockFace.SOUTH : BlockFace.NORTH; + } + } + } + + // if there's a second door placed correctly + if (location.getBlock().getType() == block.getType()) { + Door nextDoor = (Door) location.getBlock().getBlockData(); + if (nextDoor.getHinge() == door.getHinge()) return; + if (nextDoor.getFacing() != expectedFacing) return; + // ! because it seems door is actually opening after this event + nextDoor.setOpen(!door.isOpen()); + location.getBlock().setBlockData(nextDoor); + } + } +} diff --git a/src/main/java/eu/m724/utils/UtilsPlugin.java b/src/main/java/eu/m724/utils/UtilsPlugin.java deleted file mode 100644 index a96dc31..0000000 --- a/src/main/java/eu/m724/utils/UtilsPlugin.java +++ /dev/null @@ -1,16 +0,0 @@ -package eu.m724.utils; - -import eu.m724.utils.chat.ChatCommands; -import eu.m724.utils.chat.ChatManager; -import org.bukkit.plugin.java.JavaPlugin; - -public class UtilsPlugin extends JavaPlugin { - @Override - public void onEnable() { - ChatManager chatManager = new ChatManager(this); - chatManager.init(); - - ChatCommands chatCommands = new ChatCommands(chatManager); - getCommand("chat").setExecutor(chatCommands); - } -} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index bdb0b00..07486d6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ -name: mUtils724 +name: Tweaks724 version: ${project.version} -main: eu.m724.utils.UtilsPlugin +main: eu.m724.tweaks.TweaksPlugin api-version: 1.21.1 commands: