Always call plot events through the event dispatcher

This commit is contained in:
SirYwell 2022-01-28 08:13:15 +01:00
parent 126aa53b61
commit 03aa60b05d
5 changed files with 26 additions and 12 deletions

View File

@ -126,7 +126,7 @@ public class Done extends SubCommand {
long flagValue = System.currentTimeMillis() / 1000; long flagValue = System.currentTimeMillis() / 1000;
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(DoneFlag.class) PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(DoneFlag.class)
.createFlagInstance(Long.toString(flagValue)); .createFlagInstance(Long.toString(flagValue));
PlotFlagAddEvent event = new PlotFlagAddEvent(plotFlag, plot); PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied")); player.sendMessage(TranslatableCaption.of("events.event_denied"));
return; return;

View File

@ -25,6 +25,7 @@
*/ */
package com.plotsquared.core.command; package com.plotsquared.core.command;
import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.CaptionUtility; import com.plotsquared.core.configuration.caption.CaptionUtility;
@ -44,6 +45,7 @@ import com.plotsquared.core.plot.flag.InternalFlag;
import com.plotsquared.core.plot.flag.PlotFlag; import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.types.IntegerFlag; import com.plotsquared.core.plot.flag.types.IntegerFlag;
import com.plotsquared.core.plot.flag.types.ListFlag; import com.plotsquared.core.plot.flag.types.ListFlag;
import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.StringComparison; import com.plotsquared.core.util.StringComparison;
@ -79,8 +81,12 @@ import java.util.stream.Stream;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public final class FlagCommand extends Command { public final class FlagCommand extends Command {
public FlagCommand() { private final EventDispatcher eventDispatcher;
@Inject
public FlagCommand(final @NonNull EventDispatcher eventDispatcher) {
super(MainCommand.getInstance(), true); super(MainCommand.getInstance(), true);
this.eventDispatcher = eventDispatcher;
} }
private static boolean sendMessage(PlotPlayer<?> player) { private static boolean sendMessage(PlotPlayer<?> player) {
@ -335,7 +341,7 @@ public final class FlagCommand extends Command {
return; return;
} }
Plot plot = player.getLocation().getPlotAbs(); Plot plot = player.getLocation().getPlotAbs();
PlotFlagAddEvent event = new PlotFlagAddEvent(plotFlag, plot); PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
player.sendMessage( player.sendMessage(
TranslatableCaption.of("events.event_denied"), TranslatableCaption.of("events.event_denied"),
@ -394,7 +400,7 @@ public final class FlagCommand extends Command {
return; return;
} }
Plot plot = player.getLocation().getPlotAbs(); Plot plot = player.getLocation().getPlotAbs();
PlotFlagAddEvent event = new PlotFlagAddEvent(plotFlag, plot); PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
player.sendMessage( player.sendMessage(
TranslatableCaption.of("events.event_denied"), TranslatableCaption.of("events.event_denied"),
@ -463,7 +469,7 @@ public final class FlagCommand extends Command {
return; return;
} }
final Plot plot = player.getLocation().getPlotAbs(); final Plot plot = player.getLocation().getPlotAbs();
PlotFlagRemoveEvent event = new PlotFlagRemoveEvent(flag, plot); PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(flag, plot);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
player.sendMessage( player.sendMessage(
TranslatableCaption.of("events.event_denied"), TranslatableCaption.of("events.event_denied"),
@ -516,7 +522,7 @@ public final class FlagCommand extends Command {
} }
} else { } else {
PlotFlag<?, ?> plotFlag = parsedFlag.createFlagInstance(list); PlotFlag<?, ?> plotFlag = parsedFlag.createFlagInstance(list);
PlotFlagAddEvent addEvent = new PlotFlagAddEvent(plotFlag, plot); PlotFlagAddEvent addEvent = eventDispatcher.callFlagAdd(plotFlag, plot);
if (addEvent.getEventResult() == Result.DENY) { if (addEvent.getEventResult() == Result.DENY) {
player.sendMessage( player.sendMessage(
TranslatableCaption.of("events.event_denied"), TranslatableCaption.of("events.event_denied"),

View File

@ -38,10 +38,12 @@ import com.plotsquared.core.plot.PlotInventory;
import com.plotsquared.core.plot.PlotItemStack; import com.plotsquared.core.plot.PlotItemStack;
import com.plotsquared.core.plot.flag.PlotFlag; import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.implementations.MusicFlag; import com.plotsquared.core.plot.flag.implementations.MusicFlag;
import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.InventoryUtil; import com.plotsquared.core.util.InventoryUtil;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.item.ItemTypes;
import net.kyori.adventure.text.minimessage.Template; import net.kyori.adventure.text.minimessage.Template;
import org.checkerframework.checker.nullness.qual.NonNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Arrays; import java.util.Arrays;
@ -63,10 +65,12 @@ public class Music extends SubCommand {
); );
private final InventoryUtil inventoryUtil; private final InventoryUtil inventoryUtil;
private final EventDispatcher eventDispatcher;
@Inject @Inject
public Music(final @Nullable InventoryUtil inventoryUtil) { public Music(final @Nullable InventoryUtil inventoryUtil, final @NonNull EventDispatcher eventDispatcher) {
this.inventoryUtil = inventoryUtil; this.inventoryUtil = inventoryUtil;
this.eventDispatcher = eventDispatcher;
} }
@Override @Override
@ -104,7 +108,7 @@ public class Music extends SubCommand {
if (item.getType() == ItemTypes.BEDROCK) { if (item.getType() == ItemTypes.BEDROCK) {
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
.createFlagInstance(item.getType()); .createFlagInstance(item.getType());
PlotFlagRemoveEvent event = new PlotFlagRemoveEvent(plotFlag, plot); PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
getPlayer().sendMessage( getPlayer().sendMessage(
TranslatableCaption.of("events.event_denied"), TranslatableCaption.of("events.event_denied"),
@ -121,7 +125,7 @@ public class Music extends SubCommand {
} else if (item.getName().toLowerCase(Locale.ENGLISH).contains("disc")) { } else if (item.getName().toLowerCase(Locale.ENGLISH).contains("disc")) {
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
.createFlagInstance(item.getType()); .createFlagInstance(item.getType());
PlotFlagAddEvent event = new PlotFlagAddEvent(plotFlag, plot); PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
getPlayer().sendMessage( getPlayer().sendMessage(
TranslatableCaption.of("events.event_denied"), TranslatableCaption.of("events.event_denied"),

View File

@ -45,6 +45,7 @@ import com.plotsquared.core.queue.ChunkQueueCoordinator;
import com.plotsquared.core.queue.GlobalBlockQueue; import com.plotsquared.core.queue.GlobalBlockQueue;
import com.plotsquared.core.queue.QueueCoordinator; import com.plotsquared.core.queue.QueueCoordinator;
import com.plotsquared.core.util.ChunkManager; import com.plotsquared.core.util.ChunkManager;
import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.RegionManager; import com.plotsquared.core.util.RegionManager;
import com.plotsquared.core.util.RegionUtil; import com.plotsquared.core.util.RegionUtil;
@ -93,6 +94,7 @@ public class HybridUtils {
private final GlobalBlockQueue blockQueue; private final GlobalBlockQueue blockQueue;
private final WorldUtil worldUtil; private final WorldUtil worldUtil;
private final SchematicHandler schematicHandler; private final SchematicHandler schematicHandler;
private final EventDispatcher eventDispatcher;
@Inject @Inject
public HybridUtils( public HybridUtils(
@ -100,13 +102,15 @@ public class HybridUtils {
final @NonNull ChunkManager chunkManager, final @NonNull ChunkManager chunkManager,
final @NonNull GlobalBlockQueue blockQueue, final @NonNull GlobalBlockQueue blockQueue,
final @NonNull WorldUtil worldUtil, final @NonNull WorldUtil worldUtil,
final @NonNull SchematicHandler schematicHandler final @NonNull SchematicHandler schematicHandler,
final @NonNull EventDispatcher eventDispatcher
) { ) {
this.plotAreaManager = plotAreaManager; this.plotAreaManager = plotAreaManager;
this.chunkManager = chunkManager; this.chunkManager = chunkManager;
this.blockQueue = blockQueue; this.blockQueue = blockQueue;
this.worldUtil = worldUtil; this.worldUtil = worldUtil;
this.schematicHandler = schematicHandler; this.schematicHandler = schematicHandler;
this.eventDispatcher = eventDispatcher;
} }
public void regeneratePlotWalls(final PlotArea area) { public void regeneratePlotWalls(final PlotArea area) {
@ -348,7 +352,7 @@ public class HybridUtils {
result.add(whenDone.value.variety_sd); result.add(whenDone.value.variety_sd);
PlotFlag<?, ?> plotFlag = GlobalFlagContainer.getInstance().getFlag(AnalysisFlag.class).createFlagInstance( PlotFlag<?, ?> plotFlag = GlobalFlagContainer.getInstance().getFlag(AnalysisFlag.class).createFlagInstance(
result); result);
PlotFlagAddEvent event = new PlotFlagAddEvent(plotFlag, origin); PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, origin);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
return; return;
} }

View File

@ -359,7 +359,7 @@ public class ExpireManager {
.getFlag(AnalysisFlag.class) .getFlag(AnalysisFlag.class)
.createFlagInstance(changed.asList()); .createFlagInstance(changed.asList());
PlotFlagAddEvent event = PlotFlagAddEvent event =
new PlotFlagAddEvent(plotFlag, newPlot); eventDispatcher.callFlagAdd(plotFlag, plot);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
return; return;
} }