Skip to content

Commit

Permalink
#112: Fixed NPE when joining server without voxelsniper permissions (…
Browse files Browse the repository at this point in the history
…still needs to be validated) (#113)
  • Loading branch information
Lennart99 authored Jan 19, 2023
1 parent c65a4f1 commit da8b7ea
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,37 @@
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
*/
public class SnipeTool {

private final BiMap<SnipeAction, VoxelMaterial> 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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -156,15 +161,19 @@ 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;
}

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;
}
Expand Down Expand Up @@ -283,11 +292,11 @@ public final void sendMessage(final @NotNull ComponentLike message) {
this.getPlayer().sendMessage(message);
}

public IBrush instantiateBrush(Class<? extends IBrush> brush) {
public @Nullable IBrush instantiateBrush(Class<? extends IBrush> brush) {
try {
var brushInstance = brush.newInstance();
if(getPlayer().hasPermission(brushInstance.getPermissionNode()))
return brushInstance;
return brushInstance;
} catch (InstantiationException | IllegalAccessException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@

import java.util.function.Consumer;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlayerBrushChangedEvent extends PlayerEvent<PlayerBrushChangedEvent> implements Cancellable {

private static final HandlerList<PlayerBrushChangedEvent> 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;
Expand All @@ -31,10 +34,12 @@ public static void registerListener(EventPriority priority, Consumer<PlayerBrush
handlers.registerListener(priority, handler);
}

@Nullable
public IBrush getOldBrush() {
return oldBrush;
}

@NotNull
public IBrush getNewBrush() {
return newBrush;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import java.util.function.Consumer;

import org.jetbrains.annotations.NotNull;

public class PlayerBrushSizeChangedEvent extends PlayerEvent<PlayerBrushSizeChangedEvent> implements Cancellable {

private static final HandlerList<PlayerBrushSizeChangedEvent> 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;
Expand Down Expand Up @@ -45,6 +47,7 @@ public String getToolId() {
return toolId;
}

@NotNull
public IBrush getBrush() {
return brush;
}
Expand Down

0 comments on commit da8b7ea

Please sign in to comment.