-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52a61b1
commit baa8059
Showing
7 changed files
with
458 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package gregtech.api.recipes; | ||
|
||
import gregtech.api.util.ItemStackKey; | ||
import net.minecraft.item.ItemStack; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.lang.ref.WeakReference; | ||
import java.util.WeakHashMap; | ||
|
||
public class KeySharedStack { | ||
|
||
private static final WeakHashMap<ItemStackKey,WeakReference<ItemStackKey>> registeredItemStackKeys = new WeakHashMap<>(); | ||
|
||
private KeySharedStack() { | ||
|
||
} | ||
|
||
public static synchronized ItemStackKey getRegisteredStack(final @Nonnull ItemStack itemStack) { | ||
if (itemStack.isEmpty()) { | ||
throw new IllegalArgumentException("stack cannot be empty"); | ||
} | ||
|
||
int oldStackSize = itemStack.getCount(); | ||
itemStack.setCount(1); | ||
|
||
ItemStackKey search = new ItemStackKey(itemStack, false); | ||
WeakReference<ItemStackKey> weak = registeredItemStackKeys.get(search); | ||
ItemStackKey ret = null; | ||
|
||
if (weak != null) { | ||
ret = weak.get(); | ||
} | ||
|
||
if (ret == null) { | ||
ret = new ItemStackKey(itemStack); | ||
registeredItemStackKeys.put(ret, new WeakReference<>(ret)); | ||
} | ||
itemStack.setCount(oldStackSize); | ||
|
||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.