commit f3f641aa7c315240672545c824c3524764dd71b7
parent f24da464dcaa3a591124873ac45144a77b27d776
Author: typable <contact@typable.dev>
Date: Wed, 4 Jan 2023 13:37:19 +0100
Organized code
Diffstat:
3 files changed, 228 insertions(+), 178 deletions(-)
diff --git a/src/de/typable/minecrafthub/Main.java b/src/de/typable/minecrafthub/Main.java
@@ -17,7 +17,6 @@ import org.bukkit.WorldType;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
-import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.inventory.meta.BookMeta;
@@ -78,232 +77,225 @@ public class Main extends JavaPlugin
chairListener.onDisable();
}
- @SuppressWarnings("deprecation")
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
if(sender instanceof Player)
{
Player player = (Player) sender;
-
- if(label.equals("head"))
- {
- ItemStack handitem = player.getInventory().getItemInMainHand();
- ItemStack headitem = player.getInventory().getHelmet();
-
- player.getInventory().setHelmet(handitem);
- player.getInventory().setItemInMainHand((new ItemStack(Material.AIR)));
-
- if(player.getGameMode() == GameMode.SURVIVAL)
- {
- if(headitem != null)
- {
- player.getInventory().addItem(headitem);
- }
- }
- }
- if(label.equals("skull"))
+ switch(label)
{
- if(!player.isOp())
- {
- player.sendMessage(Constants.Messages.NOT_ENOUGH_PERMISSION);
- return true;
- }
-
- if(args.length != 1)
- {
+ case "head":
+ return setItemAsHead(player, args);
+ case "skull":
+ return getSkullForName(player, args);
+ case "invsee":
+ return openInventoryForPlayer(player, args);
+ case "info":
+ return openInfoBook(player, args);
+ case "spawn":
+ return teleportToSpawn(player, args);
+ case "home":
+ return teleportToHome(player, args);
+ case "warp":
+ return teleportToWarppoint(player, args);
+ case "sethome":
+ return setHome(player, args);
+ case "setwarp":
+ return addWarppoint(player, args);
+ default:
return false;
- }
-
- ItemStack skull = new ItemStack(Material.PLAYER_HEAD);
- SkullMeta meta = (SkullMeta) skull.getItemMeta();
- meta.setOwner(args[0]);
- skull.setItemMeta(meta);
-
- player.getInventory().addItem(skull);
}
+ }
- if(label.equals("spawn"))
- {
- final Location location = Bukkit.getWorld("world").getSpawnLocation();
+ return true;
+ }
- if(travelTo(player, location))
- {
- player.sendMessage(ChatColor.GRAY + "You've been teleported to spawn.");
- }
- }
+ private boolean setItemAsHead(final Player player, final String[] args)
+ {
+ final ItemStack item = player.getInventory().getItemInMainHand();
+ final ItemStack head = player.getInventory().getHelmet();
- if(label.equals("invsee"))
+ player.getInventory().setHelmet(item);
+ player.getInventory().setItemInMainHand((new ItemStack(Material.AIR)));
+
+ if(player.getGameMode() == GameMode.SURVIVAL)
+ {
+ if(head != null)
{
- if(args.length == 0) {
- return false;
- }
+ player.getInventory().addItem(head);
+ }
+ }
- if(Bukkit.getPlayer(args[0]) == null)
- {
- player.sendMessage(ChatColor.RED + "Player not found!");
- return true;
- }
+ return true;
+ }
+
+ @SuppressWarnings("deprecation")
+ private boolean getSkullForName(final Player player, final String[] args)
+ {
+ if(!player.isOp())
+ {
+ player.sendMessage(Constants.Messages.NOT_ENOUGH_PERMISSION);
+ return true;
+ }
- if(!player.isOp())
- {
- player.sendMessage(Constants.Messages.NOT_ENOUGH_PERMISSION);
- return true;
- }
+ if(args.length != 1)
+ {
+ return false;
+ }
- Player target = Bukkit.getPlayer(args[0]);
+ final String name = args[0];
+ final ItemStack skull = new ItemStack(Material.PLAYER_HEAD);
+ final SkullMeta meta = (SkullMeta) skull.getItemMeta();
+ meta.setOwner(name);
+ skull.setItemMeta(meta);
- player.openInventory(target.getInventory());
+ player.getInventory().addItem(skull);
- if(args.length == 1 && args[1].toLowerCase() == "enderchest")
- {
- player.openInventory(target.getEnderChest());
- }
- }
+ return true;
+ }
- if(label.equals("sethome"))
- {
- try
- {
- config.setHome(player);
- player.sendMessage(ChatColor.YELLOW + "Home point set.");
- }
- catch(Exception ex)
- {
- player.sendMessage(Constants.Messages.FAILED_TO_SAVE_CONFIG_FILE);
- }
- }
+ private boolean openInventoryForPlayer(final Player player, final String[] args)
+ {
+ if(args.length == 0)
+ {
+ return false;
+ }
- if(label.equals("home"))
- {
- final Location location = config.getHome(player);
-
- if(location == null)
- {
- player.sendMessage(ChatColor.RED + "You've don't have a home point.");
- return true;
- }
-
- if(travelTo(player, location))
- {
- player.sendMessage(ChatColor.GRAY + "You've been teleported to your home.");
- }
- }
+ if(!player.isOp())
+ {
+ player.sendMessage(Constants.Messages.NOT_ENOUGH_PERMISSION);
+ return true;
+ }
- if(label.equals("setwarp"))
- {
- if(args.length != 1)
- {
- return false;
- }
-
- final String name = args[0];
-
- if(!payFee(player, Material.COMPASS, 1))
- {
- player.sendMessage(ChatColor.RED + "The fee for creating a warp point is 1 compass!");
- return true;
- }
-
- try
- {
- if(!config.setWarp(name, player.getLocation()))
- {
- player.sendMessage(ChatColor.RED + "Warp point " + name + " already exists!");
- return true;
- }
-
- player.sendMessage(ChatColor.YELLOW + "Warp point " + name + " set.");
- }
- catch(Exception ex)
- {
- player.sendMessage(Constants.Messages.FAILED_TO_SAVE_CONFIG_FILE);
- }
- }
+ final Player target = Bukkit.getPlayer(args[0]);
- if(label.equals("warp"))
- {
- if(args.length != 1)
- {
- return false;
- }
-
- final String name = args[0];
- final Location location = config.getWarp(name);
-
- if(location == null)
- {
- player.sendMessage(ChatColor.RED + "Warp point " + name + " doesn't exist!");
- return true;
- }
-
- if(travelTo(player, location))
- {
- player.sendMessage(ChatColor.GRAY + "You've been teleported to warp point " + name + ".");
- }
- }
+ if(target == null)
+ {
+ player.sendMessage(ChatColor.RED + "Player not found!");
+ return true;
+ }
- if(label.equals("info"))
- {
- final ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
- final BookMeta meta = (BookMeta) book.getItemMeta();
- meta.setTitle("Info");
- meta.setAuthor("Server");
- meta.setPages(config.getInfoPages());
- book.setItemMeta(meta);
+ player.openInventory(target.getInventory());
- player.openBook(book);
- }
+ return true;
+ }
+
+ private boolean openInfoBook(final Player player, final String[] args)
+ {
+ final ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
+ final BookMeta meta = (BookMeta) book.getItemMeta();
+ meta.setTitle("Info");
+ meta.setAuthor("Server");
+ meta.setPages(config.getInfoPages());
+ book.setItemMeta(meta);
+
+ player.openBook(book);
+
+ return true;
+ }
+
+ private boolean teleportToSpawn(final Player player, final String[] args)
+ {
+ final Location location = Bukkit.getWorld("world").getSpawnLocation();
+
+ if(Util.travelTo(plugin, player, location))
+ {
+ player.sendMessage(ChatColor.GRAY + "You've been teleported to spawn.");
}
return true;
}
- private boolean travelTo(final Player player, final Location location)
+ private boolean teleportToHome(final Player player, final String[] args)
{
- final int fee = Util.calcTravelFee(player.getLocation(), location);
- final String unit = fee == 1 ? "emerald" : "emeralds";
+ final Location location = config.getHome(player);
+
+ if(location == null)
+ {
+ player.sendMessage(ChatColor.RED + "You've don't have a home point.");
+ return true;
+ }
+
+ if(Util.travelTo(plugin, player, location))
+ {
+ player.sendMessage(ChatColor.GRAY + "You've been teleported to your home.");
+ }
+
+ return true;
+ }
- if(!payFee(player, Material.EMERALD, fee) && player.getGameMode() == GameMode.SURVIVAL)
+ private boolean teleportToWarppoint(final Player player, final String[] args)
+ {
+ if(args.length != 1)
{
- player.sendMessage(ChatColor.RED + "Not enough emeralds to teleport! Travel fee: " + fee + " " + unit);
return false;
}
+
+ final String name = args[0];
+ final Location location = config.getWarp(name);
- if(player.getVehicle() != null)
+ if(location == null)
{
- Entity vehicle = player.getVehicle();
- vehicle.eject();
- player.teleport(location);
+ player.sendMessage(ChatColor.RED + "Warp point " + name + " doesn't exist!");
+ return true;
+ }
+
+ if(Util.travelTo(plugin, player, location))
+ {
+ player.sendMessage(ChatColor.GRAY + "You've been teleported to warp point " + name + ".");
+ }
- Bukkit.getScheduler().runTaskLater(plugin, () -> {
- vehicle.teleport(location);
- }, 3);
+ return true;
+ }
- Bukkit.getScheduler().runTaskLater(plugin, () -> {
- vehicle.addPassenger(player);
- }, 6);
+ private boolean setHome(final Player player, final String[] args)
+ {
+ try
+ {
+ config.setHome(player);
+ player.sendMessage(ChatColor.YELLOW + "Home point set.");
}
- else
+ catch(Exception ex)
{
- player.teleport(location);
+ player.sendMessage(Constants.Messages.FAILED_TO_SAVE_CONFIG_FILE);
}
return true;
}
- private boolean payFee(final Player player, final Material unit, final int amount)
+ private boolean addWarppoint(final Player player, final String[] args)
{
- final ItemStack item = player.getInventory().getItemInMainHand();
-
- if(item.getType() != unit || item.getAmount() < amount)
+ if(args.length != 1)
{
return false;
}
-
- item.setAmount(item.getAmount() - amount);
+ final String name = args[0];
+
+ if(!Util.payFee(player, Material.COMPASS, 1))
+ {
+ player.sendMessage(ChatColor.RED + "The fee for creating a warp point is 1 compass!");
+ return true;
+ }
+
+ try
+ {
+ if(!config.setWarp(name, player.getLocation()))
+ {
+ player.sendMessage(ChatColor.RED + "Warp point " + name + " already exists!");
+ return true;
+ }
+
+ player.sendMessage(ChatColor.YELLOW + "Warp point " + name + " set.");
+ }
+ catch(Exception ex)
+ {
+ player.sendMessage(Constants.Messages.FAILED_TO_SAVE_CONFIG_FILE);
+ }
+
return true;
}
+
}
diff --git a/src/de/typable/minecrafthub/config/Config.java b/src/de/typable/minecrafthub/config/Config.java
@@ -64,15 +64,23 @@ public class Config
public List<String> getInfoPages()
{
final String path = "info";
+ List<String> pages = null;
try
{
this.configuration.load(this.file);
- return (List<String>) this.configuration.getList(path);
+ pages = (List<String>) this.configuration.getList(path);
}
catch(Exception ex)
{
- return new ArrayList<>();
+ // ignore
}
+
+ if(pages == null)
+ {
+ pages = new ArrayList<>();
+ }
+
+ return pages;
}
}
diff --git a/src/de/typable/minecrafthub/util/Util.java b/src/de/typable/minecrafthub/util/Util.java
@@ -8,10 +8,13 @@ import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
+import org.bukkit.entity.Entity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.Location;
+import org.bukkit.GameMode;
+import org.bukkit.plugin.Plugin;
import de.typable.minecrafthub.constant.Constants;
import net.md_5.bungee.api.ChatColor;
@@ -186,4 +189,51 @@ public class Util
return fee;
}
+
+ public static boolean travelTo(final Plugin plugin, final Player player, final Location location)
+ {
+ final int fee = Util.calcTravelFee(player.getLocation(), location);
+ final String unit = fee == 1 ? "emerald" : "emeralds";
+
+ if(!payFee(player, Material.EMERALD, fee) && player.getGameMode() == GameMode.SURVIVAL)
+ {
+ player.sendMessage(ChatColor.RED + "Not enough emeralds to teleport! Travel fee: " + fee + " " + unit);
+ return false;
+ }
+
+ if(player.getVehicle() != null)
+ {
+ Entity vehicle = player.getVehicle();
+ vehicle.eject();
+ player.teleport(location);
+
+ Bukkit.getScheduler().runTaskLater(plugin, () -> {
+ vehicle.teleport(location);
+ }, 3);
+
+ Bukkit.getScheduler().runTaskLater(plugin, () -> {
+ vehicle.addPassenger(player);
+ }, 6);
+ }
+ else
+ {
+ player.teleport(location);
+ }
+
+ return true;
+ }
+
+ public static boolean payFee(final Player player, final Material unit, final int amount)
+ {
+ final ItemStack item = player.getInventory().getItemInMainHand();
+
+ if(item.getType() != unit || item.getAmount() < amount)
+ {
+ return false;
+ }
+
+ item.setAmount(item.getAmount() - amount);
+
+ return true;
+ }
}