From e4abbae6af086e4ea71bb20e25d29b3ed646ae83 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Wed, 27 Jul 2022 18:20:11 -0400 Subject: [PATCH] blockhunt start --- pom.xml | 2 +- .../net/tylermurphy/hideAndSeek/Main.java | 8 +++ .../hideAndSeek/command/About.java | 2 +- .../hideAndSeek/command/Debug.java | 6 +- .../hideAndSeek/configuration/Config.java | 18 +++++- .../hideAndSeek/game/Disguiser.java | 62 +++++++++++++++++++ .../hideAndSeek/game/PlayerLoader.java | 14 +++++ .../game/listener/DamageHandler.java | 8 +++ .../game/listener/DisguiseHandler.java | 46 ++++++++++++++ .../game/listener/InventoryHandler.java | 41 +++++++++++- src/main/resources/config.yml | 8 +++ src/main/resources/plugin.yml | 2 +- 12 files changed, 210 insertions(+), 7 deletions(-) create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/DisguiseHandler.java diff --git a/pom.xml b/pom.xml index 13e6590..cb6097e 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ 4.0.0 net.tylermurphy KenshinsHideAndSeek - 1.5.1 + 1.6.0 Hide and Seek Plugin UTF-8 diff --git a/src/main/java/net/tylermurphy/hideAndSeek/Main.java b/src/main/java/net/tylermurphy/hideAndSeek/Main.java index 69619f1..dc36752 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/Main.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/Main.java @@ -24,6 +24,7 @@ import net.tylermurphy.hideAndSeek.configuration.Items; import net.tylermurphy.hideAndSeek.configuration.Localization; import net.tylermurphy.hideAndSeek.database.Database; import net.tylermurphy.hideAndSeek.game.Board; +import net.tylermurphy.hideAndSeek.game.Disguiser; import net.tylermurphy.hideAndSeek.game.PlayerLoader; import net.tylermurphy.hideAndSeek.game.util.Status; import net.tylermurphy.hideAndSeek.util.CommandHandler; @@ -56,6 +57,7 @@ public class Main extends JavaPlugin implements Listener { private final Database database; private final Board board; + private final Disguiser disguiser; private Game game; @@ -64,6 +66,7 @@ public class Main extends JavaPlugin implements Listener { onConstructed(); board = new Board(); database = new Database(); + disguiser = new Disguiser(); } protected Main(JavaPluginLoader loader, PluginDescriptionFile description, File dataFolder, File file) { @@ -71,6 +74,7 @@ public class Main extends JavaPlugin implements Listener { onConstructed(); board = new Board(); database = new Database(); + disguiser = new Disguiser(); } private void onConstructed(){ @@ -123,12 +127,14 @@ public class Main extends JavaPlugin implements Listener { private void onTick() { if(game.getStatus() == Status.ENDED) game = new Game(board); game.onTick(); + disguiser.check(); } private void registerListeners() { getServer().getPluginManager().registerEvents(new BlockedCommandHandler(), this); getServer().getPluginManager().registerEvents(new ChatHandler(), this); getServer().getPluginManager().registerEvents(new DamageHandler(), this); + getServer().getPluginManager().registerEvents(new DisguiseHandler(), this); getServer().getPluginManager().registerEvents(new InteractHandler(), this); getServer().getPluginManager().registerEvents(new InventoryHandler(), this); getServer().getPluginManager().registerEvents(new JoinLeaveHandler(), this); @@ -165,6 +171,8 @@ public class Main extends JavaPlugin implements Listener { return game; } + public Disguiser getDisguiser() { return disguiser; } + public boolean supports(int v){ return version >= v; } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/About.java b/src/main/java/net/tylermurphy/hideAndSeek/command/About.java index e36ee84..a4eca9f 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/About.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/About.java @@ -26,7 +26,7 @@ public class About implements ICommand { public void execute(Player sender, String[] args) { sender.sendMessage( - String.format("%s%sHide and Seek %s(%s1.5.1%s)\n", ChatColor.AQUA, ChatColor.BOLD, ChatColor.GRAY,ChatColor.WHITE,ChatColor.GRAY) + + String.format("%s%sHide and Seek %s(%s1.6.0%s)\n", ChatColor.AQUA, ChatColor.BOLD, ChatColor.GRAY,ChatColor.WHITE,ChatColor.GRAY) + String.format("%sAuthor: %s[KenshinEto]\n", ChatColor.GRAY, ChatColor.WHITE) + String.format("%sHelp Command: %s/hs %shelp", ChatColor.GRAY, ChatColor.AQUA, ChatColor.WHITE) ); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Debug.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Debug.java index 1275ceb..7ddc72c 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Debug.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Debug.java @@ -31,7 +31,7 @@ public class Debug implements ICommand { } private void createMenu(){ - debugMenu = Main.getInstance().getServer().createInventory(null, 9, "Debug Menu"); + debugMenu = Main.getInstance().getServer().createInventory(null, 18, "Debug Menu"); debugMenu.setItem(0, createOption(0, XMaterial.LEATHER_CHESTPLATE.parseMaterial(), "&6Become a &lHider", 1, player -> { if(mapSaveEnabled) { if(Bukkit.getWorld(Main.getInstance().getGame().getGameWorld()) == null) Main.getInstance().getGame().getWorldLoader().loadMap(); @@ -79,6 +79,10 @@ public class Debug implements ICommand { debugMenu.setItem(8, createOption(8, Material.ENDER_PEARL, "&d&lTeleport: &fExit", 3, player -> { player.teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); })); + debugMenu.setItem(9, createOption(9, XMaterial.GLASS.parseMaterial(), "&dEnable Disguise", 1, PlayerLoader::openBlockHuntPicker)); + debugMenu.setItem(10, createOption(10, XMaterial.PLAYER_HEAD.parseMaterial(), "&dDisable Disguise", 1, player -> { + Main.getInstance().getDisguiser().reveal(player); + })); } private ItemStack createOption(int slow, Material material, String name, int amount, Consumer callback){ diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java index 123432d..f988d5a 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java @@ -90,7 +90,8 @@ public class Config { leaveOnEnd, mapSaveEnabled, allowNaturalCauses, - saveInventory; + saveInventory, + blockhuntEnabled; public static int minPlayers, @@ -126,6 +127,9 @@ public class Config { blockedCommands, blockedInteracts; + public static List + blockhuntBlocks; + public static String LOBBY_TITLE, GAME_TITLE, @@ -288,6 +292,18 @@ public class Config { } bungeeLeave = config.getString("leaveType") == null || config.getString("leaveType").equalsIgnoreCase("proxy"); leaveServer = config.getString("leaveServer"); + blockhuntEnabled = config.getBoolean("blockhunt.enabled"); + blockhuntBlocks = new ArrayList<>(); + tempInteracts = config.getStringList("blockhunt.blocks"); + for(String id : tempInteracts) { + Optional optional_mat = XMaterial.matchXMaterial(id); + if (optional_mat.isPresent()) { + Material mat = optional_mat.get().parseMaterial(); + if (mat != null) { + blockhuntBlocks.add(mat); + } + } + } //Leaderboard LOBBY_TITLE = leaderboard.getString("lobby.title"); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java new file mode 100644 index 0000000..b4f70ad --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java @@ -0,0 +1,62 @@ +package net.tylermurphy.hideAndSeek.game; + +import org.bukkit.Material; +import org.bukkit.entity.FallingBlock; +import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import java.util.HashMap; +import java.util.Map; + +public class Disguiser { + + private final Map blocks; + + public Disguiser(){ + this.blocks = new HashMap<>(); + } + + public FallingBlock getBlock(Player player){ + return blocks.get(player); + } + + public boolean contains(FallingBlock block) { return blocks.containsValue(block); } + + public boolean disguised(Player player) { return blocks.containsKey(player); } + + public void check(){ + for(Map.Entry set : blocks.entrySet()){ + Player player = set.getKey(); + FallingBlock block = set.getValue(); + if(block.isDead()){ + block.remove(); + FallingBlock replacement = player.getLocation().getWorld().spawnFallingBlock(player.getLocation(), block.getMaterial(), (byte)0); + replacement.setGravity(false); + replacement.setDropItem(false); + blocks.put(player, replacement); + } + } + } + + public void disguise(Player player, Material material){ + if(blocks.containsKey(player)){ + FallingBlock block = blocks.get(player); + block.remove(); + } + FallingBlock block = player.getLocation().getWorld().spawnFallingBlock(player.getLocation(), material, (byte)0); + block.setGravity(false); + block.setDropItem(false); + blocks.put(player, block); + player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false)); + } + + public void reveal(Player player){ + if(!blocks.containsKey(player)) return; + FallingBlock block = blocks.get(player); + block.remove(); + blocks.remove(player); + player.removePotionEffect(PotionEffectType.INVISIBILITY); + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/PlayerLoader.java b/src/main/java/net/tylermurphy/hideAndSeek/game/PlayerLoader.java index bd2a965..2ddfeca 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/PlayerLoader.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/PlayerLoader.java @@ -27,9 +27,11 @@ import net.tylermurphy.hideAndSeek.configuration.Items; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; @@ -45,6 +47,9 @@ public class PlayerLoader { loadPlayer(player); player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,1000000,5,false,false)); Titles.sendTitle(player, 10, 70, 20, ChatColor.WHITE + "" + message("HIDER_TEAM_NAME"), ChatColor.WHITE + message("HIDERS_SUBTITLE").toString()); + if(blockhuntEnabled){ + openBlockHuntPicker(player); + } } public static void loadSeeker(Player player, String gameWorld){ @@ -141,4 +146,13 @@ public class PlayerLoader { } } + public static void openBlockHuntPicker(Player player){ + int slots = ((blockhuntBlocks.size()-1)/9)*9+9; + Inventory inventory = Main.getInstance().getServer().createInventory(null, slots, "Select a Block"); + for(int i=0;i { + teleportEntity(player, block, event.getPlayer().getLocation()); + }); + } + + private void teleportEntity(Player player, FallingBlock block, Location location) { + PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_TELEPORT); + packet.getModifier().writeDefaults(); + packet.getIntegers().write(0, block.getEntityId()); + packet.getDoubles().write(0, location.getX()); + packet.getDoubles().write(1, location.getY()); + packet.getDoubles().write(2, location.getZ()); + try { + protocolManager.sendServerPacket(player, packet); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InventoryHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InventoryHandler.java index f0fb4f9..e72bea0 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InventoryHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InventoryHandler.java @@ -19,26 +19,29 @@ package net.tylermurphy.hideAndSeek.game.listener; +import static net.tylermurphy.hideAndSeek.configuration.Config.*; + import com.cryptomorin.xseries.XMaterial; import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.command.Debug; import net.tylermurphy.hideAndSeek.game.util.Status; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; public class InventoryHandler implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onInventoryClick(InventoryClickEvent event) { if (!(event.getWhoClicked() instanceof Player)) return; - - Player player = (Player) event.getWhoClicked(); checkForInventoryMove(event); checkForSpectatorTeleportMenu(event); checkForDebugMenu(event); + checkForBlockHuntMenu(event); } private void checkForInventoryMove(InventoryClickEvent event){ @@ -74,4 +77,38 @@ public class InventoryHandler implements Listener { } } + private void checkForBlockHuntMenu(InventoryClickEvent event){ + boolean test; + if(Main.getInstance().supports(14)){ + test = event.getView().getTitle().equals("Select a Block"); + } else { + test = event.getInventory().getName().equals("Select a Block"); + } + if(!test) return; + event.setCancelled(true); + Material mat = blockhuntBlocks.get(event.getRawSlot()); + if(mat == null) return; + Player player = (Player) event.getWhoClicked(); + Main.getInstance().getDisguiser().disguise(player, mat); + player.closeInventory(); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onInventoryClose(InventoryCloseEvent event){ + if (!(event.getPlayer() instanceof Player)) return; + boolean test; + if(Main.getInstance().supports(14)){ + test = event.getView().getTitle().equals("Select a Block"); + } else { + test = event.getInventory().getName().equals("Select a Block"); + } + if(!test) return; + Material mat = blockhuntBlocks.get(0); + if(mat == null) return; + Player player = (Player) event.getPlayer(); + if(Main.getInstance().getDisguiser().disguised(player)) return; + Main.getInstance().getDisguiser().disguise(player, mat); + player.closeInventory(); + } + } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d782edb..9500859 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -124,6 +124,14 @@ databaseName: hideandseek # Thw world border starts at [size], and decreases 100 blocks every interval. # x & z are the center location. [enabled] is whenever the border is enabled. # You can choose if Hiders are warned 30 seconds before the border moves. + +# You want block hunt? We have block hunt! Just enable it below, and set the +# available blocks to pick from, and you're all set! It's that easy! +# Items are displayed in the order that they are listed below. +blockhunt: + enabled: false + blocks: [CRAFTING_TABLE, GRASS_BLOCK, DIRT, BEACON, BOOKSHELF] + worldBorder: x: 0 z: 0 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8cd75ee..eab74ad 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: KenshinsHideAndSeek main: net.tylermurphy.hideAndSeek.Main -version: 1.5.1 +version: 1.6.0 author: KenshinEto load: STARTUP api-version: 1.13