1.3.0
Added: - Per World: All mobs peaceful
This commit is contained in:
1 parent
999f403f4f
commit
4bcad2b211
13 files changed
+978
-117
No files matched your search
@@ -8,7 +8,7 @@
|
||||
<groupId>com.rsscripting</groupId>
|
||||
<artifactId>RS-ServerAdmin</artifactId>
|
||||
<name>RS-ServerAdmin</name>
|
||||
<version>1.2.1</version>
|
||||
<version>1.3.0</version>
|
||||
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
|
||||
@@ -24,6 +24,7 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
private WorldsDAO worldsDAO;
|
||||
private CreeperSettingsDAO creeperSettingsDAO;
|
||||
private MobEquipmentDropSettingsDAO equipmentDropSettingsDAO;
|
||||
private PeacefulMobsDAO peacefulMobsDAO;
|
||||
private GitHubUpdateChecker gitHubUpdateChecker;
|
||||
|
||||
@Override
|
||||
@@ -63,6 +64,11 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
databaseManager
|
||||
);
|
||||
|
||||
peacefulMobsDAO =
|
||||
new PeacefulMobsDAO(
|
||||
databaseManager
|
||||
);
|
||||
|
||||
securityManager = new SecurityManager(this);
|
||||
|
||||
boolean keepInventoryDefault =
|
||||
@@ -70,6 +76,11 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
SettingKey.KEEP_INVENTORY_NEW_WORLD
|
||||
);
|
||||
|
||||
boolean peacefulMobsDefault =
|
||||
settingsDAO.getDefault(
|
||||
SettingKey.PEACEFUL_MOBS_NEW_WORLD
|
||||
);
|
||||
|
||||
for (World world : Bukkit.getWorlds()) {
|
||||
|
||||
worldsDAO.ensureWorldExists(
|
||||
@@ -101,6 +112,11 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
world.getName()
|
||||
);
|
||||
|
||||
peacefulMobsDAO.ensureWorldExists(
|
||||
world.getName(),
|
||||
peacefulMobsDefault
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if (getCommand("rsadmin") != null) {
|
||||
@@ -157,6 +173,11 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
this
|
||||
);
|
||||
|
||||
getServer().getPluginManager().registerEvents(
|
||||
new PeacefulMobsListener(),
|
||||
this
|
||||
);
|
||||
|
||||
getServer().getPluginManager().registerEvents(
|
||||
new UpdateNotificationListener(),
|
||||
this
|
||||
@@ -196,26 +217,33 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
}
|
||||
|
||||
public KeepInventoryDAO getKeepInventoryDAO() {
|
||||
return keepInventoryDAO;}
|
||||
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;
|
||||
}
|
||||
|
||||
public CreeperSettingsDAO getCreeperSettingsDAO() {
|
||||
return creeperSettingsDAO;
|
||||
}
|
||||
@@ -226,6 +254,13 @@ public class RSServerAdmin extends JavaPlugin {
|
||||
return equipmentDropSettingsDAO;
|
||||
|
||||
}
|
||||
|
||||
public PeacefulMobsDAO getPeacefulMobsDAO() {
|
||||
|
||||
return peacefulMobsDAO;
|
||||
|
||||
}
|
||||
|
||||
public GitHubUpdateChecker getGitHubUpdateChecker() {
|
||||
|
||||
return gitHubUpdateChecker;
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.rsscripting.serveradmin.dao;
|
||||
|
||||
import com.rsscripting.serveradmin.database.DatabaseManager;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class PeacefulMobsDAO {
|
||||
|
||||
private final DatabaseManager databaseManager;
|
||||
|
||||
public PeacefulMobsDAO(DatabaseManager databaseManager) {
|
||||
this.databaseManager = databaseManager;
|
||||
}
|
||||
|
||||
public boolean getWorldSetting(
|
||||
String worldName
|
||||
) throws SQLException {
|
||||
|
||||
Connection connection =
|
||||
databaseManager.getConnection();
|
||||
|
||||
try (PreparedStatement statement =
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
SELECT enabled
|
||||
FROM peaceful_mobs
|
||||
WHERE world_name = ?
|
||||
"""
|
||||
)) {
|
||||
|
||||
statement.setString(
|
||||
1,
|
||||
worldName
|
||||
);
|
||||
|
||||
try (ResultSet resultSet =
|
||||
statement.executeQuery()) {
|
||||
|
||||
if (resultSet.next()) {
|
||||
|
||||
return resultSet.getInt(
|
||||
"enabled"
|
||||
) == 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setWorldSetting(
|
||||
String worldName,
|
||||
boolean enabled
|
||||
) throws SQLException {
|
||||
|
||||
Connection connection =
|
||||
databaseManager.getConnection();
|
||||
|
||||
try (PreparedStatement statement =
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
INSERT OR REPLACE INTO peaceful_mobs
|
||||
(world_name, enabled)
|
||||
VALUES (?, ?)
|
||||
"""
|
||||
)) {
|
||||
|
||||
statement.setString(
|
||||
1,
|
||||
worldName
|
||||
);
|
||||
|
||||
statement.setInt(
|
||||
2,
|
||||
enabled ? 1 : 0
|
||||
);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void ensureWorldExists(
|
||||
String worldName,
|
||||
boolean defaultValue
|
||||
) throws SQLException {
|
||||
|
||||
Connection connection =
|
||||
databaseManager.getConnection();
|
||||
|
||||
try (PreparedStatement statement =
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
INSERT OR IGNORE INTO peaceful_mobs
|
||||
(world_name, enabled)
|
||||
VALUES (?, ?)
|
||||
"""
|
||||
)) {
|
||||
|
||||
statement.setString(
|
||||
1,
|
||||
worldName
|
||||
);
|
||||
|
||||
statement.setInt(
|
||||
2,
|
||||
defaultValue ? 1 : 0
|
||||
);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -52,7 +52,8 @@ public class SettingsDAO {
|
||||
|
||||
case CREEPER_BLOCK_DAMAGE,
|
||||
CREEPER_ENTITY_DAMAGE,
|
||||
KEEP_INVENTORY_NEW_WORLD -> false;
|
||||
KEEP_INVENTORY_NEW_WORLD,
|
||||
PEACEFUL_MOBS_NEW_WORLD -> false;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.rsscripting.serveradmin.dao;
|
||||
|
||||
import com.rsscripting.serveradmin.database.DatabaseManager;
|
||||
import com.rsscripting.serveradmin.RSServerAdmin;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -14,12 +13,8 @@ public class WorldsDAO {
|
||||
|
||||
private final DatabaseManager databaseManager;
|
||||
|
||||
public WorldsDAO(
|
||||
DatabaseManager databaseManager
|
||||
) {
|
||||
|
||||
public WorldsDAO(DatabaseManager databaseManager) {
|
||||
this.databaseManager = databaseManager;
|
||||
|
||||
}
|
||||
|
||||
public void ensureWorldExists(
|
||||
@@ -33,13 +28,8 @@ public class WorldsDAO {
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
INSERT OR IGNORE INTO worlds
|
||||
(
|
||||
world_name
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
?
|
||||
)
|
||||
(world_name)
|
||||
VALUES (?)
|
||||
"""
|
||||
)) {
|
||||
|
||||
@@ -49,9 +39,7 @@ public class WorldsDAO {
|
||||
);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<String> getAllWorlds()
|
||||
@@ -71,24 +59,20 @@ public class WorldsDAO {
|
||||
ORDER BY world_name
|
||||
"""
|
||||
);
|
||||
|
||||
ResultSet rs =
|
||||
ResultSet resultSet =
|
||||
statement.executeQuery()) {
|
||||
|
||||
while (rs.next()) {
|
||||
while (resultSet.next()) {
|
||||
|
||||
worlds.add(
|
||||
rs.getString(
|
||||
resultSet.getString(
|
||||
"world_name"
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return worlds;
|
||||
|
||||
}
|
||||
|
||||
public void deleteWorldEverywhere(
|
||||
@@ -100,10 +84,7 @@ public class WorldsDAO {
|
||||
|
||||
try (PreparedStatement statement =
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
DELETE FROM worlds
|
||||
WHERE world_name = ?
|
||||
"""
|
||||
"DELETE FROM worlds WHERE world_name = ?"
|
||||
)) {
|
||||
|
||||
statement.setString(
|
||||
@@ -112,15 +93,11 @@ public class WorldsDAO {
|
||||
);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
try (PreparedStatement statement =
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
DELETE FROM keep_inventory
|
||||
WHERE world_name = ?
|
||||
"""
|
||||
"DELETE FROM keep_inventory WHERE world_name = ?"
|
||||
)) {
|
||||
|
||||
statement.setString(
|
||||
@@ -129,15 +106,11 @@ public class WorldsDAO {
|
||||
);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
try (PreparedStatement statement =
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
DELETE FROM creeper_settings
|
||||
WHERE world_name = ?
|
||||
"""
|
||||
"DELETE FROM creeper_settings WHERE world_name = ?"
|
||||
)) {
|
||||
|
||||
statement.setString(
|
||||
@@ -146,15 +119,11 @@ public class WorldsDAO {
|
||||
);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
try (PreparedStatement statement =
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
DELETE FROM equipment_drop_settings
|
||||
WHERE world_name = ?
|
||||
"""
|
||||
"DELETE FROM equipment_drop_settings WHERE world_name = ?"
|
||||
)) {
|
||||
|
||||
statement.setString(
|
||||
@@ -163,9 +132,19 @@ public class WorldsDAO {
|
||||
);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
try (PreparedStatement statement =
|
||||
connection.prepareStatement(
|
||||
"DELETE FROM peaceful_mobs WHERE world_name = ?"
|
||||
)) {
|
||||
|
||||
statement.setString(
|
||||
1,
|
||||
worldName
|
||||
);
|
||||
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -108,6 +108,15 @@ public class DatabaseManager {
|
||||
"""
|
||||
);
|
||||
|
||||
statement.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS peaceful_mobs (
|
||||
world_name TEXT PRIMARY KEY,
|
||||
enabled INTEGER NOT NULL
|
||||
)
|
||||
"""
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,15 +22,31 @@ public class MainMenu {
|
||||
"Main Menu"
|
||||
);
|
||||
|
||||
ItemStack creeper = new ItemStack(Material.CREEPER_HEAD);
|
||||
ItemStack creeper =
|
||||
new ItemStack(
|
||||
Material.CREEPER_HEAD
|
||||
);
|
||||
|
||||
ItemMeta creeperMeta = creeper.getItemMeta();
|
||||
ItemMeta creeperMeta =
|
||||
creeper.getItemMeta();
|
||||
|
||||
if (creeperMeta != null) {
|
||||
creeperMeta.setDisplayName("§aCreepers");
|
||||
creeper.setItemMeta(creeperMeta);
|
||||
|
||||
creeperMeta.setDisplayName(
|
||||
"§aCreepers"
|
||||
);
|
||||
|
||||
creeper.setItemMeta(
|
||||
creeperMeta
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
inventory.setItem(
|
||||
11,
|
||||
creeper
|
||||
);
|
||||
|
||||
ItemStack scanWorlds =
|
||||
new ItemStack(
|
||||
Material.COMPASS
|
||||
@@ -40,20 +56,29 @@ public class MainMenu {
|
||||
scanWorlds.getItemMeta();
|
||||
|
||||
if (scanMeta != null) {
|
||||
scanMeta.setDisplayName("§aRescan Worlds");
|
||||
|
||||
scanMeta.setLore(java.util.List.of(
|
||||
"§7Rescan for new worlds",
|
||||
"",
|
||||
"§eClick to rescan."
|
||||
));
|
||||
scanMeta.setDisplayName(
|
||||
"§aRescan Worlds"
|
||||
);
|
||||
|
||||
scanMeta.setLore(
|
||||
java.util.List.of(
|
||||
"§7Rescan for new worlds",
|
||||
"",
|
||||
"§eClick to rescan."
|
||||
)
|
||||
);
|
||||
|
||||
scanWorlds.setItemMeta(
|
||||
scanMeta
|
||||
);
|
||||
|
||||
scanWorlds.setItemMeta(scanMeta);
|
||||
}
|
||||
|
||||
inventory.setItem(4, scanWorlds);
|
||||
|
||||
inventory.setItem(11, creeper);
|
||||
inventory.setItem(
|
||||
4,
|
||||
scanWorlds
|
||||
);
|
||||
|
||||
ItemStack mobDrops =
|
||||
new ItemStack(
|
||||
@@ -81,19 +106,64 @@ public class MainMenu {
|
||||
);
|
||||
|
||||
ItemStack keepInventory =
|
||||
new ItemStack(Material.CHEST);
|
||||
new ItemStack(
|
||||
Material.CHEST
|
||||
);
|
||||
|
||||
ItemMeta keepMeta =
|
||||
keepInventory.getItemMeta();
|
||||
|
||||
if (keepMeta != null) {
|
||||
keepMeta.setDisplayName("§aKeep Inventory");
|
||||
keepInventory.setItemMeta(keepMeta);
|
||||
|
||||
keepMeta.setDisplayName(
|
||||
"§aKeep Inventory"
|
||||
);
|
||||
|
||||
keepInventory.setItemMeta(
|
||||
keepMeta
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
inventory.setItem(15, keepInventory);
|
||||
inventory.setItem(
|
||||
15,
|
||||
keepInventory
|
||||
);
|
||||
|
||||
GUIUtils.fillEmptySlots(inventory);
|
||||
ItemStack peacefulMobs =
|
||||
new ItemStack(
|
||||
Material.ZOMBIE_HEAD
|
||||
);
|
||||
|
||||
ItemMeta peacefulMeta =
|
||||
peacefulMobs.getItemMeta();
|
||||
|
||||
if (peacefulMeta != null) {
|
||||
|
||||
peacefulMeta.setDisplayName(
|
||||
"§aPeaceful Mobs"
|
||||
);
|
||||
|
||||
peacefulMeta.setLore(
|
||||
java.util.List.of(
|
||||
"§7Prevent mobs from targeting players"
|
||||
)
|
||||
);
|
||||
|
||||
peacefulMobs.setItemMeta(
|
||||
peacefulMeta
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
inventory.setItem(
|
||||
22,
|
||||
peacefulMobs
|
||||
);
|
||||
|
||||
GUIUtils.fillEmptySlots(
|
||||
inventory
|
||||
);
|
||||
|
||||
return inventory;
|
||||
|
||||
|
||||
@@ -0,0 +1,281 @@
|
||||
package com.rsscripting.serveradmin.gui;
|
||||
|
||||
import com.rsscripting.serveradmin.RSServerAdmin;
|
||||
import com.rsscripting.serveradmin.menuholder.RSMenuHolder;
|
||||
import com.rsscripting.serveradmin.settings.SettingKey;
|
||||
import com.rsscripting.serveradmin.utils.GUIUtils;
|
||||
import com.rsscripting.serveradmin.utils.MenuLayoutUtils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class PeacefulMobsMenu {
|
||||
|
||||
private static final int WORLDS_PER_PAGE = 45;
|
||||
|
||||
public static Inventory create(
|
||||
int page
|
||||
) throws SQLException {
|
||||
|
||||
RSMenuHolder holder =
|
||||
new RSMenuHolder(
|
||||
"PEACEFUL_MOBS"
|
||||
);
|
||||
|
||||
holder.setPage(
|
||||
page
|
||||
);
|
||||
|
||||
Inventory inventory =
|
||||
Bukkit.createInventory(
|
||||
holder,
|
||||
54,
|
||||
"Peaceful Mobs"
|
||||
);
|
||||
|
||||
GUIUtils.fillEmptySlots(
|
||||
inventory
|
||||
);
|
||||
|
||||
createBackButton(
|
||||
inventory
|
||||
);
|
||||
|
||||
createDefaultButton(
|
||||
inventory
|
||||
);
|
||||
|
||||
populateWorlds(
|
||||
inventory,
|
||||
page
|
||||
);
|
||||
|
||||
return inventory;
|
||||
|
||||
}
|
||||
|
||||
private static void createBackButton(
|
||||
Inventory inventory
|
||||
) {
|
||||
|
||||
ItemStack item =
|
||||
new ItemStack(
|
||||
Material.ARROW
|
||||
);
|
||||
|
||||
ItemMeta meta =
|
||||
item.getItemMeta();
|
||||
|
||||
if (meta != null) {
|
||||
|
||||
meta.setDisplayName(
|
||||
"§cBack"
|
||||
);
|
||||
|
||||
item.setItemMeta(
|
||||
meta
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
inventory.setItem(
|
||||
3,
|
||||
item
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
private static void createDefaultButton(
|
||||
Inventory inventory
|
||||
) throws SQLException {
|
||||
|
||||
boolean enabled =
|
||||
RSServerAdmin.getInstance()
|
||||
.getSettingsDAO()
|
||||
.getDefault(
|
||||
SettingKey.PEACEFUL_MOBS_NEW_WORLD
|
||||
);
|
||||
|
||||
ItemStack item =
|
||||
new ItemStack(
|
||||
enabled
|
||||
? Material.LIME_DYE
|
||||
: Material.GRAY_DYE
|
||||
);
|
||||
|
||||
ItemMeta meta =
|
||||
item.getItemMeta();
|
||||
|
||||
if (meta != null) {
|
||||
|
||||
meta.setDisplayName(
|
||||
"§eDefault For New Worlds"
|
||||
);
|
||||
|
||||
meta.setLore(
|
||||
List.of(
|
||||
"§7Controls newly detected worlds",
|
||||
"",
|
||||
enabled
|
||||
? "§aCurrent: Mobs cannot target players"
|
||||
: "§cCurrent: Mobs can target players"
|
||||
)
|
||||
);
|
||||
|
||||
item.setItemMeta(
|
||||
meta
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
|
||||
GUIUtils.addGlow(
|
||||
item
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
inventory.setItem(
|
||||
5,
|
||||
item
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
private static void populateWorlds(
|
||||
Inventory inventory,
|
||||
int page
|
||||
) throws SQLException {
|
||||
|
||||
List<String> worlds =
|
||||
RSServerAdmin.getInstance()
|
||||
.getWorldsDAO()
|
||||
.getAllWorlds();
|
||||
|
||||
int start =
|
||||
(page - 1) * WORLDS_PER_PAGE;
|
||||
|
||||
int end =
|
||||
Math.min(
|
||||
start + WORLDS_PER_PAGE,
|
||||
worlds.size()
|
||||
);
|
||||
|
||||
int worldCount =
|
||||
end - start;
|
||||
|
||||
for (int i = start;
|
||||
i < end;
|
||||
i++) {
|
||||
|
||||
String worldName =
|
||||
worlds.get(i);
|
||||
|
||||
boolean enabled =
|
||||
RSServerAdmin.getInstance()
|
||||
.getPeacefulMobsDAO()
|
||||
.getWorldSetting(
|
||||
worldName
|
||||
);
|
||||
|
||||
ItemStack item =
|
||||
new ItemStack(
|
||||
enabled
|
||||
? Material.LIME_SHULKER_BOX
|
||||
: Material.RED_SHULKER_BOX
|
||||
);
|
||||
|
||||
ItemMeta meta =
|
||||
item.getItemMeta();
|
||||
|
||||
if (meta != null) {
|
||||
|
||||
meta.setDisplayName(
|
||||
"§e" + worldName
|
||||
);
|
||||
|
||||
meta.getPersistentDataContainer()
|
||||
.set(
|
||||
new NamespacedKey(
|
||||
RSServerAdmin.getInstance(),
|
||||
"world_name"
|
||||
),
|
||||
PersistentDataType.STRING,
|
||||
worldName
|
||||
);
|
||||
|
||||
meta.setLore(
|
||||
List.of(
|
||||
"§7Peaceful Mobs",
|
||||
"",
|
||||
enabled
|
||||
? "§aCurrent: Mobs cannot target players"
|
||||
: "§cCurrent: Mobs can target players"
|
||||
)
|
||||
);
|
||||
|
||||
item.setItemMeta(
|
||||
meta
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
int relativeIndex =
|
||||
i - start;
|
||||
|
||||
int row =
|
||||
relativeIndex / 9;
|
||||
|
||||
int indexInRow =
|
||||
relativeIndex % 9;
|
||||
|
||||
int itemsRemaining =
|
||||
worldCount - (row * 9);
|
||||
|
||||
boolean lastPartialRow =
|
||||
itemsRemaining < 9;
|
||||
|
||||
int slot;
|
||||
|
||||
if (lastPartialRow) {
|
||||
|
||||
List<Integer> rowSlots =
|
||||
MenuLayoutUtils.getCenteredSlots(
|
||||
itemsRemaining
|
||||
);
|
||||
|
||||
slot =
|
||||
9
|
||||
+ (row * 9)
|
||||
+ rowSlots.get(
|
||||
indexInRow
|
||||
);
|
||||
|
||||
} else {
|
||||
|
||||
slot =
|
||||
9
|
||||
+ (row * 9)
|
||||
+ indexInRow;
|
||||
|
||||
}
|
||||
|
||||
inventory.setItem(
|
||||
slot,
|
||||
item
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -73,6 +73,13 @@ public class MainMenuListener implements Listener {
|
||||
SettingKey.CREEPER_ENTITY_DAMAGE
|
||||
);
|
||||
|
||||
boolean peacefulMobsDefault =
|
||||
RSServerAdmin.getInstance()
|
||||
.getSettingsDAO()
|
||||
.getDefault(
|
||||
SettingKey.PEACEFUL_MOBS_NEW_WORLD
|
||||
);
|
||||
|
||||
for (org.bukkit.World world
|
||||
: org.bukkit.Bukkit.getWorlds()) {
|
||||
|
||||
@@ -103,6 +110,13 @@ public class MainMenuListener implements Listener {
|
||||
world.getName()
|
||||
);
|
||||
|
||||
RSServerAdmin.getInstance()
|
||||
.getPeacefulMobsDAO()
|
||||
.ensureWorldExists(
|
||||
world.getName(),
|
||||
peacefulMobsDefault
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
player.sendMessage(
|
||||
@@ -188,6 +202,33 @@ public class MainMenuListener implements Listener {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
| Peaceful Mobs Menu
|
||||
*/
|
||||
|
||||
case 22 -> {
|
||||
|
||||
try {
|
||||
|
||||
player.openInventory(
|
||||
PeacefulMobsMenu.create(
|
||||
1
|
||||
)
|
||||
);
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
RSServerAdmin.getInstance()
|
||||
.getLogger()
|
||||
.warning(
|
||||
"Failed to open Peaceful Mobs menu: "
|
||||
+ ex.getMessage()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,294 @@
|
||||
package com.rsscripting.serveradmin.listeners;
|
||||
|
||||
import com.rsscripting.serveradmin.RSServerAdmin;
|
||||
import com.rsscripting.serveradmin.gui.MainMenu;
|
||||
import com.rsscripting.serveradmin.gui.PeacefulMobsMenu;
|
||||
import com.rsscripting.serveradmin.menuholder.RSMenuHolder;
|
||||
import com.rsscripting.serveradmin.settings.SettingKey;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Mob;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityTargetEvent;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PeacefulMobsListener implements Listener {
|
||||
|
||||
private final RSServerAdmin plugin =
|
||||
RSServerAdmin.getInstance();
|
||||
|
||||
private final Map<String, Boolean> peacefulWorlds =
|
||||
new HashMap<>();
|
||||
|
||||
@EventHandler(
|
||||
priority = EventPriority.HIGHEST,
|
||||
ignoreCancelled = false
|
||||
)
|
||||
public void onEntityTarget(
|
||||
EntityTargetEvent event
|
||||
) {
|
||||
|
||||
if (!(event.getEntity() instanceof Mob mob)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(event.getTarget() instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String worldName =
|
||||
mob.getWorld().getName();
|
||||
|
||||
try {
|
||||
|
||||
if (isPeaceful(worldName)) {
|
||||
|
||||
event.setTarget(null);
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
plugin.getLogger().warning(
|
||||
"Failed to check Peaceful Mobs setting for world "
|
||||
+ worldName
|
||||
+ ": "
|
||||
+ ex.getMessage()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean isPeaceful(
|
||||
String worldName
|
||||
) throws Exception {
|
||||
|
||||
Boolean cached =
|
||||
peacefulWorlds.get(
|
||||
worldName
|
||||
);
|
||||
|
||||
if (cached != null) {
|
||||
|
||||
return cached;
|
||||
|
||||
}
|
||||
|
||||
boolean enabled =
|
||||
plugin.getPeacefulMobsDAO()
|
||||
.getWorldSetting(
|
||||
worldName
|
||||
);
|
||||
|
||||
peacefulWorlds.put(
|
||||
worldName,
|
||||
enabled
|
||||
);
|
||||
|
||||
return enabled;
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(
|
||||
InventoryClickEvent event
|
||||
) {
|
||||
|
||||
if (!(event.getInventory().getHolder()
|
||||
instanceof RSMenuHolder holder)) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if (!holder.getMenuId().equals(
|
||||
"PEACEFUL_MOBS"
|
||||
)) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if (event.getClickedInventory() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getClickedInventory()
|
||||
!= event.getView().getTopInventory()) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
event.setCancelled(
|
||||
true
|
||||
);
|
||||
|
||||
if (!(event.getWhoClicked()
|
||||
instanceof Player player)) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
switch (event.getSlot()) {
|
||||
|
||||
case 3 -> player.openInventory(
|
||||
MainMenu.create()
|
||||
);
|
||||
|
||||
case 5 -> {
|
||||
|
||||
try {
|
||||
|
||||
plugin.getSettingsDAO()
|
||||
.toggleDefault(
|
||||
SettingKey.PEACEFUL_MOBS_NEW_WORLD
|
||||
);
|
||||
|
||||
player.openInventory(
|
||||
PeacefulMobsMenu.create(
|
||||
holder.getPage()
|
||||
)
|
||||
);
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
plugin.getLogger().warning(
|
||||
"Failed to update Peaceful Mobs default: "
|
||||
+ ex.getMessage()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
default -> {
|
||||
|
||||
if (event.getSlot() < 9) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
if (event.getCurrentItem() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!event.getCurrentItem().hasItemMeta()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemMeta meta =
|
||||
event.getCurrentItem()
|
||||
.getItemMeta();
|
||||
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String worldName =
|
||||
meta.getPersistentDataContainer()
|
||||
.get(
|
||||
new NamespacedKey(
|
||||
plugin,
|
||||
"world_name"
|
||||
),
|
||||
PersistentDataType.STRING
|
||||
);
|
||||
|
||||
if (worldName == null
|
||||
|| worldName.isBlank()) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
boolean enabled =
|
||||
plugin.getPeacefulMobsDAO()
|
||||
.getWorldSetting(
|
||||
worldName
|
||||
);
|
||||
|
||||
boolean newValue =
|
||||
!enabled;
|
||||
|
||||
plugin.getPeacefulMobsDAO()
|
||||
.setWorldSetting(
|
||||
worldName,
|
||||
newValue
|
||||
);
|
||||
|
||||
peacefulWorlds.put(
|
||||
worldName,
|
||||
newValue
|
||||
);
|
||||
|
||||
if (newValue) {
|
||||
|
||||
World world =
|
||||
Bukkit.getWorld(
|
||||
worldName
|
||||
);
|
||||
|
||||
if (world != null) {
|
||||
|
||||
world.getEntitiesByClass(
|
||||
Mob.class
|
||||
)
|
||||
.forEach(
|
||||
mob ->
|
||||
mob.setTarget(
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
player.openInventory(
|
||||
PeacefulMobsMenu.create(
|
||||
holder.getPage()
|
||||
)
|
||||
);
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
plugin.getLogger().warning(
|
||||
"Failed to update Peaceful Mobs world setting: "
|
||||
+ ex.getMessage()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void cacheWorldSetting(
|
||||
String worldName,
|
||||
boolean enabled
|
||||
) {
|
||||
|
||||
peacefulWorlds.put(
|
||||
worldName,
|
||||
enabled
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,85 +2,100 @@ package com.rsscripting.serveradmin.listeners;
|
||||
|
||||
import com.rsscripting.serveradmin.RSServerAdmin;
|
||||
import com.rsscripting.serveradmin.settings.SettingKey;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Mob;
|
||||
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 {
|
||||
|
||||
private final RSServerAdmin plugin =
|
||||
RSServerAdmin.getInstance();
|
||||
|
||||
@EventHandler
|
||||
public void onWorldLoad(
|
||||
WorldLoadEvent event
|
||||
) {
|
||||
|
||||
World world = event.getWorld();
|
||||
|
||||
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
|
||||
plugin.getSettingsDAO().getDefault(
|
||||
SettingKey.KEEP_INVENTORY_NEW_WORLD
|
||||
);
|
||||
|
||||
plugin.getWorldsDAO().ensureWorldExists(
|
||||
world.getName()
|
||||
);
|
||||
|
||||
plugin.getKeepInventoryDAO().ensureWorldExists(
|
||||
world.getName(),
|
||||
keepInventoryDefault
|
||||
);
|
||||
|
||||
boolean blockDamageDefault =
|
||||
plugin.getSettingsDAO()
|
||||
.getDefault(
|
||||
SettingKey.CREEPER_BLOCK_DAMAGE
|
||||
);
|
||||
plugin.getSettingsDAO().getDefault(
|
||||
SettingKey.CREEPER_BLOCK_DAMAGE
|
||||
);
|
||||
|
||||
boolean entityDamageDefault =
|
||||
plugin.getSettingsDAO()
|
||||
.getDefault(
|
||||
SettingKey.CREEPER_ENTITY_DAMAGE
|
||||
);
|
||||
|
||||
plugin.getCreeperSettingsDAO()
|
||||
.ensureWorldExists(
|
||||
event.getWorld().getName(),
|
||||
blockDamageDefault,
|
||||
entityDamageDefault
|
||||
plugin.getSettingsDAO().getDefault(
|
||||
SettingKey.CREEPER_ENTITY_DAMAGE
|
||||
);
|
||||
|
||||
plugin.getEquipmentDropSettingsDAO()
|
||||
.ensureWorldExists(
|
||||
event.getWorld().getName()
|
||||
plugin.getCreeperSettingsDAO().ensureWorldExists(
|
||||
world.getName(),
|
||||
blockDamageDefault,
|
||||
entityDamageDefault
|
||||
);
|
||||
|
||||
plugin.getEquipmentDropSettingsDAO().ensureWorldExists(
|
||||
world.getName()
|
||||
);
|
||||
|
||||
boolean peacefulMobsDefault =
|
||||
plugin.getSettingsDAO().getDefault(
|
||||
SettingKey.PEACEFUL_MOBS_NEW_WORLD
|
||||
);
|
||||
|
||||
boolean keepInventoryEnabled =
|
||||
plugin.getKeepInventoryDAO()
|
||||
.getWorldSetting(
|
||||
event.getWorld().getName()
|
||||
);
|
||||
plugin.getPeacefulMobsDAO().ensureWorldExists(
|
||||
world.getName(),
|
||||
peacefulMobsDefault
|
||||
);
|
||||
|
||||
event.getWorld().setGameRule(
|
||||
GameRule.KEEP_INVENTORY,
|
||||
keepInventoryEnabled
|
||||
boolean peacefulMobsEnabled =
|
||||
plugin.getPeacefulMobsDAO().getWorldSetting(
|
||||
world.getName()
|
||||
);
|
||||
|
||||
if (peacefulMobsEnabled) {
|
||||
|
||||
world.getEntitiesByClass(Mob.class)
|
||||
.forEach(mob -> mob.setTarget(null));
|
||||
|
||||
}
|
||||
|
||||
boolean keepInventory =
|
||||
plugin.getKeepInventoryDAO().getWorldSetting(
|
||||
world.getName()
|
||||
);
|
||||
|
||||
world.setGameRule(
|
||||
org.bukkit.GameRule.KEEP_INVENTORY,
|
||||
keepInventory
|
||||
);
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
RSServerAdmin.getInstance()
|
||||
.getLogger()
|
||||
.warning(
|
||||
"Failed to register world settings for "
|
||||
+ event.getWorld().getName()
|
||||
+ ": "
|
||||
+ ex.getMessage()
|
||||
);
|
||||
plugin.getLogger().severe(
|
||||
"Failed to initialize settings for world "
|
||||
+ world.getName()
|
||||
+ ": "
|
||||
+ ex.getMessage()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ public enum SettingKey {
|
||||
CREEPER_BLOCK_DAMAGE,
|
||||
CREEPER_ENTITY_DAMAGE,
|
||||
KEEP_INVENTORY_NEW_WORLD,
|
||||
KEEP_INVENTORY
|
||||
KEEP_INVENTORY,
|
||||
PEACEFUL_MOBS_NEW_WORLD
|
||||
|
||||
}
|
||||
@@ -36,6 +36,12 @@ public class WorldSyncUtils {
|
||||
SettingKey.CREEPER_ENTITY_DAMAGE
|
||||
);
|
||||
|
||||
boolean peacefulMobsDefault =
|
||||
plugin.getSettingsDAO()
|
||||
.getDefault(
|
||||
SettingKey.PEACEFUL_MOBS_NEW_WORLD
|
||||
);
|
||||
|
||||
Set<String> bukkitWorlds =
|
||||
new HashSet<>();
|
||||
|
||||
@@ -75,6 +81,12 @@ public class WorldSyncUtils {
|
||||
worldName
|
||||
);
|
||||
|
||||
plugin.getPeacefulMobsDAO()
|
||||
.ensureWorldExists(
|
||||
worldName,
|
||||
peacefulMobsDefault
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in new issue
Block a user