66import net .minecraft .item .ItemStack ;
77import net .minecraft .nbt .NbtCompound ;
88import net .minecraft .nbt .NbtElement ;
9- import net .minecraft .nbt .NbtList ;
109import net .minecraft .registry .RegistryWrapper ;
1110import net .minecraft .util .Hand ;
1211import net .minecraft .util .math .BlockPos ;
1312
14- import java .util .HashSet ;
13+ import java .util .HashMap ;
14+ import java .util .Map ;
1515import java .util .Set ;
1616import java .util .UUID ;
1717
1818public class ItemProviderBlockEntity extends GlowcaseBlockEntity implements InfiniteInventory , StackInteractable {
1919 protected ItemStack stack = ItemStack .EMPTY ;
2020 protected GivesItem givesItem = GivesItem .ALWAYS ;
21- protected final Set <UUID > givenTo = new HashSet <>();
21+ public long cooldown = 0 ;
22+ protected final Map <UUID , Long > givenTimes = new HashMap <>();
2223
2324 public ItemProviderBlockEntity (BlockPos pos , BlockState state ) {
2425 super (Glowcase .ITEM_PROVIDER_BLOCK_ENTITY .get (), pos , state );
@@ -32,7 +33,7 @@ public boolean matchesStack(ItemStack stack) {
3233 @ Override
3334 public void setFromStack (ItemStack stack ) {
3435 this .stack = stack .copy ();
35- this .givenTo .clear ();
36+ this .givenTimes .clear ();
3637 this .markDirty ();
3738 }
3839
@@ -61,13 +62,10 @@ public void writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLook
6162 super .writeNbt (tag , registryLookup );
6263 if (!this .stack .isEmpty ()) tag .put ("item" , this .stack .encode (registryLookup ));
6364 tag .putString ("gives_item" , this .givesItem .name ());
64- NbtList given = new NbtList ();
65- for (UUID id : givenTo ) {
66- NbtCompound givenTag = new NbtCompound ();
67- givenTag .putUuid ("id" , id );
68- given .add (givenTag );
69- }
70- tag .put ("given_to" , given );
65+ tag .putLong ("cooldown" , this .cooldown );
66+ NbtCompound timesNbt = new NbtCompound ();
67+ givenTimes .forEach ((id , tick ) -> timesNbt .putLong (id .toString (), tick ));
68+ tag .put ("given_times" , timesNbt );
7169 }
7270
7371 @ Override
@@ -79,28 +77,30 @@ public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku
7977 } else {
8078 this .givesItem = GivesItem .ALWAYS ;
8179 }
80+ this .cooldown = tag .getLong ("cooldown" );
8281
83- givenTo .clear ();
84- if (tag .contains ("given_to" )) {
85- NbtList given = tag .getList ("given_to" , NbtElement .COMPOUND_TYPE );
86- for (NbtElement elem : given ) {
87- NbtCompound comp = ((NbtCompound ) elem );
88- givenTo .add (comp .getUuid ("id" ));
89- }
82+ givenTimes .clear ();
83+ NbtCompound given = tag .getCompound ("given_times" );
84+ for (String key : given .getKeys ()) {
85+ givenTimes .put (UUID .fromString (key ), given .getLong (key ));
9086 }
9187 }
9288
9389 public void cycleGiveType () {
9490 this .givesItem = GivesItem .values ()[(this .givesItem .ordinal () + 1 ) % GivesItem .values ().length ];
95- givenTo .clear ();
91+ givenTimes .clear ();
9692 markDirty ();
9793 }
9894
95+ public long getCooldownTicks (PlayerEntity player ) {
96+ return givenTimes .containsKey (player .getUuid ()) ? givenTimes .get (player .getUuid ()) + this .cooldown * 20 - world .getTime () : 0 ;
97+ }
98+
9999 public boolean canGiveTo (PlayerEntity player ) {
100100 if (!hasItem ()) return false ;
101101 else return switch (this .givesItem ) {
102102 case ALWAYS -> true ;
103- case ONCE -> player .isCreative () || ! givenTo . contains (player . getUuid ()) ;
103+ case TIMED -> player .isCreative () || getCooldownTicks (player ) <= 0 ;
104104 case ONE -> player .isCreative () || !player .getInventory ().containsAny (Set .of (stack .getItem ()));
105105 };
106106 }
@@ -117,12 +117,12 @@ public void giveTo(PlayerEntity player) {
117117 player .setStackInHand (Hand .MAIN_HAND , itemStack );
118118 }
119119 if (!player .isCreative ()) {
120- givenTo . add (player .getUuid ());
120+ givenTimes . put (player .getUuid (), world . getTime ());
121121 markDirty ();
122122 }
123123 }
124124
125125 public enum GivesItem {
126- ALWAYS , ONCE , ONE
126+ ALWAYS , TIMED , ONE
127127 }
128128}
0 commit comments