Skip to content

Commit

Permalink
fix(blocks): change order of block placement actions to fix protection (
Browse files Browse the repository at this point in the history
#177)

Currently, protection plugin checks are called on block place before we check if a custom block is being placed. We should only cancel the block place event if we are sure the block belongs to our plugin.

This also removes the check if canceled and instead passes it to the event handler.
  • Loading branch information
brettsaunders21 authored Jan 6, 2022
1 parent c1ca6d4 commit 29de3bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@
<dependency>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough-protection</artifactId>
<version>1.0.3</version>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough-common</artifactId>
<version>1.0.3</version>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>de.robotricker</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,42 @@ public void onWorldInit(WorldInitEvent e) {
}
}

@EventHandler
@EventHandler(ignoreCancelled = true)
public void onPlace(BlockPlaceEvent e) {
// Check the item placed is a custom block
if (!CustomItemManager.isCustomBlockItem(e.getItemInHand())) return;

// Ensure we have valid placement data for the block
NBTItem nbtItem = new NBTItem(e.getItemInHand());
final String customBlockItemName = CustomItemManager.getCustomItemName(nbtItem);
if (!customBlockDataHashMap.containsKey(customBlockItemName)) return;

// Check player protection permissions
OfflinePlayer player = Bukkit.getOfflinePlayer(e.getPlayer().getUniqueId());
if (!Craftory.protectionManager.hasPermission(player, e.getBlockPlaced(), Interaction.PLACE_BLOCK)) {
e.setCancelled(true);
return;
}
if (!CustomItemManager.isCustomBlockItem(e.getItemInHand())) {
return;
}
NBTItem nbtItem = new NBTItem(e.getItemInHand());
final String customBlockItemName = CustomItemManager.getCustomItemName(nbtItem);
if (!customBlockDataHashMap.containsKey(customBlockItemName)) {
return;
}
if (!e.isCancelled()) {
//If Basic Block
if (Utilities.getBasicBlockRegistry().containsKey(customBlockItemName)) {
customBlockManager.placeBasicCustomBlock(customBlockItemName, e.getBlockPlaced());
} else {
CustomBlock customBlock = customBlockManager
.placeCustomBlock(customBlockItemName, e.getBlockPlaced(), e.getPlayer().getFacing(), e.getPlayer());
CustomBlockPlaceEvent customBlockPlaceEvent = new CustomBlockPlaceEvent(
e.getBlockPlaced().getLocation(), customBlockItemName, e.getBlockPlaced(), customBlock);
Bukkit.getPluginManager().callEvent(customBlockPlaceEvent);

//Give data
if (Boolean.TRUE.equals(nbtItem.hasKey("extraData"))) {
NBTCompound extraCompound = nbtItem.getCompound("extraData");
if (extraCompound.hasKey("energyStorage") && customBlock instanceof PoweredBlock) {
((PoweredBlock) customBlock).getEnergyStorage().setEnergyStored(extraCompound.getInteger("energyStorage"));
}
}
// If Basic Block (e.g. Ores)
if (Utilities.getBasicBlockRegistry().containsKey(customBlockItemName)) {
customBlockManager.placeBasicCustomBlock(customBlockItemName, e.getBlockPlaced());
} else {
// If normal Custom Block place it
CustomBlock customBlock = customBlockManager
.placeCustomBlock(customBlockItemName, e.getBlockPlaced(), e.getPlayer().getFacing(), e.getPlayer());
CustomBlockPlaceEvent customBlockPlaceEvent = new CustomBlockPlaceEvent(
e.getBlockPlaced().getLocation(), customBlockItemName, e.getBlockPlaced(), customBlock);
Bukkit.getPluginManager().callEvent(customBlockPlaceEvent);

// Provide custom block extra data from item NBT
if (Boolean.TRUE.equals(nbtItem.hasKey("extraData"))) {
NBTCompound extraCompound = nbtItem.getCompound("extraData");
if (extraCompound.hasKey("energyStorage") && customBlock instanceof PoweredBlock) {
((PoweredBlock) customBlock).getEnergyStorage().setEnergyStored(extraCompound.getInteger("energyStorage"));
}
}

}
}

Expand Down

0 comments on commit 29de3bd

Please sign in to comment.