Dateien nach "src/main/java/net/licks92/wirelessredstone/compat" hochladen
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package net.licks92.wirelessredstone.compat;
|
||||
|
||||
import net.licks92.wirelessredstone.WirelessRedstone;
|
||||
import net.licks92.wirelessredstone.materiallib.data.CrossMaterial;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.data.Directional;
|
||||
import org.bukkit.block.data.Powerable;
|
||||
import org.bukkit.block.data.Rotatable;
|
||||
import org.bukkit.block.data.type.RedstoneWallTorch;
|
||||
import org.bukkit.block.data.type.Switch;
|
||||
import org.bukkit.block.data.type.WallSign;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class InternalBlockData {
|
||||
public boolean isPowerable(@NotNull Block block) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
return block.getBlockData() instanceof org.bukkit.block.data.Powerable;
|
||||
}
|
||||
|
||||
public boolean isPowered(@NotNull Block block) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
if (!(block instanceof Powerable)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ((Powerable) block.getBlockData()).isPowered();
|
||||
}
|
||||
|
||||
public boolean isRedstoneSwitch(@NotNull Block block) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
return block.getBlockData() instanceof Switch;
|
||||
}
|
||||
|
||||
public BlockFace getSignRotation(@NotNull Block block) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
if (block.getBlockData() instanceof Rotatable) {
|
||||
Rotatable blockData = ((Rotatable) block.getBlockData());
|
||||
return blockData.getRotation();
|
||||
} else if (block.getBlockData() instanceof Directional) {
|
||||
Directional blockData = ((Directional) block.getBlockData());
|
||||
return blockData.getFacing();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Block needs to be a org.bukkit.block.data.Rotatable or org.bukkit.block.data.Directional found "
|
||||
+ block.getBlockData().getClass());
|
||||
}
|
||||
}
|
||||
|
||||
public BlockFace getDirectionalFacing(@NotNull Block block) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
if (!(block.getBlockData() instanceof Directional)) {
|
||||
throw new IllegalArgumentException("Block needs to be a org.bukkit.block.data.Directional found " + block.getBlockData().getClass());
|
||||
}
|
||||
|
||||
return ((Directional) block.getBlockData()).getFacing();
|
||||
}
|
||||
|
||||
public BlockFace getRedstoneSwitchFacing(@NotNull Block block) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
if (!(block.getBlockData() instanceof Switch)) {
|
||||
throw new IllegalArgumentException("Block needs to be a org.bukkit.block.data.type.Switch found " + block.getBlockData().getClass());
|
||||
}
|
||||
|
||||
Switch redstoneSwitch = (Switch) block.getBlockData();
|
||||
|
||||
if (redstoneSwitch.getFace() == Switch.Face.CEILING) {
|
||||
return BlockFace.UP;
|
||||
} else if (redstoneSwitch.getFace() == Switch.Face.FLOOR) {
|
||||
return BlockFace.DOWN;
|
||||
}
|
||||
|
||||
return getDirectionalFacing(block).getOppositeFace();
|
||||
}
|
||||
|
||||
public void setRedstoneWallTorch(@NotNull Block block, @NotNull BlockFace blockFace, @Nullable BlockFace storedDirection) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
block = CrossMaterial.REDSTONE_WALL_TORCH.setMaterial(block, storedDirection == BlockFace.NORTH);
|
||||
BlockState torch = block.getState();
|
||||
|
||||
if (!CrossMaterial.REDSTONE_WALL_TORCH.equals(torch.getType())) {
|
||||
WirelessRedstone.getWRLogger().warning("Receiver at " + block.getLocation().toString() + " cannot be set to a redstone wall torch. " +
|
||||
"Is it in a valid location?");
|
||||
return;
|
||||
}
|
||||
|
||||
RedstoneWallTorch torchData = (RedstoneWallTorch) block.getBlockData();
|
||||
|
||||
torchData.setFacing(blockFace);
|
||||
torch.setBlockData(torchData);
|
||||
torch.update();
|
||||
}
|
||||
|
||||
public void setSignWall(@NotNull Block block, @NotNull BlockFace blockFace, @Nullable BlockFace storedDirection) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
block = CrossMaterial.WALL_SIGN.setMaterial(block, storedDirection == BlockFace.NORTH);
|
||||
BlockState sign = block.getState();
|
||||
|
||||
if (!(block.getBlockData() instanceof WallSign)) {
|
||||
WirelessRedstone.getWRLogger().warning("Receiver at " + block.getLocation().toString() + " cannot be set to a wallsign. " +
|
||||
"Is it in a valid location?");
|
||||
return;
|
||||
}
|
||||
|
||||
WallSign signData = (WallSign) sign.getBlockData();
|
||||
signData.setFacing(blockFace);
|
||||
sign.setBlockData(signData);
|
||||
sign.update();
|
||||
}
|
||||
|
||||
public void setSignRotation(@NotNull Block block, @NotNull BlockFace blockFace) {
|
||||
Objects.requireNonNull(block, "Block cannot be NULL");
|
||||
|
||||
if (block.getBlockData() instanceof Rotatable) {
|
||||
Rotatable blockData = ((Rotatable) block.getBlockData());
|
||||
blockData.setRotation(blockFace);
|
||||
block.setBlockData(blockData);
|
||||
} else if (block.getBlockData() instanceof Directional) {
|
||||
Directional blockData = ((Directional) block.getBlockData());
|
||||
blockData.setFacing(blockFace);
|
||||
block.setBlockData(blockData);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Block needs to be a org.bukkit.block.data.Rotatable or org.bukkit.block.data.Directional found "
|
||||
+ block.getBlockData().getClass());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package net.licks92.wirelessredstone.compat;
|
||||
|
||||
public class InternalProvider {
|
||||
|
||||
private static InternalBlockData compatBlockData = new InternalBlockData();
|
||||
private static InternalWorldEditHooker compatWorldEditHooker = new InternalWorldEditHooker();
|
||||
|
||||
public static InternalBlockData getCompatBlockData() {
|
||||
return compatBlockData;
|
||||
}
|
||||
|
||||
public static InternalWorldEditHooker getCompatWorldEditHooker() {
|
||||
return compatWorldEditHooker;
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
package net.licks92.wirelessredstone.compat;
|
||||
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.event.extent.EditSessionEvent;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.util.eventbus.Subscribe;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import net.licks92.wirelessredstone.WirelessRedstone;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class InternalWorldEditHooker {
|
||||
|
||||
@Subscribe
|
||||
public void wrapForLogging(EditSessionEvent event) {
|
||||
Actor actor = event.getActor();
|
||||
World world = event.getWorld();
|
||||
if ((actor != null) && (event.getStage().equals(EditSession.Stage.BEFORE_CHANGE))) {
|
||||
event.setExtent(new InternalWorldEditLogger(actor, world, event.getExtent()));
|
||||
}
|
||||
}
|
||||
|
||||
public void register() {
|
||||
try {
|
||||
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(WirelessRedstone.getInstance(), () -> {
|
||||
try {
|
||||
InternalWorldEditHooker worldEditHooker = new InternalWorldEditHooker();
|
||||
WirelessRedstone.getInstance().setWorldEditHooker(worldEditHooker);
|
||||
WorldEdit.getInstance().getEventBus().register(worldEditHooker);
|
||||
} catch (Exception e) {
|
||||
WirelessRedstone.getWRLogger().severe("Error while hooking worldedit");
|
||||
}
|
||||
}, 0L);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void unRegister() {
|
||||
try {
|
||||
try {
|
||||
WorldEdit.getInstance().getEventBus().unregister(WirelessRedstone.getInstance().getWorldEditHooker());
|
||||
} catch (Exception e) {
|
||||
WirelessRedstone.getWRLogger().severe("Error while unhooking worldedit");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package net.licks92.wirelessredstone.compat;
|
||||
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.bukkit.BukkitWorld;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
import net.licks92.wirelessredstone.WirelessRedstone;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.Sign;
|
||||
|
||||
public class InternalWorldEditLogger extends AbstractDelegateExtent {
|
||||
|
||||
private final Actor eventActor;
|
||||
private final World eventWorld;
|
||||
private final Extent eventExtent;
|
||||
|
||||
protected InternalWorldEditLogger(Actor actor, World world, Extent extent) {
|
||||
super(extent);
|
||||
this.eventActor = actor;
|
||||
this.eventWorld = world;
|
||||
this.eventExtent = extent;
|
||||
}
|
||||
|
||||
protected void onBlockChange(BlockVector3 position, BlockStateHolder<?> blockStateHolder) {
|
||||
if (!(this.eventWorld instanceof BukkitWorld)) {
|
||||
return;
|
||||
}
|
||||
|
||||
org.bukkit.World world = ((BukkitWorld) this.eventWorld).getWorld();
|
||||
if (world == null || position == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Block block = world.getBlockAt(position.x(), position.y(), position.z());
|
||||
if (WirelessRedstone.getSignManager().isWirelessRedstoneSign(block)) {
|
||||
Sign sign = (Sign) block.getState();
|
||||
String channelName = sign.getLine(1);
|
||||
WirelessRedstone.getSignManager().removeSign(channelName, block.getLocation());
|
||||
WirelessRedstone.getWRLogger().debug("Removed sign at " + block.getLocation() + " because it was edited by WorldEdit");
|
||||
}
|
||||
}
|
||||
|
||||
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 location, T block) throws WorldEditException {
|
||||
this.onBlockChange(location, block);
|
||||
return this.eventExtent.setBlock(location, block);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user