Skip to content

Commit

Permalink
Fixed VoxelBrushCommand assigning brush before event (#107)
Browse files Browse the repository at this point in the history
* Fixed VoxelBrushCommand assigning brush before event

* Removed unnecessary permission check

* Moved permission check, removed unnecessary code

* Bump version

* Update VoxelSniperCore/src/main/java/com/github/kevindagame/snipe/Sniper.java

Co-authored-by: Lennart99 <le.le@live.nl>

Co-authored-by: Lennart99 <le.le@live.nl>
  • Loading branch information
KevinDaGame and Lennart99 authored Jan 13, 2023
1 parent d9d9b36 commit 64ec7f7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public boolean doCommand(IPlayer player, final String[] args) {
snipeData.sendMessage(Messages.BRUSH_HANDLE_NOT_FOUND.replace("%arg%", args[0]));
} else {
IBrush oldBrush = sniper.getBrush(currentToolId);
IBrush newBrush = sniper.getBrush(currentToolId);
IBrush newBrush = sniper.instantiateBrush(brush);

if (newBrush == null) {
snipeData.sendMessage(Messages.VOXEL_BRUSH_NO_PERMISSION);
Expand All @@ -99,10 +99,9 @@ public boolean doCommand(IPlayer player, final String[] args) {
} else {
newBrush.parseParameters(args[0], additionalParameters, snipeData);
}
return true;
}
if (!new PlayerBrushChangedEvent(player, currentToolId, oldBrush, newBrush).callEvent().isCancelled()) {
sniper.setBrush(currentToolId, brush);
sniper.setBrush(currentToolId, newBrush);
sniper.displayInfo();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@
import com.github.kevindagame.util.VoxelMessage;
import com.github.kevindagame.voxelsniper.material.VoxelMaterial;
import com.google.common.base.Preconditions;
import com.google.common.collect.*;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableBiMap;
import org.jetbrains.annotations.NotNull;

/**
* @author ervinnnc
*/
public class SnipeTool {

private final BiMap<SnipeAction, VoxelMaterial> actionTools = HashBiMap.create();
Class<? extends IBrush> currentBrush;
IBrush currentBrush;
VoxelMessage messageHelper;
ClassToInstanceMap<IBrush> brushes = MutableClassToInstanceMap.create();

SnipeData snipeData;
private Class<? extends IBrush> previousBrush;
private IBrush previousBrush;

protected SnipeTool(Sniper owner) {
this(SnipeBrush.class, new SnipeData(owner));
this(owner.instantiateBrush(SnipeBrush.class), new SnipeData(owner));
}

protected SnipeTool(Class<? extends IBrush> currentBrush, SnipeData snipeData) {
protected SnipeTool(IBrush brush, SnipeData snipeData) {
this.snipeData = snipeData;
messageHelper = new VoxelMessage(snipeData);
snipeData.setVoxelMessage(messageHelper);

IBrush newBrushInstance = instanciateBrush(currentBrush);
if (snipeData.owner().getPlayer().hasPermission(newBrushInstance.getPermissionNode()) || snipeData.owner().getPlayer().hasPermission("voxelsniper.brush.*")) {
brushes.put(currentBrush, newBrushInstance);
this.currentBrush = currentBrush;
if (snipeData.owner().getPlayer().hasPermission(brush.getPermissionNode())) {
this.currentBrush = brush;
}
}

public IBrush getCurrentBrush() {
if (currentBrush == null) {
return null;
}
return brushes.getInstance(currentBrush);
return currentBrush;
}

public SnipeData getSnipeData() {
Expand All @@ -65,25 +65,10 @@ public void unassignAction(VoxelMaterial itemInHand) {
actionTools.inverse().remove(itemInHand);
}

public IBrush setCurrentBrush(Class<? extends IBrush> brush) {
Preconditions.checkNotNull(brush, "Can't set brush to null.");
IBrush brushInstance = brushes.get(brush);
if (brushInstance == null) {
brushInstance = instanciateBrush(brush);
Preconditions.checkNotNull(brushInstance, "Could not instanciate brush class.");
if (snipeData.owner().getPlayer().hasPermission(brushInstance.getPermissionNode()) || snipeData.owner().getPlayer().hasPermission("voxelsniper.brush.*")) {
brushes.put(brush, brushInstance);
previousBrush = currentBrush;
currentBrush = brush;
return brushInstance;
}
}
if (snipeData.owner().getPlayer().hasPermission(brushInstance.getPermissionNode()) || snipeData.owner().getPlayer().hasPermission("voxelsniper.brush.*")) {
previousBrush = currentBrush;
currentBrush = brush;
return brushInstance;
}
return null;
public IBrush setCurrentBrush(@NotNull IBrush brush) {
previousBrush = currentBrush;
currentBrush = brush;
return brush;
}

public SnipeAction getActionAssigned(VoxelMaterial itemInHand) {
Expand All @@ -98,13 +83,6 @@ public BiMap<SnipeAction, VoxelMaterial> getActionTools() {
return ImmutableBiMap.copyOf(actionTools);
}

IBrush instanciateBrush(Class<? extends IBrush> brush) {
try {
return brush.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return null;
}
}

public void assignAction(SnipeAction action, VoxelMaterial itemInHand) {
actionTools.forcePut(action, itemInHand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private boolean handleSneakLeftClick(String toolId, SnipeData snipeData, SnipeAc
}


public IBrush setBrush(String toolId, Class<? extends IBrush> brush) {
public IBrush setBrush(String toolId, @NotNull IBrush brush) {
if (!tools.containsKey(toolId)) {
return null;
}
Expand Down Expand Up @@ -283,6 +283,17 @@ public final void sendMessage(final @NotNull ComponentLike message) {
this.getPlayer().sendMessage(message);
}

public IBrush instantiateBrush(Class<? extends IBrush> brush) {
try {
var brushInstance = brush.newInstance();
if(getPlayer().hasPermission(brushInstance.getPermissionNode()))
return brushInstance;
} catch (InstantiationException | IllegalAccessException e) {
return null;
}
return null;
}

public enum Action {
LEFT_CLICK_BLOCK,
RIGHT_CLICK_BLOCK,
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/voxel-core.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ java {
}

group = "com.github.kevindagame"
version = "8.4.0"
version = "8.4.1"
//java.sourceCompatibility = JavaVersion.VERSION_16

tasks.withType<JavaCompile> {
Expand Down

0 comments on commit 64ec7f7

Please sign in to comment.