Skip to content

Commit

Permalink
Implemented Item Loss Chance (#755)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Faun471 authored Oct 20, 2023
1 parent 3c5e593 commit 632b859
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public void saveData() {
getDatabaseManager().getTeamMissionDataTableManager().save();
getDatabaseManager().getTeamRewardsTableManager().save();
getDatabaseManager().getTeamSettingsTableManager().save();
getDatabaseManager().getLostItemsTableManager().save();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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%.";
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/iridium/iridiumskyblock/database/LostItems.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<ItemStack> 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(", ")))
));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +30,7 @@ public class DatabaseManager {

private UserTableManager userTableManager;
private IslandTableManager islandTableManager;
private LostItemsTableManager lostItemsTableManager;
private ForeignIslandTableManager<TeamInvite, Integer> invitesTableManager;
private ForeignIslandTableManager<TeamTrust, Integer> trustTableManager;
private ForeignIslandTableManager<TeamPermission, Integer> permissionsTableManager;
Expand Down Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LostItems, Integer> {
public LostItemsTableManager(ConnectionSource connectionSource, Class<LostItems> clazz, Comparator<LostItems> comparator) throws SQLException {
super(connectionSource, clazz, comparator);
sort();
}

public void sort() {
getEntries().sort(Comparator.comparing(LostItems::getUuid));
}

public List<LostItems> getLostItems(UUID uuid) {
return getEntries().stream()
.filter(item -> item.getUuid().equals(uuid))
.sorted(Comparator.comparing(LostItems::getTime).reversed())
.collect(Collectors.toList());
}
}

0 comments on commit 632b859

Please sign in to comment.