Skip to content

Commit

Permalink
Merge pull request #77 from MCME/development
Browse files Browse the repository at this point in the history
twigs fixes
  • Loading branch information
EriolEandur authored Nov 26, 2023
2 parents 10174f4 + e237e30 commit 9df3bc0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mcmiddleearth</groupId>
<artifactId>MCME-Architect</artifactId>
<version>2.9.6</version>
<version>2.9.7</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

import java.util.logging.Logger;

/**
*
* @author Eriol_Eandur
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
};
}

}

0 comments on commit 9df3bc0

Please sign in to comment.