From 04296bdb041434128b5e1dc136225525b8217ca2 Mon Sep 17 00:00:00 2001 From: PauMAVA Date: Thu, 26 Dec 2019 13:38:59 +0100 Subject: [PATCH] Added plugin base structure --- pom.xml | 82 +++++++++++++++++++ src/me/PauMAVA/TTR/TTRCore.java | 39 +++++++++ src/me/PauMAVA/TTR/match/TTRMatch.java | 23 ++++++ src/me/PauMAVA/TTR/teams/TTRTeam.java | 46 +++++++++++ src/me/PauMAVA/TTR/teams/TTRTeamHandler.java | 49 +++++++++++ src/me/PauMAVA/TTR/ui/CustomUI.java | 56 +++++++++++++ src/me/PauMAVA/TTR/ui/TeamSelector.java | 22 +++++ src/me/PauMAVA/TTR/util/EventListener.java | 79 ++++++++++++++++++ .../PauMAVA/TTR/util/PacketIntercepter.java | 41 ++++++++++ src/me/PauMAVA/TTR/util/TTRPrefix.java | 20 +++++ src/plugin.yml | 4 + 11 files changed, 461 insertions(+) create mode 100644 pom.xml create mode 100644 src/me/PauMAVA/TTR/TTRCore.java create mode 100644 src/me/PauMAVA/TTR/match/TTRMatch.java create mode 100644 src/me/PauMAVA/TTR/teams/TTRTeam.java create mode 100644 src/me/PauMAVA/TTR/teams/TTRTeamHandler.java create mode 100644 src/me/PauMAVA/TTR/ui/CustomUI.java create mode 100644 src/me/PauMAVA/TTR/ui/TeamSelector.java create mode 100644 src/me/PauMAVA/TTR/util/EventListener.java create mode 100644 src/me/PauMAVA/TTR/util/PacketIntercepter.java create mode 100644 src/me/PauMAVA/TTR/util/TTRPrefix.java create mode 100644 src/plugin.yml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7b6d069 --- /dev/null +++ b/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + + me.PauMAVA + TheTowersRemastered + pre-0.1 + + src + + + src + + **/*.yml + + + + + + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + lib + me.PauMAVA.TTR.TTRCore + + + + + + maven-antrun-plugin + 1.8 + + + copy + package + + run + + false + + + Copying .jar to Development Server.... + + + + + + + + + + + + + + + PauMAVen + https://raw.githubusercontent.com/PauMAVA/PauMAVen/master + + + + + me.PauMAVA + craftbukkit + 1.15 + provided + + + \ No newline at end of file diff --git a/src/me/PauMAVA/TTR/TTRCore.java b/src/me/PauMAVA/TTR/TTRCore.java new file mode 100644 index 0000000..18a4b25 --- /dev/null +++ b/src/me/PauMAVA/TTR/TTRCore.java @@ -0,0 +1,39 @@ +package me.PauMAVA.TTR; + +import me.PauMAVA.TTR.match.TTRMatch; +import me.PauMAVA.TTR.util.EventListener; +import org.bukkit.plugin.java.JavaPlugin; + +public class TTRCore extends JavaPlugin { + + private static TTRCore instance; + private boolean enabled = false; + private TTRMatch match; + + @Override + public void onEnable() { + instance = this; + if(this.getConfig().getBoolean("enableOnStart")) { + enabled = true; + } + this.match = new TTRMatch(); + this.getServer().getPluginManager().registerEvents(new EventListener(), this); + } + + @Override + public void onDisable() { + + } + + public static TTRCore getInstance() { + return instance; + } + + public boolean enabled() { + return this.enabled; + } + + public TTRMatch getCurrentMatch() { + return this.match; + } +} diff --git a/src/me/PauMAVA/TTR/match/TTRMatch.java b/src/me/PauMAVA/TTR/match/TTRMatch.java new file mode 100644 index 0000000..124d544 --- /dev/null +++ b/src/me/PauMAVA/TTR/match/TTRMatch.java @@ -0,0 +1,23 @@ +package me.PauMAVA.TTR.match; + +public class TTRMatch { + + private boolean isOnCourse; + + public TTRMatch() { + isOnCourse = false; + } + + public boolean isOnCourse() { + return this.isOnCourse; + } + + public void startMatch() { + + } + + public void endMatch() { + + } + +} diff --git a/src/me/PauMAVA/TTR/teams/TTRTeam.java b/src/me/PauMAVA/TTR/teams/TTRTeam.java new file mode 100644 index 0000000..1c11fd6 --- /dev/null +++ b/src/me/PauMAVA/TTR/teams/TTRTeam.java @@ -0,0 +1,46 @@ +package me.PauMAVA.TTR.teams; + +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class TTRTeam { + + private String identifier; + private List players = new ArrayList(); + private int points = 0; + + public TTRTeam(String identifier, List players) { + this.identifier = identifier; + this.players = players; + } + + public TTRTeam(String identifier) { + this.identifier = identifier; + } + + public String getIdentifier() { + return this.identifier; + } + + public void addPlayer(Player player) { + this.players.add(player); + } + + public void removePlayer(Player player) { + this.players.remove(player); + } + + public List getPlayers() { + return this.players; + } + + public void addPoints(int points) { + this.points += points; + } + + public int getPoints() { + return this.points; + } +} diff --git a/src/me/PauMAVA/TTR/teams/TTRTeamHandler.java b/src/me/PauMAVA/TTR/teams/TTRTeamHandler.java new file mode 100644 index 0000000..0c93525 --- /dev/null +++ b/src/me/PauMAVA/TTR/teams/TTRTeamHandler.java @@ -0,0 +1,49 @@ +package me.PauMAVA.TTR.teams; + +import me.PauMAVA.TTR.match.TTRMatch; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class TTRTeamHandler { + + private TTRMatch match = new TTRMatch(); + private List teams = new ArrayList(); + + public void setUpDefaultTeams() { + teams.add(new TTRTeam("red")); + teams.add(new TTRTeam("blue")); + } + + public void setUpCustomTeams(List customTeams) { + this.teams = customTeams; + } + + public boolean addPlayerToTeam(Player player, String teamIdentifier) { + TTRTeam team = getTeam(teamIdentifier); + if(team == null) { + return false; + } + team.addPlayer(player); + return true; + } + + public boolean removePlayerFromTeam(Player player, String teamIdentifier) { + TTRTeam team = getTeam(teamIdentifier); + if(team == null) { + return false; + } + team.removePlayer(player); + return true; + } + + private TTRTeam getTeam(String teamIdentifier) { + for(TTRTeam team: this.teams) { + if(team.getIdentifier().equalsIgnoreCase(teamIdentifier)) { + return team; + } + } + return null; + } +} diff --git a/src/me/PauMAVA/TTR/ui/CustomUI.java b/src/me/PauMAVA/TTR/ui/CustomUI.java new file mode 100644 index 0000000..4b4fa19 --- /dev/null +++ b/src/me/PauMAVA/TTR/ui/CustomUI.java @@ -0,0 +1,56 @@ +package me.PauMAVA.TTR.ui; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.Arrays; + +public class CustomUI { + + private Inventory inventory; + private String title; + private int size; + + CustomUI(int size, String title) { + this.inventory = Bukkit.getServer().createInventory(null, size, title); + } + + void openUI(Player player) { + player.openInventory(this.inventory); + } + + void closeUI(Player player) { + if(player.getOpenInventory().equals(this.inventory)) { + player.closeInventory(); + } + } + + public void setSlot(int id, ItemStack item, @Nullable String title, @Nullable String lore) { + if(title == null) { + this.inventory.setItem(id, item); + return; + } + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(title); + if(lore != null) { + meta.setLore(new ArrayList(Arrays.asList(lore))); + } + item.setItemMeta(meta); + this.inventory.setItem(id, item); + } + + public void clearSlot(int id) { + this.inventory.clear(id); + } + + public void clearUI() { + this.inventory.clear(); + } + +} diff --git a/src/me/PauMAVA/TTR/ui/TeamSelector.java b/src/me/PauMAVA/TTR/ui/TeamSelector.java new file mode 100644 index 0000000..8b0eb84 --- /dev/null +++ b/src/me/PauMAVA/TTR/ui/TeamSelector.java @@ -0,0 +1,22 @@ +package me.PauMAVA.TTR.ui; + +import org.bukkit.entity.Player; + +public class TeamSelector extends CustomUI { + + private Player owner; + + public TeamSelector(Player player) { + super(27, "Team Selection"); + this.owner = player; + } + + public void openSelector() { + super.openUI(this.owner); + } + + public void closeSelector() { + super.closeUI(this.owner); + } + +} diff --git a/src/me/PauMAVA/TTR/util/EventListener.java b/src/me/PauMAVA/TTR/util/EventListener.java new file mode 100644 index 0000000..7beff7c --- /dev/null +++ b/src/me/PauMAVA/TTR/util/EventListener.java @@ -0,0 +1,79 @@ +package me.PauMAVA.TTR.util; + +import com.mojang.datafixers.kinds.IdF; +import me.PauMAVA.TTR.TTRCore; +import me.PauMAVA.TTR.ui.TeamSelector; +import org.bukkit.ChatColor; +import org.bukkit.Material; +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.inventory.InventoryClickEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.scoreboard.Team; + +public class EventListener implements Listener { + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + if(TTRCore.getInstance().enabled()) { + Inventory playerInventory = event.getPlayer().getInventory(); + playerInventory.clear(); + playerInventory.setItem(0, new ItemStack(Material.BLACK_BANNER)); + } + } + + @EventHandler + public void onPlayerLeave(PlayerQuitEvent event) { + + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent event) { + if(TTRCore.getInstance().enabled() && !TTRCore.getInstance().getCurrentMatch().isOnCourse()) { + event.setCancelled(true); + } + } + + @EventHandler + public void onPlayerDropEvent(PlayerDropItemEvent event) { + if(TTRCore.getInstance().enabled() && !TTRCore.getInstance().getCurrentMatch().isOnCourse()) { + event.setCancelled(true); + } + } + + @EventHandler + public void playerClickEvent(PlayerInteractEvent event) { + if(TTRCore.getInstance().enabled() && !TTRCore.getInstance().getCurrentMatch().isOnCourse()) { + event.setCancelled(true); + if(event.getItem() != null && event.getItem().getType() == Material.BLACK_BANNER) { + new TeamSelector(event.getPlayer()).openSelector(); + } + } + } + + @EventHandler + public void placeBlockEvent(BlockPlaceEvent event) { + if(TTRCore.getInstance().enabled() && !TTRCore.getInstance().getCurrentMatch().isOnCourse()) { + event.getPlayer().sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GRAY + "You cannot break that block!"); + event.setCancelled(true); + } + } + + @EventHandler + public void breakBlockEvent(BlockBreakEvent event) { + if(TTRCore.getInstance().enabled() && !TTRCore.getInstance().getCurrentMatch().isOnCourse()) { + event.getPlayer().sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GRAY + "You cannot place a block there!"); + event.setCancelled(true); + } + } + + +} diff --git a/src/me/PauMAVA/TTR/util/PacketIntercepter.java b/src/me/PauMAVA/TTR/util/PacketIntercepter.java new file mode 100644 index 0000000..d4d1a46 --- /dev/null +++ b/src/me/PauMAVA/TTR/util/PacketIntercepter.java @@ -0,0 +1,41 @@ +package me.PauMAVA.TTR.util; + +import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; +import net.minecraft.server.v1_15_R1.PacketPlayOutChat; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.entity.Player; + +public class PacketIntercepter { + + public void addPlayer(Player player) { + ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler(){ + @Override + public void channelRead(ChannelHandlerContext context, Object packet) { + if(packet instanceof PacketPlayOutChat) { + + } + + try{ + super.channelRead(context, packet); + } catch (Exception e) { + + e.printStackTrace(); + } + } + + @Override + public void write(ChannelHandlerContext context, Object packet, ChannelPromise promise) { + + } + }; + ChannelPipeline pipeline = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel.pipeline(); + try { + pipeline.addBefore("packet_handler", player.getName(), channelDuplexHandler); + } catch (IllegalArgumentException ignored) {} + } + + +} diff --git a/src/me/PauMAVA/TTR/util/TTRPrefix.java b/src/me/PauMAVA/TTR/util/TTRPrefix.java new file mode 100644 index 0000000..384f862 --- /dev/null +++ b/src/me/PauMAVA/TTR/util/TTRPrefix.java @@ -0,0 +1,20 @@ +package me.PauMAVA.TTR.util; + +import org.bukkit.ChatColor; + +public enum TTRPrefix { + + TTR_GAME(ChatColor.GRAY + "" + ChatColor.BOLD + "[" + ChatColor.RESET + ChatColor.GREEN + "" + ChatColor.BOLD + "The Towers" + ChatColor.RESET + ChatColor.GRAY + "" + ChatColor.BOLD + "]" + ChatColor.RESET + " "); + + private String prefix; + + TTRPrefix(String prefix) { + this.prefix = prefix; + } + + @Override + public String toString() { + return this.prefix; + } + +} diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..007a7e6 --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,4 @@ +name: TheTowersRemastered +version: pre-0.1 +api-version: 1.15 +main: me.PauMAVA.TTR.TTRCore \ No newline at end of file