Skip to content
This repository has been archived by the owner on Jun 2, 2021. It is now read-only.

Commit

Permalink
Adds /restartwhenempty
Browse files Browse the repository at this point in the history
permission is tbtickets.admin.restart
  • Loading branch information
ShakeforProtein committed Feb 13, 2020
1 parent aa1afba commit d389a06
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.ShakeforProtein</groupId>
<artifactId>TreeboTickets</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<packaging>jar</packaging>

<name>TreeboTickets</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package me.shakeforprotein.treebotickets.Commands;

import me.shakeforprotein.treebotickets.TreeboTickets;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public class RestartWhenEmpty implements CommandExecutor {

private TreeboTickets pl;


public RestartWhenEmpty(TreeboTickets main) {
this.pl = main;

}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Bukkit.getScheduler().runTaskTimer(pl, new Runnable() {
@Override
public void run() {
if(pl.getServer().getOnlinePlayers().isEmpty()){
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "restart");
}
else{
for(Player player : pl.getServer().getOnlinePlayers()){
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder(pl.badge + ChatColor.RED + ChatColor.BOLD + "This server needs to restart for maintenance. Expected downtime is 2 minutes").create());
}
}
}
}, 100L, 100L);
return true;
}



public static boolean isNumeric (String str)
{
return str.matches("\\d+");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package me.shakeforprotein.treebotickets.Commands;

import me.shakeforprotein.treebotickets.TreeboTickets;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;

public class RestorePlayerInventory implements CommandExecutor {

private TreeboTickets pl;

public RestorePlayerInventory(TreeboTickets main) {
this.pl = main;
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {

if (args.length == 2) {

if (Bukkit.getPlayer(args[0]) != null && Bukkit.getPlayer(args[0]).isOnline()) {
Player p = Bukkit.getPlayer(args[0]);
File deathFile = new File(pl.getDataFolder() + File.separator + "deaths", File.separator + p.getUniqueId().toString() + "_" + args[1] + ".yml");
File deathFileUsed = new File(pl.getDataFolder() + File.separator + "deaths", File.separator + "USED_" + p.getUniqueId().toString() + "_" + args[1] + ".yml");

if (deathFile.exists()) {
sender.sendMessage(pl.badge + "Death file found");
FileConfiguration deathYaml = YamlConfiguration.loadConfiguration(deathFile);
if (deathYaml.getString("used").equalsIgnoreCase("false")) {
if (deathYaml.getString("uuid").equalsIgnoreCase(p.getUniqueId().toString())) {
if (p.getWorld().getName().equalsIgnoreCase(deathYaml.getString("world"))) {
int i = 0;
for (String key : deathYaml.getConfigurationSection("inventory").getKeys(false)) {
p.getInventory().addItem(deathYaml.getItemStack("inventory.slot_" + i));
i++;
}
p.setTotalExperience(deathYaml.getInt("experience"));
deathYaml.set("used", true);
try {
deathYaml.save(deathFile);
deathFile.renameTo(deathFileUsed);
} catch (IOException err) {
pl.makeLog(err);
sender.sendMessage(pl.err + "Failed to update 'used' status on docket " + deathFile.toString());
}
} else {
sender.sendMessage(pl.err + "Player world mismatch. This inventory is for World:" + deathYaml.getString("world"));
}
} else {
sender.sendMessage(pl.err + "Player UUID mismatch. This inventory does not belong to Player: " + args[0]);
}
} else {
sender.sendMessage(pl.err + "This inventory has already been recovered");
}
} else {
sender.sendMessage(pl.err + "No matching death file on record.");
}
} else {
sender.sendMessage(pl.err + "Player: " + args[0] + " not found.");
}
} else if (args.length < 2) {
sender.sendMessage(pl.err + "Insufficient arguments. This command requires a <playername> and <secretkey> argument.");
} else {
sender.sendMessage(pl.err + "Too many arguments. This command requires only a <playername> and <secretkey> argument.");
}
} else {
sender.sendMessage(ChatColor.RED + "You do not have access to this command. You require permission node " + ChatColor.GOLD + "tbtickets.admin.restoreinventory");
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.shakeforprotein.treebotickets.Commands;

import me.shakeforprotein.treebotickets.TreeboTickets;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public class ToggleDeathDocket implements CommandExecutor {

private TreeboTickets pl;

public ToggleDeathDocket(TreeboTickets main) {
this.pl = main;
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player p = (Player) sender;
if (pl.getConfig().get("deathDocket.toggle." + p.getUniqueId()) == null || pl.getConfig().get("deathDocket.toggle." + p.getUniqueId()).equals("false")) {
pl.getConfig().set("deathDocket.toggle." + p.getUniqueId(), "true");
} else {
pl.getConfig().set("deathDocket.toggle." + p.getUniqueId(), "false");
}
} else {
sender.sendMessage(pl.err + "Only players can run this command");
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public void playerUseItem(PlayerInteractEvent e) {
}
}
}
if (e.getPlayer().hasPermission("tbtickets.admin.restoreinventory") && e.getItem() != null && e.getItem().hasItemMeta() && e.getItem().getItemMeta().hasDisplayName() && e.getItem().getItemMeta().getDisplayName().contains("Death Docket -")) {
if (e.getItem().getItemMeta().getLore().get(4) != null) {
String cmd = "tellraw " + e.getPlayer().getName() + " [\"\",{\"text\":\"Click \"},{\"text\":\"[HERE]\",\"color\":\"green\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/restoreplayerinventory " + e.getItem().getItemMeta().getLore().get(0).split(" - ")[1] + " " + e.getItem().getItemMeta().getLore().get(4).split(" - ")[1] + "\"}},{\"text\":\" to restore player inventory.\"}]";
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);

}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package me.shakeforprotein.treebotickets.Listeners;

import me.shakeforprotein.treebotickets.TreeboTickets;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


public class RespawnListener implements Listener {

private TreeboTickets pl;

private HashMap<String, ItemStack> docketMap = new HashMap<>();

public RespawnListener(TreeboTickets main) {
this.pl = main;
}

@EventHandler
public boolean onPlayerDeath(PlayerDeathEvent e) {
if (!e.getDrops().isEmpty()) {
String key1 = UUID.randomUUID().toString();
String key2 = UUID.randomUUID().toString();
String key3 = key1 + "-" + key2;

File deathFile = new File(pl.getDataFolder() + File.separator + "deaths", File.separator + e.getEntity().getUniqueId().toString() + "_" + key3 + ".yml");
FileConfiguration deathYaml = YamlConfiguration.loadConfiguration(deathFile);


Player p = e.getEntity();
Location loc = e.getEntity().getLocation();
int droppedXP = p.getTotalExperience();
String deathMessage = e.getDeathMessage();

List<ItemStack> itemList = e.getDrops();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());

ItemStack docket = new ItemStack(Material.PAPER, 1);
ItemMeta docketMeta = docket.getItemMeta();
docketMeta.setDisplayName(pl.badge + "Death Docket - " + date);
deathYaml.set("date", date);

List<String> docketLore = new ArrayList<String>();
docketLore.add("Player - " + p.getName());
deathYaml.set("player", p.getName());
deathYaml.set("uuid", p.getUniqueId().toString());
docketLore.add("Location - " + loc.getWorld().getName() + " " + loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ());
deathYaml.set("world", loc.getWorld().getName());
deathYaml.set("xyz", loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ());
docketLore.add("XP - " + droppedXP);
deathYaml.set("experience", droppedXP);
docketLore.add("Death Message - " + deathMessage);
deathYaml.set("deathMessage", deathMessage);
docketLore.add("SecretKey - " + key3);
deathYaml.set("secretKey", key3);
int i = 0;
for (ItemStack item : itemList) {
deathYaml.set("inventory.slot_" + i, item);
i++;
docketLore.add(item.getAmount() + " X " + item.getType().name());
if (item.hasItemMeta() && item.getItemMeta().hasEnchants()) {
for (Enchantment enchantment : item.getItemMeta().getEnchants().keySet()) {
docketLore.add(" - " + enchantment.getKey() + " LvL " + item.getItemMeta().getEnchants().get(enchantment));
}
} else if (item.hasItemMeta() && item.getItemMeta() instanceof PotionMeta) {
docketLore.add(" - " + ((PotionMeta) item.getItemMeta()).getBasePotionData().getType() + " Ext:" + ((PotionMeta) item.getItemMeta()).getBasePotionData().isExtended() + " Upg:" + ((PotionMeta) item.getItemMeta()).getBasePotionData().isUpgraded());
} else if (item.hasItemMeta() && item.getItemMeta().hasAttributeModifiers()) {
for (Attribute attrib : item.getItemMeta().getAttributeModifiers().keySet()) {
docketLore.add(" - " + attrib.name() + " LvL " + item.getItemMeta().getAttributeModifiers().get(attrib));
}
}
}
docketMeta.setLore(docketLore);
docket.setItemMeta(docketMeta);
docketMap.put(p.getUniqueId().toString(), docket);
deathYaml.set("used", "false");
try {
deathYaml.save(deathFile);
} catch (IOException err) {
pl.makeLog(err);
}
}
return true;
}

@EventHandler
public void onRespawnEvent(PlayerRespawnEvent e) {
Player p = e.getPlayer();
if (!pl.getConfig().getBoolean("deathDocket.disable")) {
if (docketMap.containsKey(p.getUniqueId().toString())) {
if (pl.getConfig().get("deathDocket.toggle." + p.getUniqueId()) == null || pl.getConfig().get("deathDocket.toggle." + p.getUniqueId().toString()).equals("false")) {

p.getInventory().setItemInOffHand(docketMap.get(p.getUniqueId().toString()));
p.sendMessage(pl.badge + "You been issued a death docket. You can disable this feature at any time with /toggledeathdocket, but in doing so, staff will be unable to assist with item recovery");
} else {
p.sendMessage(pl.badge + "You have Death Dockets disabled. Re-enable with /toggledeathdocket");
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public void onEnable() {
this.getCommand("seen").setExecutor(onHere);
this.getCommand("discord").setExecutor(discord);
this.getCommand("getstat").setExecutor(getStat);
this.getCommand("toggledeathdocket").setExecutor(new ToggleDeathDocket(this));
this.getCommand("restartwhenempty").setExecutor(new RestartWhenEmpty(this));


if(getConfig().get("bstatsIntegration") != null) {
Expand Down Expand Up @@ -205,6 +207,7 @@ public boolean execute(CommandSender commandSender, String s, String[] strings)
getServer().getPluginManager().registerEvents(new OnPlayerDisconnect(this), this);
getServer().getPluginManager().registerEvents(new HubMenuInventoryListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerUseItem(this), this);
getServer().getPluginManager().registerEvents(new RespawnListener(this), this);


getConfig().options().copyDefaults(true);
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ api-version: 1.15
softdepend: [PlaceholderAPI]

commands:
toggledeathdocket:
description: Toggles death docket
usage: /<command>
discord:
description: Overides discordsrv commands.
usage: /<command> <args>
Expand Down Expand Up @@ -58,13 +61,23 @@ commands:
tbtst:
description: Not for players to use
usage: Do not play with this command.
restoreplayerinventory:
description: Not for players to use
usage: Do not play with this command.
restartwhenempty:
permission: tbtickets.admin.restart
description: Sets a timer to restart the server when it detects the server is empty.

permissions:
tbtickets.staffmanager:
description:
default: false
tbtickets.admin.ontime:
description:
default: op
tbtickets.admin.restoreinventory:
description:
default: false
tbtickets.admin.restart:
description:
default: false
Expand Down

0 comments on commit d389a06

Please sign in to comment.