Skip to content

Commit 7386f4b

Browse files
Add support for Shelf item logging (#833)
* add shelf logging * bump supported version to 1.21.11 * cleanup * Update pom.xml Remove project branch definition from properties. * fix incorrect instanceof check --------- Co-authored-by: Intelli <[email protected]>
1 parent 0f0cecc commit 7386f4b

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

src/main/java/net/coreprotect/bukkit/BukkitAdapter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,18 @@ public boolean isCopperChest(Material material) {
366366
return false;
367367
}
368368

369+
@Override
370+
public boolean isShelf(Material material){
371+
return false;
372+
}
373+
369374
@Override
370375
public Set<Material> copperChestMaterials() {
371376
return EMPTY_SET;
372377
}
378+
379+
@Override
380+
public Set<Material> shelfMaterials() {
381+
return EMPTY_SET;
382+
}
373383
}

src/main/java/net/coreprotect/bukkit/BukkitInterface.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ public interface BukkitInterface {
132132
*/
133133
boolean isChiseledBookshelf(Material material);
134134

135+
136+
/**
137+
* Checks if a material is a shelf of any wood kind.
138+
*
139+
* @param material
140+
* The material to check
141+
* @return true if the material is a shelf, false otherwise
142+
*/
143+
boolean isShelf(Material material);
144+
145+
135146
/**
136147
* Checks if a material is a bookshelf book.
137148
*
@@ -441,4 +452,6 @@ public interface BukkitInterface {
441452

442453
Set<Material> copperChestMaterials();
443454

455+
Set<Material> shelfMaterials();
456+
444457
}

src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
* - Registry handling for named objects
2424
* - Updated interaction blocks
2525
*/
26-
public class Bukkit_v1_21 extends Bukkit_v1_20 implements BukkitInterface {
26+
public class Bukkit_v1_21 extends Bukkit_v1_20 {
2727

2828
public static Set<Material> COPPER_CHESTS = new HashSet<>(Arrays.asList());
29+
public static Set<Material> SHELVES = new HashSet<>(Arrays.asList());
2930

3031
/**
3132
* Initializes the Bukkit_v1_21 adapter with 1.21-specific block groups and mappings.
@@ -37,6 +38,7 @@ public Bukkit_v1_21() {
3738
BlockGroup.INTERACT_BLOCKS.addAll(copperChestMaterials());
3839
BlockGroup.CONTAINERS.addAll(copperChestMaterials());
3940
BlockGroup.UPDATE_STATE.addAll(copperChestMaterials());
41+
BlockGroup.CONTAINERS.addAll(shelfMaterials());
4042
}
4143

4244
/**
@@ -111,6 +113,7 @@ public Object getRegistryKey(Object value) {
111113
return ((Keyed) value).getKey().toString();
112114
}
113115

116+
114117
/**
115118
* Gets a registry value from a key string and class.
116119
* Used for deserializing registry objects.
@@ -177,6 +180,11 @@ public boolean isCopperChest(Material material) {
177180
return false;
178181
}
179182

183+
@Override
184+
public boolean isShelf(Material material) {
185+
return SHELVES.contains(material);
186+
}
187+
180188
@Override
181189
public Set<Material> copperChestMaterials() {
182190
if (COPPER_CHESTS.isEmpty()) {
@@ -198,4 +206,16 @@ public Set<Material> copperChestMaterials() {
198206

199207
return COPPER_CHESTS;
200208
}
209+
210+
@Override
211+
public Set<Material> shelfMaterials() {
212+
if (SHELVES.isEmpty()) {
213+
Material shelf = Material.getMaterial("OAK_SHELF");
214+
if (shelf != null) {
215+
SHELVES.addAll(Tag.WOODEN_SHELVES.getValues());
216+
}
217+
}
218+
219+
return SHELVES;
220+
}
201221
}

src/main/java/net/coreprotect/listener/player/PlayerInteractListener.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import org.bukkit.block.Sign;
1919
import org.bukkit.block.data.Bisected;
2020
import org.bukkit.block.data.Bisected.Half;
21+
import org.bukkit.block.data.SideChaining.ChainPart;
2122
import org.bukkit.block.data.BlockData;
2223
import org.bukkit.block.data.Lightable;
2324
import org.bukkit.block.data.Waterlogged;
2425
import org.bukkit.block.data.type.Bed;
2526
import org.bukkit.block.data.type.Bed.Part;
2627
import org.bukkit.block.data.type.Cake;
28+
import org.bukkit.block.data.type.Shelf;
2729
import org.bukkit.entity.EnderCrystal;
2830
import org.bukkit.entity.Entity;
2931
import org.bukkit.entity.Player;
@@ -37,6 +39,7 @@
3739
import org.bukkit.inventory.EquipmentSlot;
3840
import org.bukkit.inventory.InventoryHolder;
3941
import org.bukkit.inventory.ItemStack;
42+
import org.bukkit.util.Vector;
4043

4144
import net.coreprotect.CoreProtect;
4245
import net.coreprotect.bukkit.BukkitAdapter;
@@ -465,6 +468,49 @@ else if (event.getHand().equals(EquipmentSlot.OFF_HAND) && offHand != null && Bu
465468
InventoryChangeListener.inventoryTransaction(player.getName(), blockState.getLocation(), null);
466469
}
467470
}
471+
} else if (BukkitAdapter.ADAPTER.isShelf(type) ){
472+
BlockData blockState = block.getBlockData();
473+
if (blockState instanceof Shelf){
474+
Shelf shelf = (Shelf) blockState;
475+
476+
// ignore clicking on the back face
477+
if (event.getBlockFace() != shelf.getFacing()){
478+
return;
479+
}
480+
481+
if (shelf.getSideChain() == ChainPart.UNCONNECTED){
482+
InventoryChangeListener.inventoryTransaction(player.getName(), block.getLocation(), null);
483+
} else {
484+
Block center = block;
485+
Vector direction = shelf.getFacing().getDirection();
486+
487+
if (shelf.getSideChain() == ChainPart.LEFT){
488+
center = center.getRelative(direction.getBlockZ(), 0, -direction.getBlockX());
489+
} else if (shelf.getSideChain() == ChainPart.RIGHT){
490+
center = center.getRelative(-direction.getBlockZ(), 0, direction.getBlockX());
491+
}
492+
493+
BlockData centerBlockData = center.getBlockData();
494+
if (centerBlockData instanceof Shelf){
495+
// log center
496+
InventoryChangeListener.inventoryTransaction(player.getName(), center.getLocation(), null);
497+
498+
if (((Shelf)centerBlockData).getSideChain() != ChainPart.CENTER){
499+
// if it's not the center it's just a chain of 2
500+
InventoryChangeListener.inventoryTransaction(player.getName(), block.getLocation(), null);
501+
} else {
502+
Block left = center.getRelative(-direction.getBlockZ(), 0, direction.getBlockX());
503+
InventoryChangeListener.inventoryTransaction(player.getName(), left.getLocation(), null);
504+
505+
Block right = center.getRelative(direction.getBlockZ(), 0, -direction.getBlockX());
506+
InventoryChangeListener.inventoryTransaction(player.getName(), right.getLocation(), null);
507+
}
508+
} else {
509+
// fallback if invalid block is found just log clicked shelf
510+
InventoryChangeListener.inventoryTransaction(player.getName(), block.getLocation(), null);
511+
}
512+
}
513+
}
468514
}
469515
else if (BukkitAdapter.ADAPTER.isDecoratedPot(type)) {
470516
BlockState blockState = block.getState();

0 commit comments

Comments
 (0)