Skip to content

Commit

Permalink
Scaffolding no longer get broken when a torch is ontop (#12), gravity…
Browse files Browse the repository at this point in the history
… blocks can not fall anymore when a torch is ontop
  • Loading branch information
TobiasDeBruijn committed Jun 20, 2021
1 parent 0cf2f4d commit d59567a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/main/java/dev/array21/harotorch/HaroTorch.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void run() {
Bukkit.getPluginManager().registerEvents(new PlayerInteractEventListener(), this);
Bukkit.getPluginManager().registerEvents(new BlockPhysicsEventListener(), this);
Bukkit.getPluginManager().registerEvents(new BlockBurnEventListener(), this);
Bukkit.getPluginManager().registerEvents(new BlockFadeEventListener(), this);

//Commands
this.getCommand("torch").setExecutor(new TorchCommandExecutor(this));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.array21.harotorch.events;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockFadeEvent;

public class BlockFadeEventListener implements Listener {

@EventHandler
public void onBlockFadeEvent(BlockFadeEvent event) {
//We wont break a scaffolding block when a torch is ontop of it
if(event.getBlock().getType() == Material.SCAFFOLDING) {
Location l = event.getBlock().getLocation();
if(!Common.checkSurroundings(l)) {
event.setCancelled(true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
package dev.array21.harotorch.events;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
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 dev.array21.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;

// We dont allow opening a trapdoor when a torch is ontop of it
if(event.getBlock().getType().data == TrapDoor.class) {
Openable blockState = (Openable) event.getBlock().getBlockData();
if(blockState.isOpen()) {
if(!Common.checkSurroundings(event.getBlock().getLocation())) {
blockState.setOpen(false);
event.getBlock().setBlockData(blockState);
}
}
}

//We wont break a scaffolding block when a torch is ontop of it
if(event.getBlock().getType() == Material.SCAFFOLDING) {
Location l = event.getBlock().getLocation();
if(!Common.checkSurroundings(l)) {
Block b = event.getBlock();
}
}

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);
//We don't want gravity blocks to fall when they (could) have a torch attached/ontop
if(event.getBlock().getType().hasGravity()) {
if(!Common.checkSurroundings(event.getBlock().getLocation())) {
event.setCancelled(true);
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/dev/array21/harotorch/events/Common.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.array21.harotorch.events;

import org.bukkit.Location;

import dev.array21.harotorch.torch.TorchHandler;

public class Common {

/**
* Check if a Location has any torches surrounding it
* @param loc
* @return Returns true if it does not, false if it does.
*/
public static boolean checkSurroundings(Location loc) {
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)) {
return false;
}

return true;
}
}

0 comments on commit d59567a

Please sign in to comment.