Skip to content

Commit

Permalink
Loot table pause feature and getting rid of library loading system in…
Browse files Browse the repository at this point in the history
… new versions of the game
  • Loading branch information
AntonUden committed Dec 2, 2023
1 parent 0853247 commit a277f10
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import net.zeeraa.novacore.spigot.language.LanguageReader;
import net.zeeraa.novacore.spigot.librarymanagement.LibraryBlockedException;
import net.zeeraa.novacore.spigot.librarymanagement.LibraryEntry;
import net.zeeraa.novacore.spigot.librarymanagement.LibraryLoadCondition;
import net.zeeraa.novacore.spigot.librarymanagement.NovaCoreLibraryManager;
import net.zeeraa.novacore.spigot.logger.SpigotAbstractionLogger;
import net.zeeraa.novacore.spigot.loottable.LootTableManager;
Expand Down Expand Up @@ -142,13 +143,18 @@ public class NovaCore extends JavaPlugin implements Listener {
private static final List<LibraryEntry> BUILTIN_LIBRARIES = new ArrayList<>();

static {
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.examination.Examinable", "examination-api-1.3.1-SNAPSHOT.jar"));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.key.Keyed", "adventure-key-4.14.0.jar"));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.Adventure", "adventure-api-4.14.0.jar"));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.text.serializer.ComponentSerializer", "adventure-api-4.14.0.jar")); // Fix issue when packetevents is installed
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer", "adventure-text-serializer-legacy-4.14.0.jar"));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.text.serializer.json.JSONComponentSerializer", "adventure-text-serializer-json-4.14.0.jar"));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.text.serializer.gson.GsonComponentSerializer", "adventure-text-serializer-gson-4.14.0.jar"));
LibraryLoadCondition kyoriLibraryConditions = new LibraryLoadCondition();
kyoriLibraryConditions.addPreventLoadCondition(() -> {
return Bukkit.getPluginManager().getPlugin("NovaCore1_17Plus") != null;
});

BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.examination.Examinable", "examination-api-1.3.1-SNAPSHOT.jar", kyoriLibraryConditions));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.key.Keyed", "adventure-key-4.14.0.jar", kyoriLibraryConditions));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.Adventure", "adventure-api-4.14.0.jar", kyoriLibraryConditions));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.text.serializer.ComponentSerializer", "adventure-api-4.14.0.jar", kyoriLibraryConditions)); // Fix issue when packetevents is installed
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer", "adventure-text-serializer-legacy-4.14.0.jar", kyoriLibraryConditions));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.text.serializer.json.JSONComponentSerializer", "adventure-text-serializer-json-4.14.0.jar", kyoriLibraryConditions));
BUILTIN_LIBRARIES.add(new LibraryEntry("net.kyori.adventure.text.serializer.gson.GsonComponentSerializer", "adventure-text-serializer-gson-4.14.0.jar", kyoriLibraryConditions));
}

/**
Expand Down Expand Up @@ -508,7 +514,13 @@ public void onEnable() {

boolean dontShutdownOnFail = libraryConfig.getBoolean("DoNotShutdownOnFail", false);
for (LibraryEntry lib : BUILTIN_LIBRARIES) {
Log.debug("NovaCore", "Checking if library " + lib.getLibraryName() + " needs to be loaded. Class: " + lib.getClassName());
if (!lib.shouldLoad()) {
Log.debug("NovaCore", "Skipping library " + lib.getLibraryName() + " dut to blocking load conditions");
continue;
}

// Log.debug("NovaCore", "Checking if library " + lib.getLibraryName() + " needs
// to be loaded. Class: " + lib.getClassName());
try {
if (libraryManager.loadIfClassIsMissing(lib.getLibraryName(), lib.getClassName())) {
Log.info("NovaCore", "Loaded library " + lib.getLibraryName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
package net.zeeraa.novacore.spigot.librarymanagement;

import javax.annotation.Nullable;

public class LibraryEntry {
private String className;
private String libraryName;
private String className;
private String libraryName;
@Nullable
private LibraryLoadCondition loadCondition;

public LibraryEntry(String className, String libraryName) {
this(className, libraryName, null);
}

public LibraryEntry(String className, String libraryName, @Nullable LibraryLoadCondition loadCondition) {
this.className = className;
this.libraryName = libraryName;
this.loadCondition = loadCondition;
}

public LibraryEntry(String className, String libraryName) {
this.className = className;
this.libraryName = libraryName;
}
public String getClassName() {
return className;
}

public String getClassName() {
return className;
}
public String getLibraryName() {
return libraryName;
}

public String getLibraryName() {
return libraryName;
}
@Nullable
public LibraryLoadCondition getLoadCondition() {
return loadCondition;
}

public boolean shouldLoad() {
if(loadCondition != null) {
return loadCondition.shouldLoad();
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.zeeraa.novacore.spigot.librarymanagement;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class LibraryLoadCondition {
private List<Supplier<Boolean>> dontLoadIf;

public LibraryLoadCondition() {
dontLoadIf = new ArrayList<>();
}

public void addPreventLoadCondition(Supplier<Boolean> condition) {
this.dontLoadIf.add(condition);
}

public boolean shouldLoad() {
if (dontLoadIf.stream().anyMatch(s -> s.get())) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

Expand Down Expand Up @@ -37,8 +38,8 @@
public class ChestLootManager extends NovaModule implements Listener {
private static ChestLootManager instance;

private ArrayList<Location> chests;
private HashMap<Location, Inventory> enderChests;
private List<Location> chests;
private Map<Location, EnderchestData> enderChests;

private BlockFace chestBlockFaces[] = { BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH };

Expand All @@ -51,6 +52,8 @@ public class ChestLootManager extends NovaModule implements Listener {

private boolean clearOldItems;

private boolean paused;

private Random random;

public static ChestLootManager getInstance() {
Expand All @@ -60,7 +63,7 @@ public static ChestLootManager getInstance() {
public ChestLootManager() {
super("NovaCore.ChestLootManager");
ChestLootManager.instance = this;
this.enderChests = new HashMap<Location, Inventory>();
this.enderChests = new HashMap<Location, EnderchestData>();
this.chests = new ArrayList<Location>();
this.chestLootTable = null;
this.enderChestLootTable = null;
Expand All @@ -69,6 +72,7 @@ public ChestLootManager() {
this.worlds = new ArrayList<>();
this.clearOldItems = true;
this.random = new Random();
this.paused = false;
}

public Random getRandom() {
Expand Down Expand Up @@ -128,7 +132,7 @@ public void refillChests(boolean announce) {
ChestRefillEvent event = new ChestRefillEvent(announce);
Bukkit.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
enderChests.clear();
enderChests.values().forEach(EnderchestData::setRefillReady);
chests.clear();
if (event.isShowMessage()) {
Bukkit.getOnlinePlayers().forEach(player -> {
Expand All @@ -155,6 +159,14 @@ public void setEnderChestLootTable(String enderChestLootTable) {
this.enderChestLootTable = enderChestLootTable;
}

public boolean isPaused() {
return paused;
}

public void setPaused(boolean paused) {
this.paused = paused;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent e) {
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
Expand All @@ -171,7 +183,9 @@ public void onPlayerInteract(PlayerInteractEvent e) {
}

if (e.getClickedBlock().getType() == Material.CHEST || e.getClickedBlock().getType() == Material.TRAPPED_CHEST) {
fillChest(e.getClickedBlock(), clearOldItems);
if (!paused) {
fillChest(e.getClickedBlock(), clearOldItems);
}
} else if (e.getClickedBlock().getType() == Material.ENDER_CHEST) {
if (enderChestLootTable != null) {
e.setCancelled(true);
Expand All @@ -180,53 +194,62 @@ public void onPlayerInteract(PlayerInteractEvent e) {
if (e.getClickedBlock() != null) {
Log.trace("Filling ender chest at location " + e.getClickedBlock().getLocation().toString());

if (!enderChests.containsKey(e.getClickedBlock().getLocation())) {
Inventory inventory = Bukkit.createInventory(new EnderChestHolder(), 27, "Ender chest");
Location location = e.getClickedBlock().getLocation();

LootTable lootTable = NovaCore.getInstance().getLootTableManager().getLootTable(enderChestLootTable);
Inventory inventory;
if (!enderChests.containsKey(location)) {
inventory = Bukkit.createInventory(new EnderChestHolder(), 27, "Ender chest");
enderChests.put(location, new EnderchestData(inventory, false));
}

if (lootTable == null) {
Log.warn("Missing loot table " + enderChestLootTable);
return;
}
EnderchestData chestData = enderChests.get(location);
inventory = chestData.getInventory();
if (!paused) {
if (!chestData.isHasFilled()) {
LootTable lootTable = NovaCore.getInstance().getLootTableManager().getLootTable(enderChestLootTable);

ChestFillEvent event = new ChestFillEvent(e.getClickedBlock(), lootTable, ChestType.ENDERCHEST, clearOldItems);
if (lootTable == null) {
Log.warn("Missing loot table " + enderChestLootTable);
} else {
chestData.setHasFilled(true);
ChestFillEvent event = new ChestFillEvent(e.getClickedBlock(), lootTable, ChestType.ENDERCHEST, clearOldItems);
Bukkit.getServer().getPluginManager().callEvent(event);

Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}

if (event.isCancelled()) {
return;
}
if (event.hasLootTableChanged()) {
lootTable = event.getLootTable();
}

if (event.hasLootTableChanged()) {
lootTable = event.getLootTable();
}
if (event.isClearOldItems()) {
inventory.clear();
}

if (event.isClearOldItems()) {
inventory.clear();
}
List<ItemStack> loot = lootTable.generateLoot();

List<ItemStack> loot = lootTable.generateLoot();
List<Integer> slots = getAvailableSlots(inventory);

List<Integer> slots = getAvailableSlots(inventory);
while (loot.size() > slots.size()) {
loot.remove(0);
}

while (loot.size() > slots.size()) {
loot.remove(0);
}
Collections.shuffle(slots, random);

Collections.shuffle(slots, random);
while (loot.size() > 0) {
int slot = slots.remove(0);
ItemStack item = loot.remove(0);

while (loot.size() > 0) {
int slot = slots.remove(0);
ItemStack item = loot.remove(0);
inventory.setItem(slot, item);
}

inventory.setItem(slot, item);
chestData.setHasFilled(true);
}
}

enderChests.put(e.getClickedBlock().getLocation(), inventory);
}

p.openInventory(enderChests.get(e.getClickedBlock().getLocation()));
p.openInventory(inventory);
}
}
}
Expand Down Expand Up @@ -309,4 +332,34 @@ private void fillChest(Block block, boolean clearOldItems) {
}
}
}
}

class EnderchestData {
private Inventory inventory;
private boolean hasFilled;

public EnderchestData(Inventory inventory, boolean hasFilled) {
this.inventory = inventory;
this.hasFilled = hasFilled;
}

public Inventory getInventory() {
return inventory;
}

public void setInventory(Inventory inventory) {
this.inventory = inventory;
}

public void setRefillReady() {
this.setHasFilled(false);
}

public boolean isHasFilled() {
return hasFilled;
}

public void setHasFilled(boolean hasFilled) {
this.hasFilled = hasFilled;
}
}

0 comments on commit a277f10

Please sign in to comment.