SteamWar/BauSystem2.0
Archiviert
12
0

Improve SmartPlaceListener

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-08-29 17:23:02 +02:00
Ursprung b143df939a
Commit 688270d311

Datei anzeigen

@ -22,14 +22,19 @@ package de.steamwar.bausystem.features.smartplace;
import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional; import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Rotatable; import org.bukkit.block.data.Rotatable;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
@Linked(LinkageType.LISTENER) @Linked(LinkageType.LISTENER)
public class SmartPlaceListener implements Listener { public class SmartPlaceListener implements Listener {
@ -52,4 +57,49 @@ public class SmartPlaceListener implements Listener {
block.setBlockData(blockData); block.setBlockData(blockData);
} }
} }
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
if (event.getPlayer().isSneaking()) return;
if (!event.getClickedBlock().getType().isInteractable()) return;
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
if (event.getItem() == null) return;
if (!event.getMaterial().isBlock()) return;
try {
BlockData blockData = event.getItem().getType().createBlockData();
if (!(blockData instanceof Directional) && !(blockData instanceof Rotatable)) {
return;
}
} catch (Exception e) {
return;
}
switch (event.getMaterial()) {
case REPEATER:
case COMPARATOR:
return;
default:
break;
}
event.setUseInteractedBlock(Event.Result.DENY);
World world = event.getPlayer().getWorld();
Block block = world.getBlockAt(event.getClickedBlock().getX() + event.getBlockFace().getModX(), event.getClickedBlock().getY() + event.getBlockFace().getModY(), event.getClickedBlock().getZ() + event.getBlockFace().getModZ());
block.setType(event.getMaterial());
BlockData blockData = event.getItem().getType().createBlockData();
BlockFace blockFace = event.getBlockFace();
if (block.getType() == Material.HOPPER) {
blockFace = blockFace.getOppositeFace();
}
if (blockData instanceof Directional) {
Directional directional = (Directional) blockData;
if (directional.getFaces().contains(blockFace)) {
directional.setFacing(blockFace);
}
} else if (blockData instanceof Rotatable) {
((Rotatable) blockData).setRotation(blockFace);
}
block.setBlockData(blockData);
}
} }