commit fa24c494d66444d73cf574b3e76d92c3f2b727f8
parent 4ea1a501485c57f509ffc631d5b2370380c578f3
Author: typable <typable.dev@gmail.com>
Date: Sun, 10 Jan 2021 16:34:54 +0100
Added shutdown and restart
Diffstat:
5 files changed, 172 insertions(+), 7 deletions(-)
diff --git a/plugin.yml b/plugin.yml
@@ -4,6 +4,7 @@ version: 0.0.1
author: typable
api-version: 1.16
commands:
+ shutdown:
head:
skull:
usage: /skull <player>
diff --git a/src/de/typable/minecrafthub/Main.java b/src/de/typable/minecrafthub/Main.java
@@ -1,5 +1,10 @@
package de.typable.minecrafthub;
+import java.io.File;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
@@ -9,15 +14,18 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
+import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import de.typable.minecrafthub.constant.DefaultConstants;
import de.typable.minecrafthub.event.AutoWorkbenchListener;
import de.typable.minecrafthub.event.ChairListener;
+import de.typable.minecrafthub.event.ChestLockListener;
import de.typable.minecrafthub.event.DoubleDoorListener;
import de.typable.minecrafthub.event.EventListener;
import de.typable.minecrafthub.event.StandbyListener;
+import de.typable.minecrafthub.util.Util;
public class Main extends JavaPlugin
@@ -27,11 +35,16 @@ public class Main extends JavaPlugin
private DoubleDoorListener doubleDoorListener;
private ChairListener chairListener;
private AutoWorkbenchListener autoWorkbenchListener;
+ private ChestLockListener chestLockListener;
private EventListener eventListener;
+ private Plugin plugin;
+
@Override
public void onEnable()
{
+ plugin = this;
+
pluginManager = Bukkit.getPluginManager();
standbyListener = new StandbyListener(this);
@@ -45,9 +58,47 @@ public class Main extends JavaPlugin
autoWorkbenchListener = new AutoWorkbenchListener();
pluginManager.registerEvents(autoWorkbenchListener, this);
+
+ chestLockListener = new ChestLockListener();
+ pluginManager.registerEvents(chestLockListener, this);
eventListener = new EventListener();
pluginManager.registerEvents(eventListener, this);
+
+ 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(IOException ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ });
}
@SuppressWarnings("deprecation")
@@ -58,6 +109,42 @@ public class Main extends JavaPlugin
{
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"))
{
ItemStack handitem = player.getInventory().getItemInMainHand();
diff --git a/src/de/typable/minecrafthub/event/AutoWorkbenchListener.java b/src/de/typable/minecrafthub/event/AutoWorkbenchListener.java
@@ -80,7 +80,7 @@ public class AutoWorkbenchListener implements Listener
{
Recipe recipe = iterator.next();
- if(item.isSimilar(recipe.getResult()))
+ if(Util.compare(item, recipe.getResult()))
{
List<ItemStack> ingredients = null;
@@ -159,7 +159,7 @@ public class AutoWorkbenchListener implements Listener
continue;
}
- if(ingredient.isSimilar(item))
+ if(Util.compare(item, ingredient))
{
if(item.getAmount() > amount)
{
@@ -191,7 +191,7 @@ public class AutoWorkbenchListener implements Listener
{
for(ItemStack ingredient : ingredients)
{
- if(ingredient.isSimilar(item))
+ if(Util.compare(item, ingredient))
{
ingredient.setAmount(ingredient.getAmount() + item.getAmount());
@@ -209,7 +209,7 @@ public class AutoWorkbenchListener implements Listener
{
for(ItemStack ingredient : ingredients)
{
- if(ingredient != null && !inventory.containsAtLeast(ingredient, ingredient.getAmount()))
+ if(ingredient != null && !Util.containsAtLeast(inventory, ingredient, ingredient.getAmount()))
{
return false;
}
diff --git a/src/de/typable/minecrafthub/event/ChairListener.java b/src/de/typable/minecrafthub/event/ChairListener.java
@@ -67,6 +67,7 @@ public class ChairListener implements Listener
arrow.setInvulnerable(true);
arrow.setPickupStatus(PickupStatus.DISALLOWED);
arrow.setPassenger((Entity) event.getPlayer());
+ arrow.setTicksLived(0);
event.setCancelled(true);
diff --git a/src/de/typable/minecrafthub/util/Util.java b/src/de/typable/minecrafthub/util/Util.java
@@ -1,8 +1,17 @@
package de.typable.minecrafthub.util;
+import org.bukkit.Bukkit;
import org.bukkit.Material;
+import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
+import org.bukkit.plugin.Plugin;
+
+import de.typable.minecrafthub.constant.DefaultConstants;
+import net.md_5.bungee.api.ChatColor;
+import net.md_5.bungee.api.ChatMessageType;
+import net.md_5.bungee.api.chat.TextComponent;
+
public class Util
{
@@ -10,7 +19,27 @@ public class Util
{
return item == null || item.getType() == Material.AIR;
}
-
+
+ public static boolean isType(ItemStack item, Material type)
+ {
+ return !isEmpty(item) && item.getType() == type;
+ }
+
+ public static boolean compare(ItemStack item, ItemStack item2)
+ {
+ if(item == null || item2 == null)
+ {
+ return false;
+ }
+
+ if(item.getType() != item2.getType())
+ {
+ return false;
+ }
+
+ return true;
+ }
+
public static boolean isInventoryFull(Inventory inventory, ItemStack result)
{
for(ItemStack item : inventory.getContents())
@@ -19,7 +48,7 @@ public class Util
{
return false;
}
-
+
if(result.isSimilar(item))
{
if(result.getAmount() + item.getAmount() <= item.getMaxStackSize())
@@ -28,7 +57,54 @@ public class Util
}
}
}
-
+
return true;
}
+
+ public static boolean containsAtLeast(Inventory inventory, ItemStack item, int amount)
+ {
+ for(int i = 0; i < inventory.getSize(); i++)
+ {
+ ItemStack current = inventory.getItem(i);
+
+ if(Util.compare(item, current))
+ {
+ amount -= current.getAmount();
+
+ if(amount <= 0)
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ public static void sendActionMessage(Player player, String message)
+ {
+ player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));
+ }
+
+ public static void sendCountdown(Plugin plugin, final String message, final int time, Runnable runnable)
+ {
+ Bukkit.getScheduler().runTaskTimer(plugin, new Runnable()
+ {
+ int count = time;
+
+ @Override
+ public void run()
+ {
+ if(count == 0)
+ {
+ runnable.run();
+ return;
+ }
+
+ Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + message + " " + count + " sec");
+
+ count--;
+ }
+ }, 0L, DefaultConstants.TICK);
+ }
}