Skip to content

Commit

Permalink
Fixed #11 & Other fixes
Browse files Browse the repository at this point in the history
- Fixed #8 using PlayerInteractEvent for regular trapdoor interactions and BlockPhysicsEvent for redstone trapdoor interactions
- Burnable blocks with a torch attached/ontop can no longer burn
- When a block is broken, and a torch is attached to it on the sides, it can now no longer be broken. However this causes an interesting issue, mostly due to my lazyness, I only check the blocks on all 4 sides, but I dont check if the torch is actually dependent on the block being broken. We'll see if people complain
  • Loading branch information
TobiasDeBruijn committed Jun 4, 2021
1 parent dcd32f0 commit 9148e26
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/main/java/nl/thedutchmc/harotorch/HaroTorch.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public void run() {
Bukkit.getPluginManager().registerEvents(new EntityExplodeEventListener(), this);
Bukkit.getPluginManager().registerEvents(new BlockPistonExtendEventListener(), this);
Bukkit.getPluginManager().registerEvents(new BlockPistonRetractEventListener(), this);
Bukkit.getPluginManager().registerEvents(new PlayerInteractEventListener(), this);
Bukkit.getPluginManager().registerEvents(new BlockPhysicsEventListener(), this);
Bukkit.getPluginManager().registerEvents(new BlockBurnEventListener(), this);


//Commands
this.getCommand("torch").setExecutor(new TorchCommandExecutor(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,22 @@ public class BlockBreakEventListener implements Listener {
public void onBlockBreakEvent(BlockBreakEvent event) {

Location loc = event.getBlock().getLocation();

Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ());
Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ());
Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ());
Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1);
Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1);

if(TorchHandler.isTorch(loc_y_plus_1)) {
if(
(
TorchHandler.isTorch(loc_y_plus_1)
|| TorchHandler.isTorch(loc_x_plus_1)
|| TorchHandler.isTorch(loc_x_minus_1)
|| TorchHandler.isTorch(loc_z_plus_1)
|| TorchHandler.isTorch(loc_z_minus_1)
)
&& !TorchHandler.isTorch(loc)) {

event.getPlayer().sendMessage(HaroTorch.getMessagePrefix() + ChatColor.GOLD + LangHandler.activeLang.getLangMessages().get("blockBreakNotAllowedTorchOntop"));
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nl.thedutchmc.harotorch.events;

import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBurnEvent;

import nl.thedutchmc.harotorch.torch.TorchHandler;

public class BlockBurnEventListener implements Listener {

@EventHandler
public void onBlockBurnEvent(BlockBurnEvent event) {
Location loc = event.getBlock().getLocation();

Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ());
Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ());
Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ());
Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1);
Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1);

if(TorchHandler.isTorch(loc_y_plus_1)
|| TorchHandler.isTorch(loc_x_plus_1)
|| TorchHandler.isTorch(loc_x_minus_1)
|| TorchHandler.isTorch(loc_z_plus_1)
|| TorchHandler.isTorch(loc_z_minus_1)) {

event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ public void onBlockExplodeEvent(BlockExplodeEvent event) {
continue;
}


//Check if the block above the block being exploded is a torch
Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ());
if(TorchHandler.isTorch(loc_y_plus_1)) {
Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ());
Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ());
Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1);
Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1);
if(TorchHandler.isTorch(loc_y_plus_1)
|| TorchHandler.isTorch(loc_x_plus_1)
|| TorchHandler.isTorch(loc_x_minus_1)
|| TorchHandler.isTorch(loc_z_plus_1)
|| TorchHandler.isTorch(loc_z_minus_1)) {

event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package nl.thedutchmc.harotorch.events;

import org.bukkit.Location;
import org.bukkit.block.data.Openable;
import org.bukkit.block.data.type.TrapDoor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockPhysicsEvent;

import nl.thedutchmc.harotorch.torch.TorchHandler;

import org.bukkit.event.Listener;

public class BlockPhysicsEventListener implements Listener {

@EventHandler
public void onBlockPhysicsEvent(BlockPhysicsEvent event) {
if(event.getBlock().getType().data != TrapDoor.class) {
return;
}

Openable blockState = (Openable) event.getBlock().getBlockData();
if(blockState.isOpen()) {
Location loc = event.getBlock().getLocation();

Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ());
Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ());
Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ());
Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1);
Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1);

if(TorchHandler.isTorch(loc_y_plus_1)
|| TorchHandler.isTorch(loc_x_plus_1)
|| TorchHandler.isTorch(loc_x_minus_1)
|| TorchHandler.isTorch(loc_z_plus_1)
|| TorchHandler.isTorch(loc_z_minus_1)) {

blockState.setOpen(false);
event.getBlock().setBlockData(blockState);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ public class BlockPistonExtendEventListener implements Listener {
public void onBlockPistonExtendEvent(BlockPistonExtendEvent event) {
for(Block b : event.getBlocks()) {
//The block being moved
Location movedBlockLocation = b.getLocation();
Location loc = b.getLocation();

//The block above the block being moved. If this is a torch we don't want to move the block below that torch, as it'd break the torch.
Location movedBlockLocationAbove = b.getLocation();
movedBlockLocationAbove.setY(b.getLocation().getY() +1);
Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ());
Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ());
Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ());
Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1);
Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1);

if(TorchHandler.isTorch(movedBlockLocation)) {
if(TorchHandler.isTorch(loc)) {
event.setCancelled(true);
}

if(TorchHandler.isTorch(movedBlockLocationAbove)) {
if(TorchHandler.isTorch(loc_y_plus_1)
|| TorchHandler.isTorch(loc_x_plus_1)
|| TorchHandler.isTorch(loc_x_minus_1)
|| TorchHandler.isTorch(loc_z_plus_1)
|| TorchHandler.isTorch(loc_z_minus_1)) {

event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ public class BlockPistonRetractEventListener implements Listener {
public void onBlockPistonRetractEvent(BlockPistonRetractEvent event) {
for(Block b : event.getBlocks()) {
//The block being moved
Location movedBlockLocation = b.getLocation();
Location loc = b.getLocation();

//The block above the block being moved. If this is a torch we don't want to move the block below that torch, as it'd break the torch.
Location movedBlockLocationAbove = b.getLocation();
movedBlockLocationAbove.setY(b.getLocation().getY() +1);
Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ());
Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ());
Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ());
Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1);
Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1);

if(TorchHandler.isTorch(movedBlockLocation)) {
if(TorchHandler.isTorch(loc)) {
event.setCancelled(true);
}

if(TorchHandler.isTorch(movedBlockLocationAbove)) {
if(TorchHandler.isTorch(loc_y_plus_1)
|| TorchHandler.isTorch(loc_x_plus_1)
|| TorchHandler.isTorch(loc_x_minus_1)
|| TorchHandler.isTorch(loc_z_plus_1)
|| TorchHandler.isTorch(loc_z_minus_1)) {
event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ public void onEntityExplodeEvent(EntityExplodeEvent event) {

//Check if the block above the block being exploded is a torch
Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ());
if(TorchHandler.isTorch(loc_y_plus_1)) {
Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ());
Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ());
Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1);
Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1);

if(TorchHandler.isTorch(loc_y_plus_1)
|| TorchHandler.isTorch(loc_x_plus_1)
|| TorchHandler.isTorch(loc_x_minus_1)
|| TorchHandler.isTorch(loc_z_plus_1)
|| TorchHandler.isTorch(loc_z_minus_1)) {
blocksDontRemove.add(b);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nl.thedutchmc.harotorch.events;

import org.bukkit.Location;
import org.bukkit.block.data.type.TrapDoor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;

import nl.thedutchmc.harotorch.torch.TorchHandler;


public class PlayerInteractEventListener implements Listener {

@EventHandler
public void onPlayerInteractEvent(PlayerInteractEvent event) {
if(event.getClickedBlock() == null) {
return;
}

if(event.getClickedBlock().getType().data == TrapDoor.class) {
Location clickedBlockLoc = event.getClickedBlock().getLocation();
Location possibleTorchLoc = new Location(clickedBlockLoc.getWorld(), clickedBlockLoc.getX(), clickedBlockLoc.getY() + 1d, clickedBlockLoc.getZ());

if(TorchHandler.isTorch(possibleTorchLoc)) {
event.setCancelled(true);
}
}
}
}

0 comments on commit 9148e26

Please sign in to comment.