From 13e882021b17065377ef263f238913975fe3be89 Mon Sep 17 00:00:00 2001 From: Stankman Date: Mon, 26 Jun 2023 20:26:44 -0500 Subject: [PATCH] Changed Periodical Wired Cycle to Room --- .../wired/interfaces/IWiredPeriodical.java | 12 +++ .../wired/triggers/WiredTriggerRepeater.java | 50 +++++------- .../triggers/WiredTriggerRepeaterLong.java | 52 +++++------- .../com/eu/habbo/habbohotel/rooms/Room.java | 81 ++++++++++++++++++- .../habbohotel/rooms/RoomSpecialTypes.java | 19 ++--- .../habbo/habbohotel/wired/WiredHandler.java | 4 +- 6 files changed, 138 insertions(+), 80 deletions(-) create mode 100644 src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/interfaces/IWiredPeriodical.java diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/interfaces/IWiredPeriodical.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/interfaces/IWiredPeriodical.java new file mode 100644 index 00000000..41a8ac98 --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/interfaces/IWiredPeriodical.java @@ -0,0 +1,12 @@ +package com.eu.habbo.habbohotel.items.interactions.wired.interfaces; + +import com.eu.habbo.habbohotel.rooms.RoomTile; + +public interface IWiredPeriodical { + int getInterval(); + void setInterval(int value); + boolean isTriggerTileUpdated(); + void setTriggerTileUpdated(boolean value); + RoomTile getOldTile(); + void setOldTile(RoomTile value); +} diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeater.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeater.java index f9ccd607..c8f4e1b9 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeater.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeater.java @@ -4,21 +4,33 @@ import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.items.ICycleable; import com.eu.habbo.habbohotel.items.Item; import com.eu.habbo.habbohotel.items.interactions.InteractionWiredTrigger; +import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.IWiredPeriodical; import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.WiredTriggerReset; import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.habbohotel.rooms.RoomTile; import com.eu.habbo.habbohotel.rooms.RoomUnit; import com.eu.habbo.habbohotel.wired.WiredHandler; import com.eu.habbo.habbohotel.wired.WiredTriggerType; +import lombok.Getter; +import lombok.Setter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -public class WiredTriggerRepeater extends InteractionWiredTrigger implements ICycleable, WiredTriggerReset { +public class WiredTriggerRepeater extends InteractionWiredTrigger implements IWiredPeriodical, WiredTriggerReset { public final int PARAM_REPEAT_TIME = 0; protected int counter = 0; + @Getter + @Setter + private boolean triggerTileUpdated; + @Getter + @Setter + private RoomTile oldTile; + @Setter + private int interval; + public WiredTriggerRepeater(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); } @@ -41,44 +53,20 @@ public class WiredTriggerRepeater extends InteractionWiredTrigger implements ICy @Override public void onMove(Room room, RoomTile oldLocation, RoomTile newLocation) { - if(room.getTriggersOnRoom().containsValue(this)) { - room.getTriggersOnRoom().remove(oldLocation.getX() + ";" + oldLocation.getY()); - } - + this.triggerTileUpdated = true; + this.oldTile = oldLocation; super.onMove(room, oldLocation, newLocation); } @Override public void onPickUp(Room room) { - if(room.getTriggersOnRoom().containsValue(this)) { - room.getTriggersOnRoom().remove(this.getX() + ";" + this.getY()); - } - + this.triggerTileUpdated = true; + this.oldTile = room.getLayout().getTile(this.getX(), this.getY()); super.onPickUp(room); } - @Override - public void cycle(Room room) { - String key = this.getX() + ";" + this.getY(); - - if(room.getTriggersOnRoom().containsKey(key)) { - if(room.getTriggersOnRoom().get(key).getId() != this.getId()) { - if(!(room.getTriggersOnRoom().get(key) instanceof WiredTriggerRepeaterLong) && room.getTriggersOnRoom().get(key).getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME) <= this.getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME)) { - return; - } - } - } - room.getTriggersOnRoom().put(key, this); - - this.counter += 500; - if (this.counter >= this.getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME) * 500) { - this.counter = 0; - if (this.getRoomId() != 0) { - if (room.isLoaded()) { - WiredHandler.handle(this, null, room, new Object[]{this}); - } - } - } + public int getInterval() { + return this.getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME) * 500; } @Override diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeaterLong.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeaterLong.java index 85b7a7d7..33ce55bb 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeaterLong.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeaterLong.java @@ -4,20 +4,30 @@ import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.items.ICycleable; import com.eu.habbo.habbohotel.items.Item; import com.eu.habbo.habbohotel.items.interactions.InteractionWiredTrigger; +import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.IWiredPeriodical; import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.WiredTriggerReset; import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.habbohotel.rooms.RoomTile; import com.eu.habbo.habbohotel.rooms.RoomUnit; import com.eu.habbo.habbohotel.wired.WiredHandler; import com.eu.habbo.habbohotel.wired.WiredTriggerType; +import lombok.Getter; +import lombok.Setter; import java.sql.ResultSet; import java.sql.SQLException; -public class WiredTriggerRepeaterLong extends InteractionWiredTrigger implements ICycleable, WiredTriggerReset { +public class WiredTriggerRepeaterLong extends InteractionWiredTrigger implements IWiredPeriodical, WiredTriggerReset { public final int PARAM_REPEAT_TIME = 0; - private int counter = 0; - + protected int counter = 0; + @Getter + @Setter + private boolean triggerTileUpdated; + @Getter + @Setter + private RoomTile oldTile; + @Setter + private int interval; public WiredTriggerRepeaterLong(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); } @@ -40,44 +50,20 @@ public class WiredTriggerRepeaterLong extends InteractionWiredTrigger implements @Override public void onMove(Room room, RoomTile oldLocation, RoomTile newLocation) { - if(room.getTriggersOnRoom().containsValue(this)) { - room.getTriggersOnRoom().remove(oldLocation.getX() + ";" + oldLocation.getY()); - } - + this.triggerTileUpdated = true; + this.oldTile = oldLocation; super.onMove(room, oldLocation, newLocation); } @Override public void onPickUp(Room room) { - if(room.getTriggersOnRoom().containsValue(this)) { - room.getTriggersOnRoom().remove(this.getX() + ";" + this.getY()); - } - + this.triggerTileUpdated = true; + this.oldTile = room.getLayout().getTile(this.getX(), this.getY()); super.onPickUp(room); } - @Override - public void cycle(Room room) { - String key = this.getX() + ";" + this.getY(); - - if(room.getTriggersOnRoom().containsKey(key)) { - if(room.getTriggersOnRoom().get(key).getId() != this.getId()) { - if(room.getTriggersOnRoom().get(key).getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME) <= this.getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME)) { - return; - } - } - } - room.getTriggersOnRoom().put(key, this); - - this.counter += 500; - if (this.counter >= this.getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME) * 5000) { - this.counter = 0; - if (this.getRoomId() != 0) { - if (room.isLoaded()) { - WiredHandler.handle(this, null, room, new Object[]{this}); - } - } - } + public int getInterval() { + return this.getWiredSettings().getIntegerParams().get(PARAM_REPEAT_TIME) * 500; } @Override diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java index c340743e..2356b437 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -22,6 +22,8 @@ import com.eu.habbo.habbohotel.items.interactions.games.tag.InteractionTagField; import com.eu.habbo.habbohotel.items.interactions.games.tag.InteractionTagPole; import com.eu.habbo.habbohotel.items.interactions.pets.*; import com.eu.habbo.habbohotel.items.interactions.wired.extra.WiredBlob; +import com.eu.habbo.habbohotel.items.interactions.wired.interfaces.IWiredPeriodical; +import com.eu.habbo.habbohotel.items.interactions.wired.triggers.WiredTriggerRepeaterLong; import com.eu.habbo.habbohotel.messenger.MessengerBuddy; import com.eu.habbo.habbohotel.permissions.Permission; import com.eu.habbo.habbohotel.pets.Pet; @@ -292,11 +294,18 @@ public class Room implements Comparable, ISerialize, Runnable { private RoomSpecialTypes roomSpecialTypes; @Getter private TraxManager traxManager; - @Getter - private HashMap triggersOnRoom; private boolean cycleOdd; @Getter private long cycleTimestamp; + @Getter + @Setter + private ScheduledFuture wiredPeriodicalCycle; + @Getter + @Setter + final HashMap triggersOnRoom; + @Getter + @Setter + private int periodicalTick; public Room(ResultSet set) throws SQLException { this.id = set.getInt("id"); @@ -383,7 +392,9 @@ public class Room implements Comparable, ISerialize, Runnable { this.activeTrades = new THashSet<>(0); this.rights = new TIntArrayList(); this.userVotes = new ArrayList<>(); + this.triggersOnRoom = new HashMap<>(); + this.periodicalTick = 0; } public synchronized void loadData() { @@ -410,7 +421,7 @@ public class Room implements Comparable, ISerialize, Runnable { this.loadBots(connection); this.loadPets(connection); this.loadWordFilter(connection); -// this.loadWiredData(connection); + this.startPeriodicalCycle(); this.idleCycles = 0; this.loaded = true; @@ -908,6 +919,10 @@ public class Room implements Comparable, ISerialize, Runnable { if (Emulator.getPluginManager().fireEvent(new RoomUnloadingEvent(this)).isCancelled()) return; + if(this.wiredPeriodicalCycle != null) { + this.wiredPeriodicalCycle.cancel(true); + } + if (this.loaded) { try { @@ -4356,4 +4371,64 @@ public class Room implements Comparable, ISerialize, Runnable { THashSet roomUnits = getRoomUnits(); return roomUnits.stream().filter(unit -> unit.getCurrentLocation() == tile).collect(Collectors.toSet()); } + + private void startPeriodicalCycle() { + this.wiredPeriodicalCycle = Emulator.getThreading().run(() -> { + if(Emulator.isShuttingDown) { + return; + } + + this.startPeriodicalCycle(); + + List periodicals = new ArrayList<>(); + + if(this.getRoomSpecialTypes().getTriggers(WiredTriggerType.PERIODICALLY) != null) { + periodicals.addAll(this.getRoomSpecialTypes().getTriggers(WiredTriggerType.PERIODICALLY)); + } + + if(this.getRoomSpecialTypes().getTriggers(WiredTriggerType.PERIODICALLY_LONG) != null) { + periodicals.addAll(this.getRoomSpecialTypes().getTriggers(WiredTriggerType.PERIODICALLY_LONG)); + } + + if(periodicals.isEmpty()) { + return; + } + + periodicals.parallelStream().forEach(trigger -> { + if(!(trigger instanceof IWiredPeriodical)) { + return; + } + + if(((IWiredPeriodical) trigger).isTriggerTileUpdated()) { + if(this.triggersOnRoom.containsKey(((IWiredPeriodical) trigger).getOldTile())) { + if(this.triggersOnRoom.get(((IWiredPeriodical) trigger).getOldTile()).getId() == trigger.getId() || this.triggersOnRoom.get(((IWiredPeriodical) trigger).getOldTile()) == null) { + this.triggersOnRoom.remove(((IWiredPeriodical) trigger).getOldTile()); + } + } + ((IWiredPeriodical) trigger).setTriggerTileUpdated(false); + ((IWiredPeriodical) trigger).setOldTile(null); + } + + RoomTile triggerTile = this.layout.getTile(trigger.getX(), trigger.getY()); + + if (this.triggersOnRoom.containsKey(triggerTile)) { + if (this.triggersOnRoom.get(triggerTile) == null || (this.triggersOnRoom.get(triggerTile).getId() != trigger.getId() && ((IWiredPeriodical) trigger).getInterval() <= ((IWiredPeriodical) this.triggersOnRoom.get(triggerTile)).getInterval())) { + this.triggersOnRoom.put(triggerTile, trigger); + } else if (this.triggersOnRoom.get(triggerTile).getId() != trigger.getId() && ((IWiredPeriodical) this.triggersOnRoom.get(triggerTile)).getInterval() <= ((IWiredPeriodical) trigger).getInterval()) { + return; + } + } else { + this.triggersOnRoom.put(triggerTile, trigger); + } + + if(this.periodicalTick % (((IWiredPeriodical) trigger).getInterval() / 500) != 0) { + return; + } + + WiredHandler.handle(trigger, null, this, new Object[]{trigger}); + }); + + ++this.periodicalTick; + }, 500L); + } } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomSpecialTypes.java b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomSpecialTypes.java index 3ce54325..c6bd6345 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomSpecialTypes.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomSpecialTypes.java @@ -27,10 +27,8 @@ import gnu.trove.map.hash.THashMap; import gnu.trove.set.hash.THashSet; import java.awt.*; -import java.util.ArrayList; -import java.util.Collections; +import java.util.*; import java.util.List; -import java.util.Map; public class RoomSpecialTypes { private final THashMap banzaiTeleporters; @@ -40,7 +38,7 @@ public class RoomSpecialTypes { private final THashMap petToys; private final THashMap rollers; - private final THashMap> wiredTriggers; + private final HashMap> wiredTriggers; private final THashMap> wiredEffects; private final THashMap> wiredConditions; private final THashMap wiredExtras; @@ -61,7 +59,7 @@ public class RoomSpecialTypes { this.petToys = new THashMap<>(0); this.rollers = new THashMap<>(0); - this.wiredTriggers = new THashMap<>(0); + this.wiredTriggers = new HashMap<>(0); this.wiredEffects = new THashMap<>(0); this.wiredConditions = new THashMap<>(0); this.wiredExtras = new THashMap<>(0); @@ -229,7 +227,7 @@ public class RoomSpecialTypes { public InteractionWiredTrigger getTrigger(int itemId) { synchronized (this.wiredTriggers) { - for (Map.Entry> map : this.wiredTriggers.entrySet()) { + for (Map.Entry> map : this.wiredTriggers.entrySet()) { for (InteractionWiredTrigger trigger : map.getValue()) { if (trigger.getId() == itemId) return trigger; @@ -244,7 +242,7 @@ public class RoomSpecialTypes { synchronized (this.wiredTriggers) { THashSet triggers = new THashSet<>(); - for (Map.Entry> map : this.wiredTriggers.entrySet()) { + for (Map.Entry> map : this.wiredTriggers.entrySet()) { triggers.addAll(map.getValue()); } @@ -252,7 +250,7 @@ public class RoomSpecialTypes { } } - public THashSet getTriggers(WiredTriggerType type) { + public List getTriggers(WiredTriggerType type) { return this.wiredTriggers.get(type); } @@ -260,7 +258,7 @@ public class RoomSpecialTypes { synchronized (this.wiredTriggers) { THashSet triggers = new THashSet<>(); - for (Map.Entry> map : this.wiredTriggers.entrySet()) { + for (Map.Entry> map : this.wiredTriggers.entrySet()) { for (InteractionWiredTrigger trigger : map.getValue()) { if (trigger.getX() == x && trigger.getY() == y) triggers.add(trigger); @@ -274,7 +272,7 @@ public class RoomSpecialTypes { public void addTrigger(InteractionWiredTrigger trigger) { synchronized (this.wiredTriggers) { if (!this.wiredTriggers.containsKey(trigger.getType())) - this.wiredTriggers.put(trigger.getType(), new THashSet<>()); + this.wiredTriggers.put(trigger.getType(), new ArrayList<>()); this.wiredTriggers.get(trigger.getType()).add(trigger); } @@ -354,7 +352,6 @@ public class RoomSpecialTypes { } } - public InteractionWiredCondition getCondition(int itemId) { synchronized (this.wiredConditions) { for (Map.Entry> map : this.wiredConditions.entrySet()) { diff --git a/src/main/java/com/eu/habbo/habbohotel/wired/WiredHandler.java b/src/main/java/com/eu/habbo/habbohotel/wired/WiredHandler.java index ba507ce1..28a7351f 100644 --- a/src/main/java/com/eu/habbo/habbohotel/wired/WiredHandler.java +++ b/src/main/java/com/eu/habbo/habbohotel/wired/WiredHandler.java @@ -51,7 +51,7 @@ public class WiredHandler { return false; } - THashSet triggers = room.getRoomSpecialTypes().getTriggers(triggerType); + List triggers = room.getRoomSpecialTypes().getTriggers(triggerType); if (triggers == null || triggers.isEmpty()) { return false; @@ -102,7 +102,7 @@ public class WiredHandler { if (room.getRoomSpecialTypes() == null) return false; - THashSet triggers = room.getRoomSpecialTypes().getTriggers(WiredTriggerType.CUSTOM); + List triggers = room.getRoomSpecialTypes().getTriggers(WiredTriggerType.CUSTOM); if (triggers == null || triggers.isEmpty()) return false;