11package com .github .skriptdev .skript .plugin .elements .expressions .item ;
22
3+ import com .github .skriptdev .skript .api .hytale .utils .EntityUtils ;
34import com .hypixel .hytale .component .Ref ;
45import com .hypixel .hytale .component .Store ;
56import com .hypixel .hytale .server .core .entity .LivingEntity ;
67import com .hypixel .hytale .server .core .inventory .InventoryComponent ;
8+ import com .hypixel .hytale .server .core .inventory .InventoryComponent .Armor ;
9+ import com .hypixel .hytale .server .core .inventory .InventoryComponent .Backpack ;
10+ import com .hypixel .hytale .server .core .inventory .InventoryComponent .Hotbar ;
11+ import com .hypixel .hytale .server .core .inventory .InventoryComponent .Storage ;
12+ import com .hypixel .hytale .server .core .inventory .InventoryComponent .Tool ;
13+ import com .hypixel .hytale .server .core .inventory .InventoryComponent .Utility ;
714import com .hypixel .hytale .server .core .inventory .ItemStack ;
815import com .hypixel .hytale .server .core .inventory .container .CombinedItemContainer ;
916import com .hypixel .hytale .server .core .inventory .container .ItemContainer ;
@@ -26,33 +33,34 @@ public static void register(SkriptRegistration registration) {
2633 "inventory of %livingentities%" )
2734 .name ("Inventory of LivingEntity" )
2835 .description ("While it is called \" inventory\" it will " +
29- "actually return the combined everything ItemContainer of a LivingEntity." ,
30- "This is essentially a shortcut for the `combined everything item contatiner of` expression." )
36+ "actually return the combined hotbar/storage ItemContainers of a LivingEntity." ,
37+ "This is essentially a shortcut for the `combined hotbar first item contatiner of` expression." ,
38+ "Clearing will clear all ItemContainers, not just the hotbar/storage." )
3139 .examples ("set {_inv} to inventory of player" ,
3240 "clear inventory of player" ,
3341 "add itemstack of ingredient_stick to inventory of player" )
3442 .since ("1.0.0" )
3543 .register ();
3644 }
3745
38- private Expression <LivingEntity > entity ;
46+ private Expression <LivingEntity > entities ;
3947
4048 @ SuppressWarnings ("unchecked" )
4149 @ Override
4250 public boolean init (Expression <?>[] expressions , int matchedPattern , @ NotNull ParseContext parseContext ) {
43- this .entity = (Expression <LivingEntity >) expressions [0 ];
51+ this .entities = (Expression <LivingEntity >) expressions [0 ];
4452 return true ;
4553 }
4654
4755 @ Override
4856 public ItemContainer [] getValues (@ NotNull TriggerContext ctx ) {
4957 List <ItemContainer > containers = new ArrayList <>();
50- for (LivingEntity livingEntity : this .entity .getArray (ctx )) {
51- Ref <EntityStore > reference = livingEntity .getReference ();
52- if (reference == null ) continue ;
58+ for (LivingEntity livingEntity : this .entities .getArray (ctx )) {
59+ Ref <EntityStore > ref = livingEntity .getReference ();
60+ if (ref == null ) continue ;
5361
54- Store <EntityStore > store = reference .getStore ();
55- CombinedItemContainer combined = InventoryComponent .getCombined (store , reference , InventoryComponent .EVERYTHING );
62+ Store <EntityStore > store = ref .getStore ();
63+ CombinedItemContainer combined = InventoryComponent .getCombined (store , ref , InventoryComponent .HOTBAR_FIRST );
5664 containers .add (combined );
5765 }
5866 return containers .toArray (new ItemContainer [0 ]);
@@ -70,16 +78,40 @@ public Optional<Class<?>[]> acceptsChange(@NotNull ChangeMode mode) {
7078 public void change (@ NotNull TriggerContext ctx , @ NotNull ChangeMode changeMode , Object @ NotNull [] changeWith ) {
7179 ItemContainer [] toChange = getValues (ctx );
7280 if (changeMode == ChangeMode .ADD ) {
73- for (ItemContainer inventory : toChange ) {
81+ for (ItemContainer container : toChange ) {
7482 for (Object o : changeWith ) {
7583 if (o instanceof ItemStack itemStack ) {
76- inventory .addItemStack (itemStack );
84+ container .addItemStack (itemStack );
7785 }
7886 }
7987 }
8088 } else if (changeMode == ChangeMode .DELETE ) {
81- for (ItemContainer inventory : toChange ) {
82- inventory .clear ();
89+ for (LivingEntity livingEntity : this .entities .getArray (ctx )) {
90+ // Combined storage clearing is currently broken
91+ Storage storage = EntityUtils .getComponent (livingEntity , Storage .getComponentType ());
92+ if (storage != null ) {
93+ storage .getInventory ().clear ();
94+ }
95+ Hotbar hotbar = EntityUtils .getComponent (livingEntity , Hotbar .getComponentType ());
96+ if (hotbar != null ) {
97+ hotbar .getInventory ().clear ();
98+ }
99+ Armor armor = EntityUtils .getComponent (livingEntity , Armor .getComponentType ());
100+ if (armor != null ) {
101+ armor .getInventory ().clear ();
102+ }
103+ Backpack backpack = EntityUtils .getComponent (livingEntity , Backpack .getComponentType ());
104+ if (backpack != null ) {
105+ backpack .getInventory ().clear ();
106+ }
107+ Utility utility = EntityUtils .getComponent (livingEntity , Utility .getComponentType ());
108+ if (utility != null ) {
109+ utility .getInventory ().clear ();
110+ }
111+ Tool tool = EntityUtils .getComponent (livingEntity , Tool .getComponentType ());
112+ if (tool != null ) {
113+ tool .getInventory ().clear ();
114+ }
83115 }
84116 }
85117 }
@@ -91,7 +123,7 @@ public boolean isSingle() {
91123
92124 @ Override
93125 public String toString (@ NotNull TriggerContext ctx , boolean debug ) {
94- return "inventory of " + this .entity .toString (ctx , debug );
126+ return "inventory of " + this .entities .toString (ctx , debug );
95127 }
96128
97129}
0 commit comments