11package com .circulation .only_one_item .util ;
22
3- import it .unimi .dsi .fastutil .ints .Int2ObjectMap ;
43import it .unimi .dsi .fastutil .ints .Int2ObjectOpenHashMap ;
5- import it .unimi .dsi .fastutil .objects .Object2ObjectOpenHashMap ;
64import lombok .Getter ;
7- import lombok .NonNull ;
8- import net .minecraft .init .Items ;
5+ import lombok .ToString ;
96import net .minecraft .item .Item ;
107import net .minecraft .item .ItemStack ;
8+ import net .minecraft .nbt .NBTTagCompound ;
119import net .minecraft .util .ResourceLocation ;
1210
1311import java .util .Map ;
1412import java .util .Objects ;
13+ import java .util .concurrent .ConcurrentHashMap ;
14+ import java .util .function .Function ;
1515
16+ @ ToString
1617public final class SimpleItem {
17- private static final Map <ResourceLocation , Int2ObjectMap <SimpleItem >> chace = new Object2ObjectOpenHashMap <>();
18- public static final SimpleItem EMPTY = SimpleItem .getInstance (ItemStack .EMPTY );
19- @ NonNull
18+
2019 private final ResourceLocation item ;
2120 @ Getter
2221 private final int meta ;
22+ public static final SimpleItem empty = new SimpleItem (ItemStack .EMPTY );
2323 private final int hashCode ;
24+ private static final NBTTagCompound NullNbt = new NBTTagCompound () {
25+ @ Override
26+ public boolean equals (Object nbt ) {
27+ return nbt == this ;
28+ }
2429
25- private SimpleItem (@ NonNull ResourceLocation item , int meta ) {
30+ @ Override
31+ public int hashCode () {
32+ return Integer .MIN_VALUE ;
33+ }
34+ };
35+ private static final Map <ResourceLocation , Int2ObjectOpenHashMapS > chane = new ConcurrentHashMap <>();
36+ private static final Function <ResourceLocation , Int2ObjectOpenHashMapS > intMap = item -> new Int2ObjectOpenHashMapS ();
37+ private final NBTTagCompound nbt ;
38+ private SimpleItem (ResourceLocation item , int meta , NBTTagCompound nbt ) {
2639 this .item = item ;
2740 this .meta = meta ;
28- this .hashCode = Objects .hash (item , meta );
41+ this .nbt = nbt ;
42+ this .hashCode = Objects .hash (item , meta , nbt );
2943 }
3044
31- public static SimpleItem getInstance ( @ NonNull String item , int meta ) {
32- return getInstance ( new ResourceLocation ( item ), meta );
45+ private SimpleItem ( ItemStack stack ) {
46+ this ( stack . getItem (). getRegistryName (), stack . getItemDamage ( ), stack . getTagCompound () );
3347 }
3448
35- public static SimpleItem getInstance (ResourceLocation item , int meta ) {
36- var map = chace .computeIfAbsent (item , rl -> new Int2ObjectOpenHashMap <>());
37- if (map .containsKey (meta )) {
38- return map .get (meta );
39- }
40- var value = new SimpleItem (item , meta );
41- map .put (meta , value );
42- return value ;
49+ public static SimpleItem getInstance (final String rl , final int meta ) {
50+ return getInstance (new ResourceLocation (rl ), meta );
4351 }
4452
45- public static SimpleItem getInstance (@ NonNull ItemStack stack ) {
46- return getInstance (stack .getItem ().getRegistryName (), stack .getMetadata ());
53+ public static SimpleItem getInstance (final ResourceLocation rl , final int meta ) {
54+ return chane .computeIfAbsent (rl , intMap )
55+ .computeIfAbsent (meta )
56+ .computeIfAbsent (NullNbt , n -> new SimpleItem (rl , meta , null ));
4757 }
4858
49- public boolean isEmpty () {
50- return this == EMPTY || Objects .equals (this .item , ItemStack .EMPTY .getItem ().getRegistryName ());
59+ public static SimpleItem getInstance (final ItemStack stack ) {
60+ if (stack .isEmpty ()) return empty ;
61+ var nbt = stack .getTagCompound ();
62+ return chane .computeIfAbsent (stack .getItem ().getRegistryName (), intMap )
63+ .computeIfAbsent (stack .getItemDamage ())
64+ .computeIfAbsent (nbt == null ? NullNbt : nbt , n -> new SimpleItem (stack ));
5165 }
5266
53- public Item getItem () {
54- return Item .getByNameOrId (item .toString ());
67+ public static SimpleItem getNoNBTInstance (final ItemStack stack ) {
68+ if (stack .isEmpty ()) return empty ;
69+ return chane .computeIfAbsent (stack .getItem ().getRegistryName (), intMap )
70+ .computeIfAbsent (stack .getItemDamage ())
71+ .computeIfAbsent (NullNbt , n -> new SimpleItem (stack ));
5572 }
5673
57- public String getItemID () {
58- return item . toString ( );
74+ public Item getItem () {
75+ return Item . REGISTRY . getObject ( item );
5976 }
6077
61- public ResourceLocation getItemRL () {
78+ public ResourceLocation getRegistryName () {
6279 return item ;
6380 }
6481
82+ public String getItemID () {
83+ return item .toString ();
84+ }
85+
6586 public ItemStack getItemStack (int amount ) {
66- var stack = getItem ();
67- if (stack != null && stack != Items . AIR ) {
68- return new ItemStack ( stack , amount , meta );
87+ var i = new ItemStack ( getItem (), amount , meta );
88+ if (nbt != null && ! nbt . isEmpty () ) {
89+ i . setTagCompound ( nbt );
6990 }
70- return ItemStack .EMPTY ;
91+ return i ;
92+ }
93+
94+ public boolean isEmpty () {
95+ return this == empty ;
7196 }
7297
7398 @ Override
7499 public boolean equals (Object o ) {
75100 if (o == null || getClass () != o .getClass ()) return false ;
76101 SimpleItem that = (SimpleItem ) o ;
77- return meta == that .meta && Objects .equals (item , that .item );
102+ return meta == that .meta && Objects .equals (item , that .item ) && Objects . equals ( nbt , that . nbt ) ;
78103 }
79104
80105 @ Override
81106 public int hashCode () {
82107 return hashCode ;
83108 }
84109
85- @ Override
86- public String toString () {
87- return item + ":" + meta ;
110+ private static class Int2ObjectOpenHashMapS extends Int2ObjectOpenHashMap <Map <NBTTagCompound , SimpleItem >> {
111+
112+ public Map <NBTTagCompound , SimpleItem > computeIfAbsent (int key ) {
113+ Map <NBTTagCompound , SimpleItem > v ;
114+
115+ if ((v = get (key )) == null ) {
116+ synchronized (this ) {
117+ if ((v = get (key )) == null ) {
118+ v = new ConcurrentHashMap <>();
119+ put (key , v );
120+ }
121+ }
122+ }
123+
124+ return v ;
125+ }
126+
88127 }
89128}
0 commit comments