QUOTE: Never too old to chase dreams.

Added warp and home teleportation - poppy - A feature-rich Minecraft plugin which enhances gaming experience

poppy

A feature-rich Minecraft plugin which enhances gaming experience
git clone git://192.168.2.2/poppy
Log | Files | Refs | README

commit 317d6a086e537d286a1d221fb19065915904ab1d
parent 97e1405fc6fa1437434a5a819ac7140909109a7e
Author: typable <contact@typable.dev>
Date:   Tue,  3 Jan 2023 16:45:05 +0100

Added warp and home teleportation

Diffstat:
MREADME.md | 17-----------------
Mbuild.gradle | 25+------------------------
Mres/plugin.yml | 15+++++++--------
Msrc/de/typable/minecrafthub/Main.java | 281++++++++++++++++++++++++++++++++++---------------------------------------------
Asrc/de/typable/minecrafthub/config/Config.java | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/de/typable/minecrafthub/constant/Constants.java | 19+++++++++++++++++++
Dsrc/de/typable/minecrafthub/constant/DefaultConstants.java | 17-----------------
Msrc/de/typable/minecrafthub/event/EventListener.java | 3+--
Dsrc/de/typable/minecrafthub/event/StandbyListener.java | 79-------------------------------------------------------------------------------
Msrc/de/typable/minecrafthub/util/Util.java | 33+++++++++++++++++++++++++++++++--
10 files changed, 242 insertions(+), 308 deletions(-)

diff --git a/README.md b/README.md @@ -1,17 +1,6 @@ # minecraft-hub A Minecraft Server Management Plugin -### Authentication - -```bash -ssh-keygen // no password -eval $(ssh-agent) -ssh-add ~/.ssh/id_rsa -scp ~/.ssh/id_rsa.pub <user>@<host>:~/.ssh/authorized_keys -``` - -More information: [Passwordless-SSH-Login](https://endjin.com/blog/2019/09/passwordless-ssh-from-windows-10-to-raspberry-pi) - ### Export as Jar File ```bash @@ -25,9 +14,3 @@ Custom configuration required! ```bash gradle task local ``` - -### Transfer to Remote Server - -```bash -gradle task deploy -``` diff --git a/build.gradle b/build.gradle @@ -1,7 +1,3 @@ -plugins { - id 'org.hidetake.ssh' version '2.10.1' -} - apply plugin: 'java' sourceSets { @@ -27,28 +23,9 @@ targetCompatibility = 1.11 dependencies { implementation 'org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT' - implementation 'com.google.code.gson:gson:2.8.6' -} - -remotes { - server { - host = 'mc.typable.dev' - user = 'ubuntu' - identity = file('.env/id_rsa') - } -} - -task deploy { - doLast { - ssh.run { - session(remotes.server) { - put from: project.file('build/libs/MinecraftHub.jar'), into: '/home/ubuntu/run/mc/plugins' - } - } - } } task local (dependsOn: 'jar', type: Copy) { from project.file('build/libs') - into 'E:/run/spigot-17/plugins' + into '/Users/andreas/Documents/run/minecraft/plugins' } \ No newline at end of file diff --git a/res/plugin.yml b/res/plugin.yml @@ -4,16 +4,16 @@ version: 0.0.1 author: typable api-version: 1.16 commands: - shutdown: head: skull: usage: /skull <player> - standby: - usage: /standby <true, false> - kit: - world: - usage: /world <name> spawn: usage: /spawn invsee: - usage: /invsee <name> <enderchest> -\ No newline at end of file + usage: /invsee <name> <enderchest> + sethome: + home: + setwarp: + usage: /setwarp <name> + warp: + usage: /warp <name> diff --git a/src/de/typable/minecrafthub/Main.java b/src/de/typable/minecrafthub/Main.java @@ -17,28 +17,27 @@ 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.material.Tree; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitTask; -import de.typable.minecrafthub.constant.DefaultConstants; +import de.typable.minecrafthub.constant.Constants; 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.util.Util; +import de.typable.minecrafthub.config.Config; public class Main extends JavaPlugin { private PluginManager pluginManager; - private StandbyListener standbyListener; private DoubleDoorListener doubleDoorListener; private ChairListener chairListener; private AutoWorkbenchListener autoWorkbenchListener; @@ -46,23 +45,16 @@ public class Main extends JavaPlugin private EventListener eventListener; private Plugin plugin; - private BukkitTask task; - - private static final int BLOCKS_PER_CHUNK = 16; - private static final int CHUNKS_PER_EMERALD = 25; - private static final int MIN_FEE = 1; - private static final int MAX_FEE = 64; + private Config config; @Override public void onEnable() { plugin = this; + config = new Config("config/minecraft-hub.yml"); pluginManager = Bukkit.getPluginManager(); - standbyListener = new StandbyListener(this); - pluginManager.registerEvents(standbyListener, this); - doubleDoorListener = new DoubleDoorListener(); pluginManager.registerEvents(doubleDoorListener, this); @@ -77,60 +69,11 @@ public class Main extends JavaPlugin eventListener = new EventListener(); pluginManager.registerEvents(eventListener, this); - - task = Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() - { - @Override - public void run() - { - try(ServerSocket serverSocket = new ServerSocket(25560)) - { - while(!serverSocket.isClosed()) - { - Socket socket = serverSocket.accept(); - - Util.sendCountdown(plugin, "Server restarts in", 5, new Runnable() - { - @Override - public void run() - { - for(Player target : Bukkit.getOnlinePlayers()) - { - target.kickPlayer("Server restarting..."); - } - - Bukkit.getServer().shutdown(); - } - }); - - socket.close(); - } - } - catch(BindException ex) - { - // ignore - } - catch(IOException ex) - { - ex.printStackTrace(); - } - } - }); - - new WorldCreator("creative") - .type(WorldType.FLAT) - .generateStructures(false) - .createWorld(); } @Override public void onDisable() { - if(task != null && task.isCancelled()) - { - task.cancel(); - } - chairListener.onDisable(); } @@ -141,42 +84,6 @@ public class Main extends JavaPlugin if(sender instanceof Player) { Player player = (Player) sender; - - if(label.equals("shutdown")) - { - if(!player.isOp()) - { - player.sendMessage(DefaultConstants.Messages.NOT_ENOUGH_PERMISSION); - return true; - } - - String path = new File(".").getAbsolutePath(); - - File file = new File(path + "/.shutdown"); - - try - { - file.createNewFile(); - - Util.sendCountdown(this, "Server shuts down in", 5, new Runnable() - { - @Override - public void run() - { - for(Player target : Bukkit.getOnlinePlayers()) - { - target.kickPlayer("Server stopped"); - } - - Bukkit.getServer().shutdown(); - } - }); - } - catch(IOException ex) - { - ex.printStackTrace(); - } - } if(label.equals("head")) { @@ -199,7 +106,7 @@ public class Main extends JavaPlugin { if(!player.isOp()) { - player.sendMessage(DefaultConstants.Messages.NOT_ENOUGH_PERMISSION); + player.sendMessage(Constants.Messages.NOT_ENOUGH_PERMISSION); return true; } @@ -216,118 +123,174 @@ public class Main extends JavaPlugin player.getInventory().addItem(skull); } - if(label.equals("standby")) + if(label.equals("spawn")) { - if(!player.isOp()) + final Location location = Bukkit.getWorld("world").getSpawnLocation(); + + if(travelTo(player, location)) { - player.sendMessage(DefaultConstants.Messages.NOT_ENOUGH_PERMISSION); - return true; + player.sendMessage(ChatColor.GRAY + "You've been teleported to spawn."); } + } - if(args.length != 1) - { + if(label.equals("invsee")) + { + if(args.length == 0) { return false; } - String argument = args[0]; + if(Bukkit.getPlayer(args[0]) == null) + { + player.sendMessage(ChatColor.RED + "Player not found!"); + return true; + } - if(!argument.equals("true") && !argument.equals("false")) + if(!player.isOp()) { - return false; + player.sendMessage(Constants.Messages.NOT_ENOUGH_PERMISSION); + return true; } - boolean enabled = argument.equals("true"); - standbyListener.setEnabled(enabled); + Player target = Bukkit.getPlayer(args[0]); + + player.openInventory(target.getInventory()); - player.sendMessage(ChatColor.GRAY + "Standby mode is now " + (enabled ? "enabled" : "disabled")); + if(args.length == 1 && args[1].toLowerCase() == "enderchest") + { + player.openInventory(target.getEnderChest()); + } } - if(label.equals("world")) + if(label.equals("sethome")) { - if(!player.isOp()) + try { - player.sendMessage(DefaultConstants.Messages.NOT_ENOUGH_PERMISSION); - return true; + config.setHome(player); + player.sendMessage(ChatColor.YELLOW + "Home point set."); } - - if(args.length != 1) + catch(Exception ex) { - return false; + player.sendMessage(Constants.Messages.FAILED_TO_SAVE_CONFIG_FILE); } - - World world = Bukkit.getWorld(args[0]); + } - if(world == null) + if(label.equals("home")) + { + final Location location = config.getHome(player); + + if(location == null) { - player.sendMessage(DefaultConstants.Messages.WORLD_NOT_EXIST); + player.sendMessage(ChatColor.RED + "You've don't have a home point."); return true; } - - player.teleport(world.getSpawnLocation()); + + if(travelTo(player, location)) + { + player.sendMessage(ChatColor.GRAY + "You've been teleported to your home."); + } } - if(label.equals("spawn")) + if(label.equals("setwarp")) { - ItemStack item = player.getInventory().getItemInMainHand(); - - Location locationSpawn = Bukkit.getWorld("world").getSpawnLocation(); - Location locationPlayer = player.getLocation(); - - Double distance = locationPlayer.distance(locationSpawn); - - int fee = (int) Math.floor(distance / (BLOCKS_PER_CHUNK * CHUNKS_PER_EMERALD)); - - if(fee < MIN_FEE) + if(args.length != 1) { - fee = MIN_FEE; + return false; } + + final String name = args[0]; - if(fee > MAX_FEE) + if(!payFee(player, Material.COMPASS, 1)) { - fee = MAX_FEE; + player.sendMessage(ChatColor.RED + "The fee for creating a warp point is 1 compass!"); + return true; } - if(item.getType() == Material.EMERALD && item.getAmount() >= fee) + try { - - item.setAmount(item.getAmount() - fee); - player.teleport(locationSpawn); + 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."); } - else + catch(Exception ex) { - player.sendMessage(ChatColor.RED + "Not enough emeralds to teleport! You need " + fee + " emerald(s) in your main hand."); + player.sendMessage(Constants.Messages.FAILED_TO_SAVE_CONFIG_FILE); } } - if(label.equals("invsee")) + if(label.equals("warp")) { - if(args.length == 0) { + if(args.length != 1) + { return false; } + + final String name = args[0]; + final Location location = config.getWarp(name); - if(Bukkit.getPlayer(args[0]) == null) + if(location == null) { - player.sendMessage(ChatColor.RED + "Player not found!"); + player.sendMessage(ChatColor.RED + "Warp point " + name + " doesn't exist!"); return true; } - - if(!player.isOp()) + + if(travelTo(player, location)) { - player.sendMessage(DefaultConstants.Messages.NOT_ENOUGH_PERMISSION); - return true; + player.sendMessage(ChatColor.GRAY + "You've been teleported to warp point " + name + "."); } + } + } - Player target = Bukkit.getPlayer(args[0]); + return true; + } - player.openInventory(target.getInventory()); + private boolean travelTo(final Player player, final Location location) + { + final int fee = Util.calcTravelFee(player.getLocation(), location); + final String unit = fee == 1 ? "emerald" : "emeralds"; - if(args.length == 1 && args[1].toLowerCase() == "enderchest") - { - player.openInventory(target.getEnderChest()); - } - } + 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; + } + + private 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; } } diff --git a/src/de/typable/minecrafthub/config/Config.java b/src/de/typable/minecrafthub/config/Config.java @@ -0,0 +1,61 @@ +package de.typable.minecrafthub.config; + +import java.io.File; +import java.io.IOException; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.Location; + +import de.typable.minecrafthub.util.Util; + +public class Config +{ + private File file; + private FileConfiguration configuration; + + public Config(final String path) + { + this.file = new File(path); + this.configuration = YamlConfiguration.loadConfiguration(this.file); + } + + public void setHome(final Player player) throws IOException + { + final String path = "home." + player.getDisplayName(); + final Location location = Util.calcBlockCenter(player.getLocation()); + + this.configuration.set(path, location); + this.configuration.save(this.file); + } + + public Location getHome(final Player player) + { + final String path = "home." + player.getDisplayName(); + + return this.configuration.getLocation(path); + } + + public boolean setWarp(final String name, final Location location) throws IOException + { + final String path = "warp." + name; + + if(this.configuration.getLocation(path) != null) + { + return false; + } + + this.configuration.set(path, Util.calcBlockCenter(location)); + this.configuration.save(this.file); + + return true; + } + + public Location getWarp(final String name) + { + final String path = "warp." + name; + + return this.configuration.getLocation(path); + } +} diff --git a/src/de/typable/minecrafthub/constant/Constants.java b/src/de/typable/minecrafthub/constant/Constants.java @@ -0,0 +1,19 @@ +package de.typable.minecrafthub.constant; + +import org.bukkit.ChatColor; + + +public class Constants +{ + public static final long TICK = 20; + public static final int BLOCKS_PER_CHUNK = 16; + public static final int CHUNKS_PER_EMERALD = 25; + public static final int MIN_FEE = 1; + public static final int MAX_FEE = 64; + + 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 FAILED_TO_SAVE_CONFIG_FILE = ChatColor.RED + "Failed to save changes!"; + } +} diff --git a/src/de/typable/minecrafthub/constant/DefaultConstants.java b/src/de/typable/minecrafthub/constant/DefaultConstants.java @@ -1,17 +0,0 @@ -package de.typable.minecrafthub.constant; - -import org.bukkit.ChatColor; - - -public class DefaultConstants -{ - public static final long TICK = 20; - - 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!"; - public static final String WORLD_NOT_EXIST = ChatColor.RED + "This world does not exist!"; - } -} diff --git a/src/de/typable/minecrafthub/event/EventListener.java b/src/de/typable/minecrafthub/event/EventListener.java @@ -47,9 +47,8 @@ public class EventListener implements Listener } } - // farming with right click - EquipmentSlot equip = event.getHand(); + if(equip.equals(EquipmentSlot.HAND)) { diff --git a/src/de/typable/minecrafthub/event/StandbyListener.java b/src/de/typable/minecrafthub/event/StandbyListener.java @@ -1,79 +0,0 @@ -package de.typable.minecrafthub.event; - -import java.util.Collection; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitTask; - -import de.typable.minecrafthub.constant.DefaultConstants; - - -public class StandbyListener implements Listener, Runnable -{ - private Plugin plugin; - private BukkitTask task; - private boolean enabled; - private long delay; - - public StandbyListener(Plugin plugin) - { - this.plugin = plugin; - this.delay = 10 * 60; - } - - @EventHandler - private void onPlayerJoin(PlayerJoinEvent event) - { - if(enabled && task != null) - { - task.cancel(); - task = null; - } - } - - @EventHandler - private void onPlayerQuit(PlayerQuitEvent event) - { - if(enabled) - { - Collection<? extends Player> online = Bukkit.getOnlinePlayers(); - - if(online.size() == 1 && online.contains(event.getPlayer())) - { - task = Bukkit.getScheduler().runTaskLater(plugin, this, delay * DefaultConstants.TICK); - } - } - } - - @Override - public void run() - { - Bukkit.getServer().shutdown(); - } - - public boolean isEnabled() - { - return enabled; - } - - public void setEnabled(boolean enabled) - { - this.enabled = enabled; - } - - public long getDelay() - { - return delay; - } - - public void setDelay(long delay) - { - this.delay = delay; - } -} diff --git a/src/de/typable/minecrafthub/util/Util.java b/src/de/typable/minecrafthub/util/Util.java @@ -11,8 +11,9 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; +import org.bukkit.Location; -import de.typable.minecrafthub.constant.DefaultConstants; +import de.typable.minecrafthub.constant.Constants; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; @@ -110,7 +111,7 @@ public class Util count--; } - }, 0L, DefaultConstants.TICK); + }, 0L, Constants.TICK); } public static Float convertFacingToYaw(BlockFace face) @@ -157,4 +158,32 @@ public class Util return isStem(blockNorth) || isStem(blockEast) || isStem(blockSouth) || isStem(blockWest); } + + public static Location calcBlockCenter(final Location location) + { + Location centered = location.clone(); + centered.setX(Location.locToBlock(location.getX()) + 0.5); + centered.setY(Location.locToBlock(location.getY())); + centered.setZ(Location.locToBlock(location.getZ()) + 0.5); + + return centered; + } + + public static int calcTravelFee(final Location from, final Location to) + { + final Double distance = from.distance(to); + int fee = (int) Math.floor(distance / (Constants.BLOCKS_PER_CHUNK * Constants.CHUNKS_PER_EMERALD)); + + if(fee < Constants.MIN_FEE) + { + fee = Constants.MIN_FEE; + } + + if(fee > Constants.MAX_FEE) + { + fee = Constants.MAX_FEE; + } + + return fee; + } }