From e237e30bb985431de8c01a8154a9de7502d59a8b Mon Sep 17 00:00:00 2001 From: EriolEandur Date: Sat, 25 Nov 2023 23:55:20 +0100 Subject: [PATCH] twigs fixes --- pom.xml | 2 +- .../LecternProtectionListener.java | 15 ++++- .../SpecialBlockBranchTwigs.java | 55 +++++++++++++++---- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index c428ab7..e3501e4 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.mcmiddleearth MCME-Architect - 2.9.6 + 2.9.7 jar UTF-8 diff --git a/src/main/java/com/mcmiddleearth/architect/additionalListeners/LecternProtectionListener.java b/src/main/java/com/mcmiddleearth/architect/additionalListeners/LecternProtectionListener.java index 9a039c4..f389860 100644 --- a/src/main/java/com/mcmiddleearth/architect/additionalListeners/LecternProtectionListener.java +++ b/src/main/java/com/mcmiddleearth/architect/additionalListeners/LecternProtectionListener.java @@ -11,15 +11,15 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; +import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.player.PlayerEditBookEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerTakeLecternBookEvent; import org.bukkit.inventory.ItemStack; -import java.util.Arrays; import java.util.Objects; -import java.util.logging.Logger; public class LecternProtectionListener implements Listener { @@ -54,6 +54,17 @@ public void placeBookLectern(PlayerInteractEvent event) { || event.getPlayer().hasPermission(Permission.LECTERN_EDITOR.getPermissionNode())) { BlockData data = event.getClickedBlock().getBlockData(); if(data instanceof org.bukkit.block.data.type.Lectern lecternData) { + if(lecternData.isPowered() && lectern.getInventory().getItem(0)!=null) { + event.setCancelled(true); + event.getPlayer().openBook(Objects.requireNonNull(lectern.getInventory().getItem(0))); + Bukkit.getPluginManager().registerEvents(new Listener() { + @EventHandler + public void onEdit(PlayerEditBookEvent event) { + Objects.requireNonNull(lectern.getInventory().getItem(0)).setItemMeta(event.getNewBookMeta()); + HandlerList.unregisterAll(this); + } + }, ArchitectPlugin.getPluginInstance()); + } if(!lecternData.hasBook() && event.getItem()!=null && (event.getItem().getType().equals(Material.WRITTEN_BOOK) diff --git a/src/main/java/com/mcmiddleearth/architect/specialBlockHandling/specialBlocks/SpecialBlockBranchTwigs.java b/src/main/java/com/mcmiddleearth/architect/specialBlockHandling/specialBlocks/SpecialBlockBranchTwigs.java index fc62415..297d679 100644 --- a/src/main/java/com/mcmiddleearth/architect/specialBlockHandling/specialBlocks/SpecialBlockBranchTwigs.java +++ b/src/main/java/com/mcmiddleearth/architect/specialBlockHandling/specialBlocks/SpecialBlockBranchTwigs.java @@ -24,6 +24,8 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; +import java.util.logging.Logger; + /** * * @author Eriol_Eandur @@ -32,9 +34,16 @@ public class SpecialBlockBranchTwigs extends SpecialBlockFourDirectionsVariants private static final String[] variants = new String[]{"Lower","Upper"}; + private Shift upperShift = new Shift(0,0,1); + private Shift lowerShift = new Shift(0,1,0); + protected SpecialBlockBranchTwigs(String id, String[] variants, - BlockData[][] data) { + BlockData[][] data, Shift lowerShift, Shift upperShift) { super(id,variants, data, SpecialBlockType.BRANCH_TWIGS); + if(lowerShift!=null) { + this.lowerShift = lowerShift; + this.upperShift = upperShift; + } } @Override @@ -46,17 +55,18 @@ public Block getBlock(Block clicked, BlockFace blockFace, Location interactionPo @Override public Shift getLower(BlockFace orientation, Block clicked, Player player, Location interactionPoint) { +Logger.getGlobal().info("Shift twig: "+lowerShift.getX()+" "+lowerShift.getY()+" "+lowerShift.getZ()); + Shift shift = lowerShift; if(getVariant(null, null, null, player, interactionPoint)==1) { - return new Shift(0,1,0); //1 = upper - } else { - return switch(orientation) { //0 = lower - case SOUTH -> new Shift(0,0,1); - case EAST -> new Shift(1,0,0); - case NORTH -> new Shift(0,0,-1); - case WEST -> new Shift(-1,0,0); - default -> new Shift(0,0,0); - }; + shift = upperShift; } + return switch(orientation) { //1 = upper + case SOUTH -> rotate(shift,0);//new Shift(0,0,1); + case EAST -> rotate(shift,1);//new Shift(1,0,0); + case NORTH -> rotate(shift, 2);//new Shift(0,0,-1); + case WEST -> rotate(shift, 3);//new Shift(-1,0,0); + default -> new Shift(0,0,0); + }; } @Override @@ -77,15 +87,27 @@ public BlockFace getDownwardOrientation(BlockFace orientation) { public static SpecialBlockBranchTwigs loadFromConfig(ConfigurationSection config, String id) { BlockData[][] data = loadBlockDataFromConfig(config, SpecialBlockFourDirections.fourFaces, variants); + Shift lowerShift = null; + if(config.contains("lowerShift")) { + ConfigurationSection shiftSection = config.getConfigurationSection("lowerShift"); + assert shiftSection != null; + lowerShift = new Shift(shiftSection.getInt("x",0),shiftSection.getInt("y",0),shiftSection.getInt("z",1)); + } + Shift upperShift = null; + if(config.contains("upperShift")) { + ConfigurationSection shiftSection = config.getConfigurationSection("upperShift"); + assert shiftSection != null; + upperShift = new Shift(shiftSection.getInt("x",0),shiftSection.getInt("y",0),shiftSection.getInt("z",1)); + } if (data == null) { return null; } - return new SpecialBlockBranchTwigs(id, variants, data); + return new SpecialBlockBranchTwigs(id, variants, data, lowerShift, upperShift); } @Override public int getVariant(Block blockPlace, Block clicked, BlockFace blockFace, Player player, Location interactionPoint) { - return (player.getLocation().getPitch()>=0?0:1); //0=Lower, 1=Upper + return (player.getLocation().getPitch()>=0?1:0); //0=Lower, 1=Upper } @Override @@ -202,5 +224,14 @@ public BlockFace getOrientation(Block block) { } return null; }*/ + + private static Shift rotate(Shift shift, int rotations) { + return switch(rotations % 4) { + case 1 -> new Shift(shift.getZ(),shift.getY(),-shift.getX()); + case 2 -> new Shift(-shift.getX(),shift.getY(),-shift.getZ()); + case 3 -> new Shift(-shift.getZ(),shift.getY(),shift.getX()); + default -> shift; + }; + } }