22
33import cn .nukkit .block .BlockID ;
44import cn .nukkit .blockentity .BlockEntity ;
5- import cn .nukkit .event .EventHandler ;
65import cn .nukkit .event .EventPriority ;
76import cn .nukkit .event .Listener ;
87import cn .nukkit .event .inventory .InventoryTransactionEvent ;
98import cn .nukkit .inventory .InventoryType ;
109import cn .nukkit .inventory .transaction .action .SlotChangeAction ;
1110import cn .nukkit .item .Item ;
1211import cn .nukkit .plugin .PluginBase ;
12+ import lombok .Getter ;
1313import me .iwareq .fakeinventories .block .DoubleFakeBlock ;
1414import me .iwareq .fakeinventories .block .FakeBlock ;
15+ import me .iwareq .fakeinventories .block .FakeBlockOffset ;
1516import me .iwareq .fakeinventories .block .SingleFakeBlock ;
16- import lombok .Getter ;
1717
1818import java .util .EnumMap ;
1919import java .util .Map ;
@@ -24,10 +24,12 @@ public class FakeInventories extends PluginBase implements Listener {
2424 private static FakeInventories instance ;
2525
2626 private static final Map <InventoryType , FakeBlock > FAKE_BLOCKS = new EnumMap <>(InventoryType .class );
27+ private static FakeBlockOffset FAKE_BLOCK_OFFSET ;
2728
2829 @ Override
2930 public void onLoad () {
30- instance = this ;
31+ FakeInventories .instance = this ;
32+ this .saveDefaultConfig ();
3133 FAKE_BLOCKS .put (InventoryType .CHEST , new SingleFakeBlock (BlockID .CHEST , BlockEntity .CHEST ));
3234 FAKE_BLOCKS .put (InventoryType .ENDER_CHEST , new SingleFakeBlock (BlockID .ENDER_CHEST , BlockEntity .ENDER_CHEST ));
3335 FAKE_BLOCKS .put (InventoryType .DOUBLE_CHEST , new DoubleFakeBlock (BlockID .CHEST , BlockEntity .CHEST ));
@@ -38,30 +40,33 @@ public void onLoad() {
3840 FAKE_BLOCKS .put (InventoryType .DROPPER , new SingleFakeBlock (BlockID .DROPPER , InventoryType .DROPPER .getDefaultTitle ()));
3941 FAKE_BLOCKS .put (InventoryType .HOPPER , new SingleFakeBlock (BlockID .HOPPER_BLOCK , BlockEntity .HOPPER ));
4042 FAKE_BLOCKS .put (InventoryType .SHULKER_BOX , new SingleFakeBlock (BlockID .SHULKER_BOX , BlockEntity .SHULKER_BOX ));
43+ FAKE_BLOCK_OFFSET = FakeBlockOffset .valueOf (this .getConfig ().getString ("fake-block-offset-mode" ).toUpperCase ());
4144 }
4245
4346 @ Override
4447 public void onEnable () {
45- this .getServer ().getPluginManager ().registerEvents (this , this );
46- }
47-
48- @ EventHandler (priority = EventPriority .MONITOR )
49- public void onInventoryTransaction (InventoryTransactionEvent event ) {
50- event .getTransaction ().getActions ().forEach (action -> {
51- if (action instanceof SlotChangeAction slotChange ) {
52- if (slotChange .getInventory () instanceof FakeInventory inventory ) {
53- int slot = slotChange .getSlot ();
54- Item sourceItem = action .getSourceItem ();
55- inventory .handle (slot , sourceItem , event );
48+ this .getServer ().getPluginManager ().subscribeEvent (InventoryTransactionEvent .class , event -> {
49+ event .getTransaction ().getActions ().forEach (action -> {
50+ if (action instanceof SlotChangeAction slotChange ) {
51+ if (slotChange .getInventory () instanceof FakeInventory inventory ) {
52+ int slot = slotChange .getSlot ();
53+ Item sourceItem = action .getSourceItem ();
54+ inventory .handle (slot , sourceItem , event );
55+ }
5656 }
57- }
58- });
57+ });
58+ }, EventPriority . MONITOR , this );
5959 }
60+
6061 public static FakeBlock getFakeBlock (InventoryType inventoryType ) {
6162 FakeBlock fakeBlock = FAKE_BLOCKS .get (inventoryType );
6263 if (fakeBlock == null ) {
6364 throw new NullPointerException ("FakeBlock for " + inventoryType .name () + " inventory not found!" );
6465 }
6566 return fakeBlock ;
6667 }
68+
69+ public static FakeBlockOffset getFakeBlockOffset () {
70+ return FAKE_BLOCK_OFFSET ;
71+ }
6772}
0 commit comments