commit 5d0099afe37b76dbe6fa7c60021a956056640e79
parent 898b3131b4fc43fb31516b767fdb30b62a54c540
Author: typable <typable.dev@gmail.com>
Date: Fri, 28 May 2021 13:22:26 +0200
Fixed chat, added leaves decay & added Game
Diffstat:
10 files changed, 473 insertions(+), 11 deletions(-)
diff --git a/build.gradle b/build.gradle
@@ -26,7 +26,7 @@ sourceCompatibility = 1.11
targetCompatibility = 1.11
dependencies {
- implementation 'org.spigotmc:spigot-api:1.16.4-R0.1-SNAPSHOT'
+ implementation 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
implementation 'com.google.code.gson:gson:2.8.6'
}
@@ -46,4 +46,9 @@ task deploy {
}
}
}
+}
+
+task local (dependsOn: 'jar', type: Copy) {
+ from project.file('build/libs')
+ into 'E:/run/spigot-16.5/plugins'
}
\ No newline at end of file
diff --git a/res/plugin.yml b/res/plugin.yml
@@ -9,4 +9,5 @@ commands:
skull:
usage: /skull <player>
standby:
- usage: /standby <true, false>
-\ No newline at end of file
+ usage: /standby <true, false>
+ kit:
+\ No newline at end of file
diff --git a/src/de/typable/minecrafthub/Config.java b/src/de/typable/minecrafthub/Config.java
@@ -0,0 +1,40 @@
+package de.typable.minecrafthub;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import com.google.gson.Gson;
+
+
+public class Config
+{
+ private static final Gson gson = new Gson();
+
+ public static Config open(String path)
+ {
+ File file = new File(path);
+
+ try
+ {
+ if(!file.exists())
+ {
+ file.createNewFile();
+ }
+
+ String content = Files.readString(Paths.get(path));
+
+ if(content != null)
+ {
+ // TODO: convert string to json object
+ }
+ }
+ catch(IOException ex)
+ {
+ ex.printStackTrace();
+ }
+
+ return new Config();
+ }
+}
diff --git a/src/de/typable/minecrafthub/Main.java b/src/de/typable/minecrafthub/Main.java
@@ -25,7 +25,9 @@ import de.typable.minecrafthub.event.AutoWorkbenchListener;
import de.typable.minecrafthub.event.ChairListener;
import de.typable.minecrafthub.event.DoubleDoorListener;
import de.typable.minecrafthub.event.EventListener;
+import de.typable.minecrafthub.event.LeavesDecayListener;
import de.typable.minecrafthub.event.StandbyListener;
+import de.typable.minecrafthub.game.kit.KitGame;
import de.typable.minecrafthub.util.Util;
@@ -36,8 +38,11 @@ public class Main extends JavaPlugin
private DoubleDoorListener doubleDoorListener;
private ChairListener chairListener;
private AutoWorkbenchListener autoWorkbenchListener;
+ private LeavesDecayListener leavesDecayListener;
private EventListener eventListener;
+ private KitGame kitGame;
+
private Plugin plugin;
private BukkitTask task;
@@ -60,8 +65,15 @@ public class Main extends JavaPlugin
autoWorkbenchListener = new AutoWorkbenchListener();
pluginManager.registerEvents(autoWorkbenchListener, this);
+ leavesDecayListener = new LeavesDecayListener(this);
+ pluginManager.registerEvents(leavesDecayListener, this);
+
eventListener = new EventListener();
pluginManager.registerEvents(eventListener, this);
+
+ kitGame = new KitGame();
+ this.getCommand("kit").setExecutor(kitGame);
+ pluginManager.registerEvents(kitGame, this);
task = Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable()
{
@@ -122,8 +134,8 @@ public class Main extends JavaPlugin
{
Player player = (Player) sender;
- if(label.equals("shutdown")) {
-
+ if(label.equals("shutdown"))
+ {
if(!player.isOp())
{
player.sendMessage(DefaultConstants.Messages.NOT_ENOUGH_PERMISSION);
diff --git a/src/de/typable/minecrafthub/constant/DefaultConstants.java b/src/de/typable/minecrafthub/constant/DefaultConstants.java
@@ -10,5 +10,7 @@ public class DefaultConstants
public static final class Messages
{
public static final String NOT_ENOUGH_PERMISSION = ChatColor.RED + "You don't have enough Permission to perform this command!";
+ public static final String ALREADY_INGAME = ChatColor.RED + "You're already ingame!";
+ public static final String NOT_INGAME = ChatColor.RED + "You're not ingame!";
}
}
diff --git a/src/de/typable/minecrafthub/event/EventListener.java b/src/de/typable/minecrafthub/event/EventListener.java
@@ -49,9 +49,8 @@ public class EventListener implements Listener
@EventHandler
public void onChat(AsyncPlayerChatEvent event)
{
- // FIXME Conversion Exception on '%
-
- String format = ChatColor.WHITE + event.getPlayer().getName() + ": " + ChatColor.GRAY + event.getMessage();
+ String message = ChatColor.GRAY + event.getMessage().replace("%", "%%");
+ String format = ChatColor.WHITE + event.getPlayer().getName() + ": " + message;
event.setFormat(format);
}
@@ -86,9 +85,9 @@ public class EventListener implements Listener
ArmorStand armorstand = (ArmorStand) event.getRightClicked();
armorstand.setArms(true);
- event.setCancelled(true);
-
item.setAmount(item.getAmount() - 2);
+
+ event.setCancelled(true);
}
}
}
@@ -100,7 +99,8 @@ public class EventListener implements Listener
{
ArmorStand armorstand = (ArmorStand) event.getEntity();
- if(armorstand.hasArms()){
+ if(armorstand.hasArms())
+ {
event.getDrops().add(new ItemStack(Material.STICK, 2));
}
}
diff --git a/src/de/typable/minecrafthub/event/LeavesDecayListener.java b/src/de/typable/minecrafthub/event/LeavesDecayListener.java
@@ -0,0 +1,124 @@
+package de.typable.minecrafthub.event;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.bukkit.Tag;
+import org.bukkit.block.Block;
+import org.bukkit.block.BlockFace;
+import org.bukkit.block.data.type.Leaves;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.BlockBreakEvent;
+import org.bukkit.event.block.LeavesDecayEvent;
+import org.bukkit.plugin.Plugin;
+
+public class LeavesDecayListener implements Listener
+{
+ private static final int DELAY = 50;
+ private static final List<BlockFace> NEIGHBORS = Arrays.asList(
+ BlockFace.UP,
+ BlockFace.NORTH,
+ BlockFace.EAST,
+ BlockFace.SOUTH,
+ BlockFace.WEST,
+ BlockFace.DOWN
+ );
+
+ private Plugin plugin;
+ private List<Block> scheduled = new ArrayList<>();
+
+ public LeavesDecayListener(Plugin plugin)
+ {
+ this.plugin = plugin;
+ }
+
+ @EventHandler
+ public void onBlockBreak(BlockBreakEvent event)
+ {
+ onBlockRemove(event.getBlock());
+ }
+
+ @EventHandler
+ public void onLeavesDecay(LeavesDecayEvent event)
+ {
+ onBlockRemove(event.getBlock());
+ }
+
+ private void onBlockRemove(Block block)
+ {
+ if(!Tag.LOGS.isTagged(block.getType()) && !Tag.LEAVES.isTagged(block.getType()))
+ {
+ return;
+ }
+
+ Collections.shuffle(NEIGHBORS);
+
+ for(BlockFace face : NEIGHBORS)
+ {
+ final Block neighbor = block.getRelative(face);
+
+ if(!Tag.LEAVES.isTagged(neighbor.getType()))
+ {
+ continue;
+ }
+
+ Leaves leaves = (Leaves) neighbor.getBlockData();
+
+ if(leaves.isPersistent())
+ {
+ continue;
+ }
+
+ if(scheduled.contains(neighbor))
+ {
+ continue;
+ }
+
+ plugin.getServer().getScheduler().runTaskLater(plugin, () -> decay(neighbor), DELAY);
+
+ scheduled.add(neighbor);
+ }
+ }
+
+ private boolean decay(Block block)
+ {
+ scheduled.remove(block);
+
+ if(!block.getWorld().isChunkLoaded(block.getX() >> 4, block.getZ() >> 4))
+ {
+ return false;
+ }
+
+ if(!Tag.LEAVES.isTagged(block.getType()))
+ {
+ return false;
+ }
+
+ Leaves leaves = (Leaves) block.getBlockData();
+
+ if(leaves.isPersistent())
+ {
+ return false;
+ }
+
+ if(leaves.getDistance() < 7)
+ {
+ return false;
+ }
+
+ LeavesDecayEvent event = new LeavesDecayEvent(block);
+ plugin.getServer().getPluginManager().callEvent(event);
+
+ if(event.isCancelled())
+ {
+ return false;
+ }
+
+ block.breakNaturally();
+
+ return true;
+ }
+}
diff --git a/src/de/typable/minecrafthub/game/Game.java b/src/de/typable/minecrafthub/game/Game.java
@@ -0,0 +1,86 @@
+package de.typable.minecrafthub.game;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.bukkit.command.CommandExecutor;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Listener;
+
+import de.typable.minecrafthub.Config;
+import de.typable.minecrafthub.constant.DefaultConstants;
+
+public abstract class Game implements CommandExecutor, Listener
+{
+ private Map<Player, PlayerState> players;
+ private Config config;
+
+ public Game()
+ {
+ players = new HashMap<>();
+ }
+
+ public void join(Player player)
+ {
+ if(players.containsKey(player))
+ {
+ player.sendMessage(DefaultConstants.Messages.ALREADY_INGAME);
+
+ return;
+ }
+
+ players.put(player, PlayerState.of(player));
+ }
+
+ public void leave(Player player)
+ {
+ PlayerState playerState = players.get(player);
+
+ if(playerState == null)
+ {
+ player.sendMessage(DefaultConstants.Messages.NOT_INGAME);
+
+ return;
+ }
+
+ playerState.apply(player);
+ players.remove(player);
+ }
+
+ public void leaveAll()
+ {
+ Iterator<Entry<Player, PlayerState>> iterator = players.entrySet().iterator();
+
+ while(iterator.hasNext())
+ {
+ Entry<Player, PlayerState> entry = iterator.next();
+ Player player = entry.getKey();
+ PlayerState playerState = entry.getValue();
+
+ playerState.apply(player);
+ iterator.remove();
+ }
+ }
+
+ public boolean isIngame(Player player)
+ {
+ return player != null && players.containsKey(player);
+ }
+
+ public Map<Player, PlayerState> getPlayers()
+ {
+ return players;
+ }
+
+ public void setConfig(Config config)
+ {
+ this.config = config;
+ }
+
+ public Config getConfig()
+ {
+ return config;
+ }
+}
diff --git a/src/de/typable/minecrafthub/game/PlayerState.java b/src/de/typable/minecrafthub/game/PlayerState.java
@@ -0,0 +1,133 @@
+package de.typable.minecrafthub.game;
+
+import java.util.UUID;
+
+import org.bukkit.GameMode;
+import org.bukkit.Location;
+import org.bukkit.entity.Player;
+import org.bukkit.inventory.ItemStack;
+
+public class PlayerState
+{
+ private UUID uuid;
+ private Double health;
+ private Integer foodLevel;
+ private Integer level;
+ private Float exp;
+ private Location location;
+ private GameMode gameMode;
+ private ItemStack[] equipment;
+
+ public static PlayerState of(Player player)
+ {
+ PlayerState playerState = new PlayerState();
+
+ playerState.setUUID(player.getUniqueId());
+ playerState.setHealth(player.getHealth());
+ playerState.setFoodLevel(player.getFoodLevel());
+ playerState.setLevel(player.getLevel());
+ playerState.setExp(player.getExp());
+ playerState.setLocation(player.getLocation());
+ playerState.setGameMode(player.getGameMode());
+ playerState.setEquipment(player.getInventory().getContents());
+
+ return playerState;
+ }
+
+ public void apply(Player player)
+ {
+ if(!player.getUniqueId().equals(uuid))
+ {
+ throw new IllegalArgumentException("Only the player with the same UUID as the state can recieve it!");
+ }
+
+ player.setHealth(health);
+ player.setFoodLevel(foodLevel);
+ player.setLevel(level);
+ player.setExp(exp);
+ player.teleport(location);
+ player.setGameMode(gameMode);
+ player.getInventory().setContents(equipment);
+ // equipment
+ }
+
+ public UUID getUUID()
+ {
+ return uuid;
+ }
+
+ public void setUUID(UUID uuid)
+ {
+ this.uuid = uuid;
+ }
+
+ public Double getHealth()
+ {
+ return health;
+ }
+
+ public void setHealth(Double health)
+ {
+ this.health = health;
+ }
+
+ public Integer getFoodLevel()
+ {
+ return foodLevel;
+ }
+
+ public void setFoodLevel(Integer foodLevel)
+ {
+ this.foodLevel = foodLevel;
+ }
+
+ public Integer getLevel()
+ {
+ return level;
+ }
+
+ public void setLevel(Integer level)
+ {
+ this.level = level;
+ }
+
+ public Float getExp()
+ {
+ return exp;
+ }
+
+ public void setExp(Float exp)
+ {
+ this.exp = exp;
+ }
+
+ public Location getLocation()
+ {
+ return location;
+ }
+
+ public void setLocation(Location location)
+ {
+ this.location = location;
+ }
+
+ public GameMode getGameMode()
+ {
+ return gameMode;
+ }
+
+ public void setGameMode(GameMode gameMode)
+ {
+ this.gameMode = gameMode;
+ }
+
+ public ItemStack[] getEquipment()
+ {
+ return equipment;
+ }
+
+ public void setEquipment(ItemStack[] equipment)
+ {
+ this.equipment = equipment;
+ }
+}
diff --git a/src/de/typable/minecrafthub/game/kit/KitGame.java b/src/de/typable/minecrafthub/game/kit/KitGame.java
@@ -0,0 +1,59 @@
+package de.typable.minecrafthub.game.kit;
+
+import org.bukkit.GameMode;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.entity.Player;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.block.BlockBreakEvent;
+
+import de.typable.minecrafthub.game.Game;
+
+public class KitGame extends Game
+{
+ public KitGame()
+ {
+ super();
+ }
+
+ @Override
+ public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
+ {
+ if(sender instanceof Player)
+ {
+ Player player = (Player) sender;
+
+ if(args.length > 0)
+ {
+ if(args[0].equals("join"))
+ {
+ join(player);
+ }
+
+ if(args[0].equals("leave"))
+ {
+ leave(player);
+ }
+ }
+ }
+
+ return false;
+ }
+
+ @Override
+ public void join(Player player)
+ {
+ super.join(player);
+ player.setGameMode(GameMode.SURVIVAL);
+ player.getInventory().clear();
+ }
+
+ @EventHandler
+ public void onBlockBreak(BlockBreakEvent event)
+ {
+ if(isIngame(event.getPlayer()))
+ {
+ event.setCancelled(true);
+ }
+ }
+}