Finished KeepInventory Per World Settings
This commit is contained in:
1 parent
c12c93be85
commit
9ca8039268
6 files changed
+254
-12
No files matched your search
@@ -9,8 +9,10 @@ import com.rsscripting.serveradmin.security.SecurityManager;
|
||||
import com.rsscripting.serveradmin.listeners.InventoryListener;
|
||||
import com.rsscripting.serveradmin.listeners.CreeperEntityDamageListener;
|
||||
import com.rsscripting.serveradmin.update.GitHubUpdateChecker;
|
||||
import com.rsscripting.serveradmin.listeners.WorldLoadListener;
|
||||
|
||||
import com.rsscripting.serveradmin.settings.SettingKey;
|
||||
import com.rsscripting.serveradmin.worlds.WorldsDAO;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
@@ -24,6 +26,7 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
private DatabaseManager databaseManager;
|
||||
private SettingsDAO settingsDAO;
|
||||
private SecurityManager securityManager;
|
||||
private WorldsDAO worldsDAO;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@@ -44,6 +47,8 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
|
||||
keepInventoryDAO = new KeepInventoryDAO(databaseManager);
|
||||
|
||||
worldsDAO = new WorldsDAO(databaseManager);
|
||||
|
||||
securityManager = new SecurityManager(this);
|
||||
|
||||
boolean keepInventoryDefault =
|
||||
@@ -53,6 +58,10 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
|
||||
for (World world : Bukkit.getWorlds()) {
|
||||
|
||||
worldsDAO.ensureWorldExists(
|
||||
world.getName()
|
||||
);
|
||||
|
||||
keepInventoryDAO.ensureWorldExists(
|
||||
world.getName(),
|
||||
keepInventoryDefault
|
||||
@@ -89,6 +98,11 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
this
|
||||
);
|
||||
|
||||
getServer().getPluginManager().registerEvents(
|
||||
new WorldLoadListener(),
|
||||
this
|
||||
);
|
||||
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
@@ -120,17 +134,24 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
getLogger().info("RS-ServerAdmin disabled.");
|
||||
}
|
||||
|
||||
public KeepInventoryDAO getKeepInventoryDAO() {return keepInventoryDAO;}
|
||||
public KeepInventoryDAO getKeepInventoryDAO() {
|
||||
return keepInventoryDAO;}
|
||||
public static RSServerAdmin getInstance() {
|
||||
|
||||
return instance;
|
||||
}
|
||||
public DatabaseManager getDatabaseManager() {
|
||||
|
||||
return databaseManager;
|
||||
}
|
||||
public SettingsDAO getSettingsDAO() {
|
||||
|
||||
return settingsDAO;
|
||||
}
|
||||
public WorldsDAO getWorldsDAO() {
|
||||
return worldsDAO;}
|
||||
public SecurityManager getSecurityManager() {
|
||||
|
||||
return securityManager;
|
||||
}
|
||||
|
||||
|
||||
@@ -156,7 +156,7 @@ public class CreeperMenu {
|
||||
}
|
||||
|
||||
inventory.setItem(
|
||||
22,
|
||||
4,
|
||||
back
|
||||
);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.rsscripting.serveradmin.gui;
|
||||
|
||||
import com.rsscripting.serveradmin.RSServerAdmin;
|
||||
import com.rsscripting.serveradmin.keepinventory.KeepInventoryDAO;
|
||||
import com.rsscripting.serveradmin.gui.RSMenuHolder;
|
||||
import com.rsscripting.serveradmin.worlds.WorldsDAO;
|
||||
import com.rsscripting.serveradmin.settings.SettingKey;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
@@ -83,7 +83,7 @@ public class KeepInventoryMenu {
|
||||
}
|
||||
|
||||
inventory.setItem(
|
||||
0,
|
||||
3,
|
||||
item
|
||||
);
|
||||
|
||||
@@ -121,8 +121,8 @@ public class KeepInventoryMenu {
|
||||
"§7Controls newly detected worlds",
|
||||
"",
|
||||
enabled
|
||||
? "§aCurrent: New worlds keep inventory"
|
||||
: "§cCurrent: New worlds drop inventory"
|
||||
? "§aCurrent: New worlds, Players keep inventory on death"
|
||||
: "§cCurrent: New worlds, Players drop inventory on death"
|
||||
)
|
||||
);
|
||||
|
||||
@@ -141,7 +141,7 @@ public class KeepInventoryMenu {
|
||||
}
|
||||
|
||||
inventory.setItem(
|
||||
4,
|
||||
5,
|
||||
item
|
||||
);
|
||||
|
||||
@@ -157,7 +157,9 @@ public class KeepInventoryMenu {
|
||||
.getKeepInventoryDAO();
|
||||
|
||||
List<String> worlds =
|
||||
dao.getAllWorlds();
|
||||
RSServerAdmin.getInstance()
|
||||
.getWorldsDAO()
|
||||
.getAllWorlds();
|
||||
|
||||
int start =
|
||||
(page - 1) * WORLDS_PER_PAGE;
|
||||
|
||||
@@ -33,6 +33,28 @@ public class MainMenu {
|
||||
creeper.setItemMeta(creeperMeta);
|
||||
}
|
||||
|
||||
ItemStack scanWorlds =
|
||||
new ItemStack(
|
||||
Material.COMPASS
|
||||
);
|
||||
|
||||
ItemMeta scanMeta =
|
||||
scanWorlds.getItemMeta();
|
||||
|
||||
if (scanMeta != null) {
|
||||
scanMeta.setDisplayName("§aRescan Worlds");
|
||||
|
||||
scanMeta.setLore(java.util.List.of(
|
||||
"§7Rescan for new worlds",
|
||||
"",
|
||||
"§eClick to rescan."
|
||||
));
|
||||
|
||||
scanWorlds.setItemMeta(scanMeta);
|
||||
}
|
||||
|
||||
inventory.setItem(4, scanWorlds);
|
||||
|
||||
inventory.setItem(11, creeper);
|
||||
|
||||
ItemStack keepInventory =
|
||||
|
||||
@@ -5,12 +5,15 @@ import com.rsscripting.serveradmin.gui.RSMenuHolder;
|
||||
import com.rsscripting.serveradmin.gui.CreeperMenu;
|
||||
import com.rsscripting.serveradmin.RSServerAdmin;
|
||||
import com.rsscripting.serveradmin.settings.SettingKey;
|
||||
import com.rsscripting.serveradmin.settings.SettingKey;
|
||||
import com.rsscripting.serveradmin.keepinventory.KeepInventoryDAO;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameRule;
|
||||
import org.bukkit.World;
|
||||
|
||||
public class InventoryListener implements Listener {
|
||||
|
||||
@@ -41,10 +44,67 @@ public class InventoryListener implements Listener {
|
||||
|
||||
switch (event.getSlot()) {
|
||||
|
||||
/*
|
||||
| Rescan for new worlds
|
||||
*/
|
||||
|
||||
case 4 -> {
|
||||
|
||||
try {
|
||||
|
||||
boolean keepInventoryDefault =
|
||||
RSServerAdmin.getInstance()
|
||||
.getSettingsDAO()
|
||||
.getDefault(
|
||||
SettingKey.KEEP_INVENTORY_NEW_WORLD
|
||||
);
|
||||
|
||||
for (org.bukkit.World world
|
||||
: org.bukkit.Bukkit.getWorlds()) {
|
||||
|
||||
RSServerAdmin.getInstance()
|
||||
.getWorldsDAO()
|
||||
.ensureWorldExists(
|
||||
world.getName()
|
||||
);
|
||||
|
||||
RSServerAdmin.getInstance()
|
||||
.getKeepInventoryDAO()
|
||||
.ensureWorldExists(
|
||||
world.getName(),
|
||||
keepInventoryDefault
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
player.sendMessage(
|
||||
"§aWorld scan complete."
|
||||
);
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
ex.printStackTrace();
|
||||
|
||||
player.sendMessage(
|
||||
"§cWorld scan failed."
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
| Creeper menu
|
||||
*/
|
||||
|
||||
case 11 -> player.openInventory(
|
||||
CreeperMenu.create()
|
||||
);
|
||||
|
||||
/*
|
||||
| Keep Inventory Menu
|
||||
*/
|
||||
|
||||
case 15 -> {
|
||||
|
||||
try {
|
||||
@@ -67,6 +127,10 @@ public class InventoryListener implements Listener {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
| Creeper menu
|
||||
*/
|
||||
|
||||
case "creeper" -> {
|
||||
|
||||
if (!(event.getWhoClicked() instanceof Player player)) {
|
||||
@@ -119,7 +183,7 @@ public class InventoryListener implements Listener {
|
||||
|
||||
}
|
||||
|
||||
case 22 -> player.openInventory(
|
||||
case 4 -> player.openInventory(
|
||||
com.rsscripting.serveradmin.gui.MainMenu.create()
|
||||
);
|
||||
|
||||
@@ -127,6 +191,10 @@ public class InventoryListener implements Listener {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
| Keep Inventory
|
||||
*/
|
||||
|
||||
case "KEEP_INVENTORY" -> {
|
||||
|
||||
if (!(event.getWhoClicked() instanceof Player player)) {
|
||||
@@ -135,11 +203,11 @@ public class InventoryListener implements Listener {
|
||||
|
||||
switch (event.getSlot()) {
|
||||
|
||||
case 0 -> player.openInventory(
|
||||
case 3 -> player.openInventory(
|
||||
com.rsscripting.serveradmin.gui.MainMenu.create()
|
||||
);
|
||||
|
||||
case 4 -> {
|
||||
case 5 -> {
|
||||
|
||||
try {
|
||||
|
||||
@@ -163,6 +231,77 @@ public class InventoryListener implements Listener {
|
||||
|
||||
}
|
||||
|
||||
default -> {
|
||||
|
||||
if (event.getSlot() < 9) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
KeepInventoryDAO dao =
|
||||
RSServerAdmin.getInstance()
|
||||
.getKeepInventoryDAO();
|
||||
|
||||
java.util.List<String> worlds =
|
||||
RSServerAdmin.getInstance()
|
||||
.getWorldsDAO()
|
||||
.getAllWorlds();
|
||||
|
||||
int worldIndex =
|
||||
((holder.getPage() - 1) * 45)
|
||||
+ (event.getSlot() - 9);
|
||||
|
||||
if (worldIndex < 0
|
||||
|| worldIndex >= worlds.size()) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
String worldName =
|
||||
worlds.get(
|
||||
worldIndex
|
||||
);
|
||||
|
||||
boolean enabled =
|
||||
dao.getWorldSetting(
|
||||
worldName
|
||||
);
|
||||
|
||||
dao.setWorldSetting(
|
||||
worldName,
|
||||
!enabled
|
||||
);
|
||||
|
||||
World world =
|
||||
Bukkit.getWorld(
|
||||
worldName
|
||||
);
|
||||
|
||||
if (world != null) {
|
||||
|
||||
world.setGameRule(
|
||||
GameRule.KEEP_INVENTORY,
|
||||
!enabled
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
player.openInventory(
|
||||
KeepInventoryMenu.create(
|
||||
holder.getPage()
|
||||
)
|
||||
);
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
ex.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.rsscripting.serveradmin.listeners;
|
||||
|
||||
import com.rsscripting.serveradmin.RSServerAdmin;
|
||||
import com.rsscripting.serveradmin.settings.SettingKey;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.world.WorldLoadEvent;
|
||||
import org.bukkit.GameRule;
|
||||
|
||||
public class WorldLoadListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onWorldLoad(
|
||||
WorldLoadEvent event
|
||||
) {
|
||||
|
||||
try {
|
||||
|
||||
RSServerAdmin plugin =
|
||||
RSServerAdmin.getInstance();
|
||||
|
||||
plugin.getWorldsDAO()
|
||||
.ensureWorldExists(
|
||||
event.getWorld().getName()
|
||||
);
|
||||
|
||||
boolean keepInventoryDefault =
|
||||
plugin.getSettingsDAO()
|
||||
.getDefault(
|
||||
SettingKey.KEEP_INVENTORY_NEW_WORLD
|
||||
);
|
||||
|
||||
plugin.getKeepInventoryDAO()
|
||||
.ensureWorldExists(
|
||||
event.getWorld().getName(),
|
||||
keepInventoryDefault
|
||||
);
|
||||
|
||||
boolean keepInventoryEnabled =
|
||||
plugin.getKeepInventoryDAO()
|
||||
.getWorldSetting(
|
||||
event.getWorld().getName()
|
||||
);
|
||||
|
||||
event.getWorld().setGameRule(
|
||||
GameRule.KEEP_INVENTORY,
|
||||
keepInventoryEnabled
|
||||
);
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
ex.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user