Skip to content

Commit

Permalink
feat: Move ExpireManager to Guice by removing legacy IMP references (#…
Browse files Browse the repository at this point in the history
…3845)

* feat: Move ExpireManager to Guice by removing legacy IMP references

* Mark ExpireManager IMP as deprecated and add comments

* Add import for PlotPlatform for function reference

* Add ExpireManager instance call and optimize performance
  • Loading branch information
michizhou authored Oct 17, 2022
1 parent 0ae8fc4 commit b8b3098
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 33 deletions.
11 changes: 11 additions & 0 deletions Core/src/main/java/com/plotsquared/core/PlotPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.plotsquared.core.location.World;
import com.plotsquared.core.permissions.PermissionHandler;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.expiration.ExpireManager;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.queue.GlobalBlockQueue;
import com.plotsquared.core.util.ChunkManager;
Expand Down Expand Up @@ -284,6 +285,16 @@ public interface PlotPlatform<P> extends LocaleHolder {
return injector().getInstance(ChunkManager.class);
}

/**
* Get the {@link ExpireManager} implementation for the platform
*
* @return Expire manager
* @since TODO
*/
default @NonNull ExpireManager expireManager() {
return injector().getInstance(ExpireManager.class);
}

/**
* Get the {@link PlotAreaManager} implementation.
*
Expand Down
9 changes: 5 additions & 4 deletions Core/src/main/java/com/plotsquared/core/PlotSquared.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ public void loadCaptionMap() throws Exception {

public void startExpiryTasks() {
if (Settings.Enabled_Components.PLOT_EXPIRY) {
ExpireManager.IMP = new ExpireManager(this.eventDispatcher);
ExpireManager.IMP.runAutomatedTask();
ExpireManager expireManager = PlotSquared.platform().expireManager();
expireManager.runAutomatedTask();
for (Settings.Auto_Clear settings : Settings.AUTO_CLEAR.getInstances()) {
ExpiryTask task = new ExpiryTask(settings, this.getPlotAreaManager());
ExpireManager.IMP.addTask(task);
expireManager.addTask(task);
}
}
}
Expand Down Expand Up @@ -645,7 +645,8 @@ private void sortPlotsByHash(final @NonNull Plot @NonNull [] input) {
} else {
list = new ArrayList<>(input);
}
list.sort(Comparator.comparingLong(a -> ExpireManager.IMP.getTimestamp(a.getOwnerAbs())));
ExpireManager expireManager = PlotSquared.platform().expireManager();
list.sort(Comparator.comparingLong(a -> expireManager.getTimestamp(a.getOwnerAbs())));
return list;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.plotsquared.core.command;

import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.caption.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.events.PlotFlagRemoveEvent;
Expand Down Expand Up @@ -139,18 +140,16 @@ public void run(PlotAnalysis value) {
return true;
}
case "start-expire" -> {
if (ExpireManager.IMP == null) {
ExpireManager.IMP = new ExpireManager(this.eventDispatcher);
}
if (ExpireManager.IMP.runAutomatedTask()) {
ExpireManager expireManager = PlotSquared.platform().expireManager() == null ? new ExpireManager(this.eventDispatcher) : PlotSquared.platform().expireManager();
if (expireManager.runAutomatedTask()) {
player.sendMessage(TranslatableCaption.of("debugexec.expiry_started"));
} else {
player.sendMessage(TranslatableCaption.of("debugexec.expiry_already_started"));
}
return true;
}
case "stop-expire" -> {
if (ExpireManager.IMP == null || !ExpireManager.IMP.cancelTask()) {
if (PlotSquared.platform().expireManager() == null || !PlotSquared.platform().expireManager().cancelTask()) {
player.sendMessage(TranslatableCaption.of("debugexec.task_halted"));
} else {
player.sendMessage(TranslatableCaption.of("debugexec.task_cancelled"));
Expand Down
4 changes: 2 additions & 2 deletions Core/src/main/java/com/plotsquared/core/command/Done.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.plotsquared.core.command;

import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.events.PlotDoneEvent;
Expand All @@ -29,7 +30,6 @@
import com.plotsquared.core.permissions.Permission;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.expiration.ExpireManager;
import com.plotsquared.core.plot.expiration.PlotAnalysis;
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.implementations.DoneFlag;
Expand Down Expand Up @@ -94,7 +94,7 @@ public boolean onCommand(final PlotPlayer<?> player, String[] args) {
Template.of("plot", plot.getId().toString())
);
final Settings.Auto_Clear doneRequirements = Settings.AUTO_CLEAR.get("done");
if (ExpireManager.IMP == null || doneRequirements == null) {
if (PlotSquared.platform().expireManager() == null || doneRequirements == null) {
finish(plot, player, true);
plot.removeRunning();
} else {
Expand Down
3 changes: 1 addition & 2 deletions Core/src/main/java/com/plotsquared/core/command/ListCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.expiration.ExpireManager;
import com.plotsquared.core.plot.flag.implementations.DoneFlag;
import com.plotsquared.core.plot.flag.implementations.PriceFlag;
import com.plotsquared.core.plot.flag.implementations.ServerPlotFlag;
Expand Down Expand Up @@ -240,7 +239,7 @@ public boolean onCommand(PlotPlayer<?> player, String[] args) {
);
return false;
}
if (ExpireManager.IMP == null) {
if (PlotSquared.platform().expireManager() == null) {
plotConsumer.accept(PlotQuery.newQuery().noPlots());
} else {
plotConsumer.accept(PlotQuery.newQuery().expiredPlots());
Expand Down
5 changes: 2 additions & 3 deletions Core/src/main/java/com/plotsquared/core/command/Trim.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.plotsquared.core.location.Location;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.expiration.ExpireManager;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.queue.GlobalBlockQueue;
import com.plotsquared.core.queue.QueueCoordinator;
Expand Down Expand Up @@ -92,8 +91,8 @@ public static boolean getTrimRegions(
}
TranslatableCaption.of("trim.trim_starting");
final List<Plot> plots = PlotQuery.newQuery().inWorld(world).asList();
if (ExpireManager.IMP != null) {
plots.removeAll(ExpireManager.IMP.getPendingExpired());
if (PlotSquared.platform().expireManager() != null) {
plots.removeAll(PlotSquared.platform().expireManager().getPendingExpired());
}
result.value1 = new HashSet<>(PlotSquared.platform().worldUtil().getChunkChunks(world));
result.value2 = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.plotsquared.core.plot.PlotTitle;
import com.plotsquared.core.plot.PlotWeather;
import com.plotsquared.core.plot.comment.CommentManager;
import com.plotsquared.core.plot.expiration.ExpireManager;
import com.plotsquared.core.plot.flag.GlobalFlagContainer;
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.implementations.DenyExitFlag;
Expand Down Expand Up @@ -163,8 +162,8 @@ public boolean plotEntry(final PlotPlayer<?> player, final Plot plot) {
if ((last != null) && !last.getId().equals(plot.getId())) {
plotExit(player, last);
}
if (ExpireManager.IMP != null) {
ExpireManager.IMP.handleEntry(player, plot);
if (PlotSquared.platform().expireManager() != null) {
PlotSquared.platform().expireManager().handleEntry(player, plot);
}
lastPlot.set(plot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import com.plotsquared.core.plot.PlotCluster;
import com.plotsquared.core.plot.PlotId;
import com.plotsquared.core.plot.PlotWeather;
import com.plotsquared.core.plot.expiration.ExpireManager;
import com.plotsquared.core.plot.flag.implementations.DoneFlag;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.plot.world.SinglePlotArea;
Expand Down Expand Up @@ -618,8 +617,8 @@ public void unregister() {
LOGGER.info("Plot {} was deleted + cleared due to {} getting banned", owned.getId(), getName());
}
}
if (ExpireManager.IMP != null) {
ExpireManager.IMP.storeDate(getUUID(), System.currentTimeMillis());
if (PlotSquared.platform().expireManager() != null) {
PlotSquared.platform().expireManager().storeDate(getUUID(), System.currentTimeMillis());
}
PlotSquared.platform().playerManager().removePlayer(this);
PlotSquared.platform().unregister(this);
Expand Down
9 changes: 4 additions & 5 deletions Core/src/main/java/com/plotsquared/core/plot/Plot.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.plotsquared.core.permissions.Permission;
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.expiration.ExpireManager;
import com.plotsquared.core.plot.expiration.PlotAnalysis;
import com.plotsquared.core.plot.flag.FlagContainer;
import com.plotsquared.core.plot.flag.GlobalFlagContainer;
Expand Down Expand Up @@ -1105,8 +1104,8 @@ public PlotAnalysis getComplexity(Settings.Auto_Clear settings) {
* @return A boolean indicating whether or not the operation succeeded
*/
public <V> boolean setFlag(final @NonNull PlotFlag<V, ?> flag) {
if (flag instanceof KeepFlag && ExpireManager.IMP != null) {
ExpireManager.IMP.updateExpired(this);
if (flag instanceof KeepFlag && PlotSquared.platform().expireManager() != null) {
PlotSquared.platform().expireManager().updateExpired(this);
}
for (final Plot plot : this.getConnectedPlots()) {
plot.getFlagContainer().addFlag(flag);
Expand Down Expand Up @@ -2831,11 +2830,11 @@ public CompletableFuture<Caption> format(final Caption iInfo, PlotPlayer<?> play
Component members = PlayerManager.getPlayerList(this.getMembers(), player);
Component denied = PlayerManager.getPlayerList(this.getDenied(), player);
String seen;
if (Settings.Enabled_Components.PLOT_EXPIRY && ExpireManager.IMP != null) {
if (Settings.Enabled_Components.PLOT_EXPIRY && PlotSquared.platform().expireManager() != null) {
if (this.isOnline()) {
seen = TranslatableCaption.of("info.now").getComponent(player);
} else {
int time = (int) (ExpireManager.IMP.getAge(this, false) / 1000);
int time = (int) (PlotSquared.platform().expireManager().getAge(this, false) / 1000);
if (time != 0) {
seen = TimeUtil.secToTime(time);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.plotsquared.core.plot.expiration;

import com.plotsquared.core.PlotPlatform;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.caption.Caption;
import com.plotsquared.core.configuration.caption.Templates;
Expand Down Expand Up @@ -60,6 +61,10 @@

public class ExpireManager {

/**
* @deprecated Use {@link PlotPlatform#expireManager()} instead
*/
@Deprecated(forRemoval = true, since = "TODO")
public static ExpireManager IMP;
private final ConcurrentHashMap<UUID, Long> dates_cache;
private final ConcurrentHashMap<UUID, Long> account_age_cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.plotsquared.core.plot.expiration;

import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
Expand Down Expand Up @@ -72,8 +73,9 @@ public boolean applies(PlotArea area) {
min = false;
diff = plots.size() - settings.REQUIRED_PLOTS;
}
ExpireManager expireManager = PlotSquared.platform().expireManager();
List<Long> entireList =
plots.stream().map(plot -> ExpireManager.IMP.getAge(plot, settings.DELETE_IF_OWNER_IS_UNKNOWN))
plots.stream().map(plot -> expireManager.getAge(plot, settings.DELETE_IF_OWNER_IS_UNKNOWN))
.collect(Collectors.toList());
List<Long> top = new ArrayList<>(diff + 1);
if (diff > 1000) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.common.eventbus.EventBus;
import com.intellectualsites.annotations.DoNotUse;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.events.PlayerAutoPlotEvent;
Expand Down Expand Up @@ -59,7 +60,6 @@
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.PlotId;
import com.plotsquared.core.plot.Rating;
import com.plotsquared.core.plot.expiration.ExpireManager;
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.implementations.DeviceInteractFlag;
import com.plotsquared.core.plot.flag.implementations.MiscPlaceFlag;
Expand Down Expand Up @@ -300,8 +300,8 @@ public void doJoinTask(final PlotPlayer<?> player) {
if (player == null) {
return; //possible future warning message to figure out where we are retrieving null
}
if (ExpireManager.IMP != null) {
ExpireManager.IMP.handleJoin(player);
if (PlotSquared.platform().expireManager() != null) {
PlotSquared.platform().expireManager().handleJoin(player);
}
if (this.worldEdit != null) {
if (player.getAttribute("worldedit")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
*/
package com.plotsquared.core.util.query;

import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.expiration.ExpireManager;

import java.util.Collection;

class ExpiredPlotProvider implements PlotProvider {

@Override
public Collection<Plot> getPlots() {
return ExpireManager.IMP.getPendingExpired();
return PlotSquared.platform().expireManager().getPendingExpired();
}

}

0 comments on commit b8b3098

Please sign in to comment.