Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;

import javax.annotation.Nonnull;

Expand All @@ -20,11 +26,13 @@ public class RelicInventoryManager {

private static final String CONFIG_PREFIX = "inventories.";
private static final String FILE_NAME = "relics-inventories.yml";
private static final long AUTOSAVE_TICKS = 20L * 60;

private final SlimeFrame plugin;
private final Map<UUID, RelicInventory> relicInventories = new HashMap<>();
private File file;
private FileConfiguration configFile;
private String lastSaved = "";

public RelicInventoryManager(SlimeFrame plugin) {
this.plugin = plugin;
Expand All @@ -40,6 +48,10 @@ public void setup() {
}
}
this.configFile = YamlConfiguration.loadConfiguration(file);
this.lastSaved = configFile.saveToString();
// Relics used to be saved only in onDisable, so a crash lost every relic earned since the last restart.
// Check once a minute and write only when something changed.
plugin.getServer().getScheduler().runTaskTimer(plugin, this::saveConfig, AUTOSAVE_TICKS, AUTOSAVE_TICKS);
}

public void saveConfig() {
Expand All @@ -51,10 +63,21 @@ public void saveConfig() {
}
}

String data = configFile.saveToString();
if (data.equals(lastSaved)) return;
// Write a temp file and swap it in, so a crash mid-write cannot leave a truncated file
Path target = file.toPath();
Path tmp = target.resolveSibling(FILE_NAME + ".tmp");
try {
configFile.save(file);
Files.writeString(tmp, data, StandardCharsets.UTF_8);
try {
Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException e) {
Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING);
}
lastSaved = data;
} catch (IOException e) {
e.printStackTrace();
plugin.getLogger().log(Level.WARNING, "Could not save " + FILE_NAME, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,19 @@ public void onEntityKill(EntityDeathEvent e) {

private boolean canDrop(Player p, ItemStack item, EntityType entity) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
if (sfItem == null) return false;
if (!sfItem.canUse(p, true)) return false;
// Only SlimeFrame's own drops are handled here, and a locked one is withheld silently. This used to call
// canUse(p, true) on every registered mob drop, so each kill sent a message per locked drop of that mob
// (on top of Slimefun's own MobDropListener).
if (!(sfItem instanceof AdvancedMobDrop advancedMobDrop)) return false;

if (sfItem instanceof AdvancedMobDrop advancedMobDrop) {
// Will be handled by the Slimefun RandomMobDrop listener
if (advancedMobDrop.getMobDropChance() != 0) return false;

// Will be handled by the Slimefun RandomMobDrop listener
if (advancedMobDrop.getMobDropChance() != 0) return false;
if (!sfItem.canUse(p, false)) return false;

int random = ThreadLocalRandom.current().nextInt(100);
int random = ThreadLocalRandom.current().nextInt(100);

return advancedMobDrop.getMobDropChance(entity) > random;
}

return false;
return advancedMobDrop.getMobDropChance(entity) > random;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.libraries.dough.items.ItemUtils;

import me.voper.slimeframe.SlimeFrame;
import me.voper.slimeframe.core.managers.RelicInventoryManager;
import me.voper.slimeframe.implementation.items.relics.RelicInventory;
Expand Down Expand Up @@ -41,25 +40,44 @@ public void onInventoryClick(InventoryClickEvent e) {
ItemStack currentItem = e.getCurrentItem();
if (currentItem == null || currentItem.getType() == Material.AIR) return;
ItemStack clone = currentItem.clone();
if (!e.isShiftClick()) clone.setAmount(1);
int wanted = clone.getAmount();

int consume = 1;
if (!e.isShiftClick()) {
clone.setAmount(1);
} else {
consume = clone.getAmount();
}

// Take out only what actually fit. Nothing used to be removed when part of a shift-clicked stack was
// left over, so the part that did fit was duplicated. The slot is written back explicitly.
HashMap<Integer, ItemStack> nonAddedItems = p.getInventory().addItem(clone);
if (nonAddedItems.isEmpty()) {
ItemUtils.consumeItem(currentItem, consume, false);
// currentItem.setAmount(currentItem.getAmount() - 1);
} else {
p.sendMessage(ChatColor.RED + "Your inventory is full!");
int moved = wanted - nonAddedItems.values().stream().mapToInt(ItemStack::getAmount).sum();
if (moved > 0) {
int rest = currentItem.getAmount() - moved;
if (rest > 0) {
ItemStack remaining = currentItem.clone();
remaining.setAmount(rest);
clickedInventory.setItem(e.getSlot(), remaining);
} else {
clickedInventory.setItem(e.getSlot(), null);
}
}
if (moved < wanted) p.sendMessage(ChatColor.RED + "Your inventory is full!");
return;
}

if (e.isShiftClick()) e.setCancelled(true);
}

// The relic inventory only gives relics out. Dragging used to bypass the click guard, so any item could be
// stored there (a 54-slot backpack that opens anywhere and is kept on death).
@EventHandler(ignoreCancelled = true)
public void onInventoryDrag(InventoryDragEvent e) {
if (!(e.getWhoClicked() instanceof Player p)) return;
Inventory relicInventory = relicInventoryManager.getRelicInventory(p).getInventory();
if (!e.getInventory().equals(relicInventory)) return;
int topSize = e.getView().getTopInventory().getSize();
for (int rawSlot : e.getRawSlots()) {
if (rawSlot < topSize) {
e.setCancelled(true);
return;
}
}
}

}