From a32c4e4fa2f5764e4d6fdf87f9653c6b9bba7b74 Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Fri, 16 Aug 2024 06:43:48 +0200 Subject: [PATCH] Remove strong reference from match to world Signed-off-by: Pablo Herrera --- .../tc/oc/pgm/loot/LootableMatchModule.java | 148 ++++++++---------- .../java/tc/oc/pgm/loot/WorldTickClock.java | 10 +- .../main/java/tc/oc/pgm/match/MatchImpl.java | 2 +- 3 files changed, 68 insertions(+), 92 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/loot/LootableMatchModule.java b/core/src/main/java/tc/oc/pgm/loot/LootableMatchModule.java index 3e2abf7d25..3f0736bc8b 100644 --- a/core/src/main/java/tc/oc/pgm/loot/LootableMatchModule.java +++ b/core/src/main/java/tc/oc/pgm/loot/LootableMatchModule.java @@ -57,18 +57,16 @@ public LootableMatchModule( this.fillers = fillers; this.match = match; this.caches = caches; - this.filledAt = new InstantMap<>(new WorldTickClock(match.getWorld())); + this.filledAt = new InstantMap<>(match.getClock()); this.immm = match.getModule(ItemModifyMatchModule.class); final FilterMatchModule fmm = match.needModule(FilterMatchModule.class); - fillers.forEach( - filler -> { - fmm.onRise( - Match.class, - filler.getRefillTrigger(), - m -> this.filledAt.keySet().removeIf(f -> filler.equals(f.getRight()))); - }); + fillers.forEach(filler -> { + fmm.onRise(Match.class, filler.getRefillTrigger(), m -> this.filledAt + .keySet() + .removeIf(f -> filler.equals(f.getRight()))); + }); } /** @@ -78,9 +76,10 @@ public LootableMatchModule( private static @Nullable Predicate filterPredicate(InventoryHolder holder) { if (holder instanceof DoubleChest) { final DoubleChest doubleChest = (DoubleChest) holder; - return filter -> - !filter.query(new BlockQuery((Chest) doubleChest.getLeftSide())).isDenied() - || !filter.query(new BlockQuery((Chest) doubleChest.getRightSide())).isDenied(); + return filter -> !filter + .query(new BlockQuery((Chest) doubleChest.getLeftSide())) + .isDenied() + || !filter.query(new BlockQuery((Chest) doubleChest.getRightSide())).isDenied(); } else if (holder instanceof BlockState) { return filter -> !filter.query(new BlockQuery((BlockState) holder)).isDenied(); } else if (holder instanceof Player) { @@ -104,25 +103,17 @@ public void onInventoryOpen(InventoryOpenEvent event) { final Predicate filterPredicate = filterPredicate(inventory.getHolder()); if (filterPredicate == null) return; - logger.fine( - () -> - opener.getNameLegacy() - + " opened a " - + inventory.getHolder().getClass().getSimpleName()); + logger.fine(() -> + opener.getNameLegacy() + " opened a " + inventory.getHolder().getClass().getSimpleName()); // Find all Fillers that apply to the holder of the opened inventory - final List fillers = - this.fillers.stream() - .filter(filler -> filterPredicate.test(filler.getFilter())) - .collect(Collectors.toList()); + final List fillers = this.fillers.stream() + .filter(filler -> filterPredicate.test(filler.getFilter())) + .collect(Collectors.toList()); if (fillers.isEmpty()) return; - logger.fine( - () -> - "Found fillers " - + fillers.stream() - .map(FillerDefinition::toString) - .collect(Collectors.joining(", "))); + logger.fine(() -> "Found fillers " + + fillers.stream().map(FillerDefinition::toString).collect(Collectors.joining(", "))); // Find all Caches that the opened inventory is part of final List fillables = new ArrayList<>(); @@ -145,67 +136,54 @@ abstract class Fillable { void fill(MatchPlayer opener, List fillers) { // Build a short list of Fillers that are NOT cooling down from a previous fill - final List coolFillers = - fillers.stream() - .filter( - filler -> - filledAt.putUnlessNewer(new Pair<>(this, filler), filler.getRefillInterval()) - == null) - .collect(Collectors.toList()); + final List coolFillers = fillers.stream() + .filter(filler -> + filledAt.putUnlessNewer(new Pair<>(this, filler), filler.getRefillInterval()) == null) + .collect(Collectors.toList()); // Find all the Inventories for this Fillable, and build a map of Fillers to the subset // of Inventories that they are allowed to fill, based on the filter of each Filler. // Note how duplicate inventories are skipped. final SetMultimap fillerInventories = HashMultimap.create(); - inventories() - .distinct() - .forEach( - inventory -> { - final Predicate passes = filterPredicate(inventory.getHolder()); - if (passes == null) return; - for (FillerDefinition filler : coolFillers) { - if (passes.test(filler.getFilter())) { - fillerInventories.put(filler, inventory); - } - } - }); - - fillerInventories - .asMap() - .forEach( - (filler, inventories) -> { - if (filler.cleanBeforeRefill()) { - inventories().forEach(Inventory::clear); - } - }); + inventories().distinct().forEach(inventory -> { + final Predicate passes = filterPredicate(inventory.getHolder()); + if (passes == null) return; + for (FillerDefinition filler : coolFillers) { + if (passes.test(filler.getFilter())) { + fillerInventories.put(filler, inventory); + } + } + }); + + fillerInventories.asMap().forEach((filler, inventories) -> { + if (filler.cleanBeforeRefill()) { + inventories().forEach(Inventory::clear); + } + }); final Random random = new Random(); - fillerInventories - .asMap() - .forEach( - (filler, inventories) -> { - // For each Filler, build a mutable list of slots that it can fill. - // (27 is the standard size for single chests) - final List slots = new ArrayList<>(inventories.size() * 27); - for (Inventory inv : inventories) { - for (int index = 0; index < inv.getSize(); index++) { - if (inv.getItem(index) == null) { - slots.add(new InventorySlot(index, inv)); - } - } - } - - filler - .getLoot() - .elements(opener) - .limit(slots.size()) - .peek( - i -> { - if (immm != null) immm.applyRules(i); - }) - .forEachOrdered(i -> slots.remove(random.nextInt(slots.size())).putItem(i)); - }); + fillerInventories.asMap().forEach((filler, inventories) -> { + // For each Filler, build a mutable list of slots that it can fill. + // (27 is the standard size for single chests) + final List slots = new ArrayList<>(inventories.size() * 27); + for (Inventory inv : inventories) { + for (int index = 0; index < inv.getSize(); index++) { + if (inv.getItem(index) == null) { + slots.add(new InventorySlot(index, inv)); + } + } + } + + filler + .getLoot() + .elements(opener) + .limit(slots.size()) + .peek(i -> { + if (immm != null) immm.applyRules(i); + }) + .forEachOrdered(i -> slots.remove(random.nextInt(slots.size())).putItem(i)); + }); } } @@ -256,13 +234,11 @@ Stream inventories() { .region() .getChunkPositions() .map(cp -> cp.getChunk(match.getWorld())) - .flatMap( - ch -> - Stream.concat( - Stream.of(ch.getTileEntities()).map(BlockQuery::new), - Stream.of(ch.getEntities()) - .filter(e -> !(e instanceof Player)) - .map(EntityQuery::new))) + .flatMap(ch -> Stream.concat( + Stream.of(ch.getTileEntities()).map(BlockQuery::new), + Stream.of(ch.getEntities()) + .filter(e -> !(e instanceof Player)) + .map(EntityQuery::new))) .filter(q -> cache.jointFilter().query(q).isAllowed()) .map(InventoryQuery::getInventory) .filter(Objects::nonNull); diff --git a/core/src/main/java/tc/oc/pgm/loot/WorldTickClock.java b/core/src/main/java/tc/oc/pgm/loot/WorldTickClock.java index 7bffd72d12..4fa332bd27 100644 --- a/core/src/main/java/tc/oc/pgm/loot/WorldTickClock.java +++ b/core/src/main/java/tc/oc/pgm/loot/WorldTickClock.java @@ -5,7 +5,7 @@ import java.time.Clock; import java.time.Instant; import java.time.ZoneId; -import org.bukkit.World; +import tc.oc.pgm.api.match.Match; import tc.oc.pgm.api.time.Tick; import tc.oc.pgm.util.collection.InstantMap; @@ -18,11 +18,11 @@ */ public class WorldTickClock extends Clock { - private final World world; + private final Match match; private Tick tick; - public WorldTickClock(World world) { - this.world = world; + public WorldTickClock(Match match) { + this.match = match; } @Override @@ -45,7 +45,7 @@ public Tick getTick() { } private Tick now() { - long tick = NMS_HACKS.getMonotonicTime(this.world); + long tick = NMS_HACKS.getMonotonicTime(match.getWorld()); if (this.tick == null || tick != this.tick.tick) { this.tick = new Tick(tick, Instant.now()); } diff --git a/core/src/main/java/tc/oc/pgm/match/MatchImpl.java b/core/src/main/java/tc/oc/pgm/match/MatchImpl.java index 254a951302..193674d8da 100644 --- a/core/src/main/java/tc/oc/pgm/match/MatchImpl.java +++ b/core/src/main/java/tc/oc/pgm/match/MatchImpl.java @@ -124,7 +124,7 @@ protected MatchImpl(String id, MapContext map, World world) { this.world = new WeakReference<>(assertNotNull(world)); this.matchModules = new ConcurrentHashMap<>(); - this.clock = new WorldTickClock(world); + this.clock = new WorldTickClock(this); this.logger = ClassLogger.get(PGM.get().getLogger(), getClass()); this.random = new Random(); this.tickRandoms = new HashMap<>();