diff --git a/VoxelSniperCore/src/main/java/com/github/kevindagame/command/VoxelBrushCommand.java b/VoxelSniperCore/src/main/java/com/github/kevindagame/command/VoxelBrushCommand.java index fda942a1..642a99a7 100644 --- a/VoxelSniperCore/src/main/java/com/github/kevindagame/command/VoxelBrushCommand.java +++ b/VoxelSniperCore/src/main/java/com/github/kevindagame/command/VoxelBrushCommand.java @@ -58,7 +58,10 @@ public boolean doCommand(IPlayer player, final String[] args) { try { int originalSize = snipeData.getBrushSize(); - if (!new PlayerBrushSizeChangedEvent(player, currentToolId, sniper.getBrush(currentToolId), originalSize, snipeData.getBrushSize()).callEvent().isCancelled()) { + var brush = sniper.getBrush(currentToolId); + if (brush == null) return false; + + if (!new PlayerBrushSizeChangedEvent(player, currentToolId, brush, originalSize, snipeData.getBrushSize()).callEvent().isCancelled()) { snipeData.setBrushSize(Integer.parseInt(args[0])); snipeData.getVoxelMessage().size(); return true; diff --git a/VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/SnipeTool.java b/VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/SnipeTool.java index 92e54b27..d44afdc7 100644 --- a/VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/SnipeTool.java +++ b/VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/SnipeTool.java @@ -4,11 +4,11 @@ import com.github.kevindagame.brush.SnipeBrush; import com.github.kevindagame.util.VoxelMessage; import com.github.kevindagame.voxelsniper.material.VoxelMaterial; -import com.google.common.base.Preconditions; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableBiMap; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author ervinnnc @@ -16,26 +16,25 @@ public class SnipeTool { private final BiMap actionTools = HashBiMap.create(); - IBrush currentBrush; - VoxelMessage messageHelper; - - SnipeData snipeData; - private IBrush previousBrush; + private IBrush currentBrush = null; + private IBrush previousBrush = null; + private final VoxelMessage messageHelper; + private final SnipeData snipeData; protected SnipeTool(Sniper owner) { this(owner.instantiateBrush(SnipeBrush.class), new SnipeData(owner)); } - protected SnipeTool(IBrush brush, SnipeData snipeData) { + protected SnipeTool(@Nullable IBrush brush, SnipeData snipeData) { this.snipeData = snipeData; messageHelper = new VoxelMessage(snipeData); snipeData.setVoxelMessage(messageHelper); - if (snipeData.owner().getPlayer().hasPermission(brush.getPermissionNode())) { + if (brush != null && snipeData.owner().getPlayer().hasPermission(brush.getPermissionNode())) { // npe brush.getPermissionNode() this.currentBrush = brush; } } - public IBrush getCurrentBrush() { + public @Nullable IBrush getCurrentBrush() { if (currentBrush == null) { return null; } @@ -54,7 +53,11 @@ public VoxelMessage getMessageHelper() { return messageHelper; } - public IBrush previousBrush() { + /** + * Sets the current Brush to the previous Brush, and then returns the new Brush + * @return The new Brush, or null if there is no previous Brush + */ + public @Nullable IBrush previousBrush() { if (previousBrush == null) { return null; } @@ -65,7 +68,7 @@ public void unassignAction(VoxelMaterial itemInHand) { actionTools.inverse().remove(itemInHand); } - public IBrush setCurrentBrush(@NotNull IBrush brush) { + public @NotNull IBrush setCurrentBrush(@NotNull IBrush brush) { previousBrush = currentBrush; currentBrush = brush; return brush; diff --git a/VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/Sniper.java b/VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/Sniper.java index 8af51056..8b2969e0 100644 --- a/VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/Sniper.java +++ b/VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/Sniper.java @@ -14,6 +14,7 @@ import com.google.common.collect.Maps; import net.kyori.adventure.text.ComponentLike; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.LinkedList; import java.util.Map; @@ -123,11 +124,15 @@ private boolean handleRightClick(IBlock clickedBlock, SnipeTool sniperTool, Snip } } - if (sniperTool.getCurrentBrush() instanceof PerformerBrush performerBrush) { + var brush = sniperTool.getCurrentBrush(); + + if (brush == null) return true; + + if (brush instanceof PerformerBrush performerBrush) { performerBrush.initP(snipeData); } - return sniperTool.getCurrentBrush().perform(snipeAction, snipeData, targetBlock, lastBlock); + return brush.perform(snipeAction, snipeData, targetBlock, lastBlock); } private boolean handleSneakLeftClick(String toolId, SnipeData snipeData, SnipeAction snipeAction, IBlock targetBlock) { @@ -156,7 +161,7 @@ public IBrush setBrush(String toolId, @NotNull IBrush brush) { return tools.get(toolId).setCurrentBrush(brush); } - public IBrush getBrush(String toolId) { + public @Nullable IBrush getBrush(String toolId) { if (!tools.containsKey(toolId)) { return null; } @@ -164,7 +169,11 @@ public IBrush getBrush(String toolId) { return tools.get(toolId).getCurrentBrush(); } - public IBrush previousBrush(String toolId) { + /** + * For the given toolID, sets the current Brush to the previous Brush, and then returns the new Brush + * @return The new Brush, or null if there is no previous Brush + */ + public @Nullable IBrush previousBrush(String toolId) { if (!tools.containsKey(toolId)) { return null; } @@ -283,11 +292,11 @@ public final void sendMessage(final @NotNull ComponentLike message) { this.getPlayer().sendMessage(message); } - public IBrush instantiateBrush(Class brush) { + public @Nullable IBrush instantiateBrush(Class brush) { try { var brushInstance = brush.newInstance(); if(getPlayer().hasPermission(brushInstance.getPermissionNode())) - return brushInstance; + return brushInstance; } catch (InstantiationException | IllegalAccessException e) { return null; } diff --git a/VoxelSniperCore/src/main/java/com/github/kevindagame/voxelsniper/events/player/PlayerBrushChangedEvent.java b/VoxelSniperCore/src/main/java/com/github/kevindagame/voxelsniper/events/player/PlayerBrushChangedEvent.java index 665a1712..5d26a120 100644 --- a/VoxelSniperCore/src/main/java/com/github/kevindagame/voxelsniper/events/player/PlayerBrushChangedEvent.java +++ b/VoxelSniperCore/src/main/java/com/github/kevindagame/voxelsniper/events/player/PlayerBrushChangedEvent.java @@ -8,15 +8,18 @@ import java.util.function.Consumer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + public class PlayerBrushChangedEvent extends PlayerEvent implements Cancellable { private static final HandlerList handlers = new HandlerList<>(); - private final IBrush oldBrush; - private final IBrush newBrush; + private final @Nullable IBrush oldBrush; + private final @NotNull IBrush newBrush; private final String toolId; private EventResult status; - public PlayerBrushChangedEvent(IPlayer p, String toolId, IBrush oldBrush, IBrush newBrush) { + public PlayerBrushChangedEvent(IPlayer p, String toolId, @Nullable IBrush oldBrush, @NotNull IBrush newBrush) { super(p); this.oldBrush = oldBrush; this.newBrush = newBrush; @@ -31,10 +34,12 @@ public static void registerListener(EventPriority priority, Consumer implements Cancellable { private static final HandlerList handlers = new HandlerList<>(); private final int oldSize; private final int newSize; private final String toolId; - private final IBrush brush; + private final @NotNull IBrush brush; private EventResult status; - public PlayerBrushSizeChangedEvent(IPlayer p, String toolId, IBrush brush, int oldSize, int newSize) { + public PlayerBrushSizeChangedEvent(IPlayer p, String toolId, @NotNull IBrush brush, int oldSize, int newSize) { super(p); this.oldSize = oldSize; this.newSize = newSize; @@ -45,6 +47,7 @@ public String getToolId() { return toolId; } + @NotNull public IBrush getBrush() { return brush; }