From 632b85983a888b59bf5ac38e92c39408a6e13ac7 Mon Sep 17 00:00:00 2001 From: Genre Mamanao <41022228+Faun471@users.noreply.github.com> Date: Sat, 21 Oct 2023 04:57:59 +0800 Subject: [PATCH] Implemented Item Loss Chance (#755) * Implemented Item Loss Chance * Removed debug stuff * Use Map#compute * whoops * changed datatype of time from long to LocalDateTime * Refactored, used early return pattern, changed from hashmap to arraylist and removed TableManager#save * Removed unused imports --- .../iridiumskyblock/IridiumSkyblock.java | 1 + .../iridiumskyblock/configs/Messages.java | 2 + .../iridiumskyblock/database/LostItems.java | 52 ++++++++++++++++ .../listeners/PlayerMoveListener.java | 60 ++++++++++++++++--- .../managers/DatabaseManager.java | 8 +-- .../tablemanagers/LostItemsTableManager.java | 26 ++++++++ 6 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/iridium/iridiumskyblock/database/LostItems.java create mode 100644 src/main/java/com/iridium/iridiumskyblock/managers/tablemanagers/LostItemsTableManager.java diff --git a/src/main/java/com/iridium/iridiumskyblock/IridiumSkyblock.java b/src/main/java/com/iridium/iridiumskyblock/IridiumSkyblock.java index 846ea2ed5..4c8eca0bd 100644 --- a/src/main/java/com/iridium/iridiumskyblock/IridiumSkyblock.java +++ b/src/main/java/com/iridium/iridiumskyblock/IridiumSkyblock.java @@ -213,6 +213,7 @@ public void saveData() { getDatabaseManager().getTeamMissionDataTableManager().save(); getDatabaseManager().getTeamRewardsTableManager().save(); getDatabaseManager().getTeamSettingsTableManager().save(); + getDatabaseManager().getLostItemsTableManager().save(); } @Override diff --git a/src/main/java/com/iridium/iridiumskyblock/configs/Messages.java b/src/main/java/com/iridium/iridiumskyblock/configs/Messages.java index b50c8a708..128b07c0d 100644 --- a/src/main/java/com/iridium/iridiumskyblock/configs/Messages.java +++ b/src/main/java/com/iridium/iridiumskyblock/configs/Messages.java @@ -9,6 +9,8 @@ public Messages() { } public String voidTeleport = "%prefix% &7You have fallen off your island. Teleporting home..."; + public String itemsString = "%amount% %item_name%"; + public String voidLostItems = "%prefix% &7You've lost %items%!"; public String netherIslandsDisabled = "%prefix% &7Nether islands have been disabled."; public String netherLocked = "%prefix% &7Reach Island level %level% to unlock the Nether."; public String islandBorderChanged = "%prefix% &7%player% has changed your Island border to %color%."; diff --git a/src/main/java/com/iridium/iridiumskyblock/database/LostItems.java b/src/main/java/com/iridium/iridiumskyblock/database/LostItems.java new file mode 100644 index 000000000..daf12c29a --- /dev/null +++ b/src/main/java/com/iridium/iridiumskyblock/database/LostItems.java @@ -0,0 +1,52 @@ +package com.iridium.iridiumskyblock.database; + +import com.iridium.iridiumcore.utils.ItemStackUtils; +import com.iridium.iridiumteams.database.DatabaseObject; +import com.j256.ormlite.field.DataType; +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.UUID; + +@NoArgsConstructor +@Getter +@Setter +@DatabaseTable(tableName = "lost_items") +public class LostItems extends DatabaseObject { + @DatabaseField(columnName = "id", generatedId = true, canBeNull = false, unique = true) + private int id; + + @DatabaseField(columnName = "uuid", canBeNull = false) + private @NotNull UUID uuid; + + @DatabaseField(columnName = "time") + private LocalDateTime time; + + @DatabaseField(columnName = "items", dataType = DataType.SERIALIZABLE) + private String[] items; + + public void setItems(ItemStack[] items) { + this.items = Arrays.stream(items) + .map(ItemStackUtils::serialize) + .toArray(String[]::new); + } + + public ItemStack[] getItems() { + return Arrays.stream(items) + .map(ItemStackUtils::deserialize) + .toArray(ItemStack[]::new); + } + + public LostItems(@NotNull UUID uuid, ItemStack[] items) { + this.uuid = uuid; + this.time = LocalDateTime.now(); + setItems(items); + } +} diff --git a/src/main/java/com/iridium/iridiumskyblock/listeners/PlayerMoveListener.java b/src/main/java/com/iridium/iridiumskyblock/listeners/PlayerMoveListener.java index af479048b..97c42dea6 100644 --- a/src/main/java/com/iridium/iridiumskyblock/listeners/PlayerMoveListener.java +++ b/src/main/java/com/iridium/iridiumskyblock/listeners/PlayerMoveListener.java @@ -2,12 +2,17 @@ import com.iridium.iridiumcore.utils.StringUtils; import com.iridium.iridiumskyblock.IridiumSkyblock; +import com.iridium.iridiumskyblock.database.LostItems; import com.iridium.iridiumskyblock.database.User; import com.iridium.iridiumskyblock.enhancements.VoidEnhancementData; import com.iridium.iridiumteams.utils.LocationUtils; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; +import java.util.stream.Collectors; public class PlayerMoveListener implements Listener { @@ -18,17 +23,56 @@ public void onPlayerMove(PlayerMoveEvent event) { IridiumSkyblock.getInstance().getTeamManager().sendIslandBorder(event.getPlayer()); user.getCurrentIsland().ifPresent(island -> { - if (event.getPlayer().getLocation().getY() < LocationUtils.getMinHeight(event.getPlayer().getWorld())) { - VoidEnhancementData voidEnhancementData = IridiumSkyblock.getInstance().getEnhancements().voidEnhancement.levels.get(IridiumSkyblock.getInstance().getTeamManager().getTeamEnhancement(island, "void").getLevel()); - if (voidEnhancementData != null && voidEnhancementData.enabled) { - if (IridiumSkyblock.getInstance().getTeamManager().teleport(event.getPlayer(), island.getHome(), island)) { - event.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().voidTeleport - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) - )); + if (event.getPlayer().getLocation().getY() >= LocationUtils.getMinHeight(event.getPlayer().getWorld())) return; + + VoidEnhancementData voidEnhancementData = IridiumSkyblock.getInstance() + .getEnhancements().voidEnhancement.levels + .get(IridiumSkyblock.getInstance().getTeamManager().getTeamEnhancement(island, "void").getLevel()); + + if (voidEnhancementData == null || !voidEnhancementData.enabled) return; + + if (!IridiumSkyblock.getInstance().getTeamManager().teleport(event.getPlayer(), island.getHome(), island)) return; + + event.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().voidTeleport + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); + + if (voidEnhancementData.itemLossChance <= 0) return; + + ArrayList lostItems = new ArrayList<>(); + for (ItemStack item : event.getPlayer().getInventory().getContents()) { + if (item == null) continue; + + ItemStack originalItem = item.clone(); + + int lostAmount = 0; + for (int i = 0; i < item.getAmount(); i++) { + if (Math.random() * 100 <= voidEnhancementData.itemLossChance) { + lostAmount++; } } + + if (lostAmount == 0) continue; + + int newAmount = originalItem.getAmount() - lostAmount; + item.setAmount(newAmount); + + originalItem.setAmount(lostAmount); + lostItems.add(originalItem); } + + IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().addEntry(new LostItems( + event.getPlayer().getUniqueId(), lostItems.toArray(new ItemStack[0]))); + + event.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance() + .getMessages().voidLostItems + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) + .replace("%items%", lostItems.stream() + .map(item -> IridiumSkyblock.getInstance().getMessages().itemsString + .replace("%amount%", String.valueOf(item.getAmount())) + .replace("%item_name%", item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : "%type%") + .replace("%type%", item.getType().name().trim().replace("_", " "))) + .collect(Collectors.joining(", "))) + )); }); } - } diff --git a/src/main/java/com/iridium/iridiumskyblock/managers/DatabaseManager.java b/src/main/java/com/iridium/iridiumskyblock/managers/DatabaseManager.java index 235bc8f2f..75d01a9a3 100644 --- a/src/main/java/com/iridium/iridiumskyblock/managers/DatabaseManager.java +++ b/src/main/java/com/iridium/iridiumskyblock/managers/DatabaseManager.java @@ -3,10 +3,8 @@ import com.iridium.iridiumskyblock.IridiumSkyblock; import com.iridium.iridiumskyblock.configs.SQL; import com.iridium.iridiumskyblock.database.Island; -import com.iridium.iridiumskyblock.managers.tablemanagers.ForeignIslandTableManager; -import com.iridium.iridiumskyblock.managers.tablemanagers.IslandTableManager; -import com.iridium.iridiumskyblock.managers.tablemanagers.TableManager; -import com.iridium.iridiumskyblock.managers.tablemanagers.UserTableManager; +import com.iridium.iridiumskyblock.database.LostItems; +import com.iridium.iridiumskyblock.managers.tablemanagers.*; import com.iridium.iridiumteams.database.*; import com.iridium.iridiumteams.database.types.*; import com.j256.ormlite.field.DataPersisterManager; @@ -32,6 +30,7 @@ public class DatabaseManager { private UserTableManager userTableManager; private IslandTableManager islandTableManager; + private LostItemsTableManager lostItemsTableManager; private ForeignIslandTableManager invitesTableManager; private ForeignIslandTableManager trustTableManager; private ForeignIslandTableManager permissionsTableManager; @@ -78,6 +77,7 @@ public void init() throws SQLException { this.teamMissionDataTableManager = new TableManager<>(connectionSource, TeamMissionData.class, Comparator.comparing(TeamMissionData::getMissionID).thenComparing(TeamMissionData::getMissionIndex)); this.teamRewardsTableManager = new ForeignIslandTableManager<>(connectionSource, TeamReward.class, Comparator.comparing(TeamReward::getTeamID)); this.teamSettingsTableManager = new ForeignIslandTableManager<>(connectionSource, TeamSetting.class, Comparator.comparing(TeamSetting::getTeamID).thenComparing(TeamSetting::getSetting)); + this.lostItemsTableManager = new LostItemsTableManager(connectionSource, LostItems.class, Comparator.comparing(LostItems::getUuid)); } /** diff --git a/src/main/java/com/iridium/iridiumskyblock/managers/tablemanagers/LostItemsTableManager.java b/src/main/java/com/iridium/iridiumskyblock/managers/tablemanagers/LostItemsTableManager.java new file mode 100644 index 000000000..0349b5280 --- /dev/null +++ b/src/main/java/com/iridium/iridiumskyblock/managers/tablemanagers/LostItemsTableManager.java @@ -0,0 +1,26 @@ +package com.iridium.iridiumskyblock.managers.tablemanagers; + +import com.iridium.iridiumskyblock.database.LostItems; +import com.j256.ormlite.support.ConnectionSource; + +import java.sql.SQLException; +import java.util.*; +import java.util.stream.Collectors; + +public class LostItemsTableManager extends TableManager { + public LostItemsTableManager(ConnectionSource connectionSource, Class clazz, Comparator comparator) throws SQLException { + super(connectionSource, clazz, comparator); + sort(); + } + + public void sort() { + getEntries().sort(Comparator.comparing(LostItems::getUuid)); + } + + public List getLostItems(UUID uuid) { + return getEntries().stream() + .filter(item -> item.getUuid().equals(uuid)) + .sorted(Comparator.comparing(LostItems::getTime).reversed()) + .collect(Collectors.toList()); + } +} \ No newline at end of file