generated from JamCoreModding/multi-loader-template-mod
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
25 changed files
with
675 additions
and
478 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
- Fix a dupe exploit (#44) | ||
This release brings about a significant internal rework with should make Utility Belt significantly | ||
less buggy. I have conducted a long play testing session before this release to try to verify | ||
everything works as intended. | ||
|
||
## Closed Issues | ||
|
||
- #47: fishing rod flickering | ||
- #46: dupe bug | ||
- #45: dupe bug | ||
|
||
Alongside the issues that were reported, many others have been fixed. |
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
189 changes: 138 additions & 51 deletions
189
common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltInventory.java
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 |
---|---|---|
@@ -1,59 +1,146 @@ | ||
package io.github.jamalam360.utility_belt; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import net.minecraft.core.NonNullList; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.ListTag; | ||
import net.minecraft.world.SimpleContainer; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class UtilityBeltInventory extends SimpleContainer { | ||
public UtilityBeltInventory() { | ||
super(4); | ||
} | ||
|
||
@Override | ||
public void fromTag(ListTag listTag) { | ||
this.clearContent(); | ||
|
||
for (int i = 0; i < listTag.size(); ++i) { | ||
this.setItem(i, ItemStack.of(listTag.getCompound(i))); | ||
} | ||
} | ||
|
||
@Override | ||
public ListTag createTag() { | ||
ListTag listTag = new ListTag(); | ||
|
||
for (int i = 0; i < this.getContainerSize(); ++i) { | ||
listTag.add(this.getItem(i).save(new CompoundTag())); | ||
} | ||
|
||
return listTag; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof UtilityBeltInventory other) { | ||
if (other.getContainerSize() == this.getContainerSize()) { | ||
for (int i = 0; i < this.getContainerSize(); i++) { | ||
if (!ItemStack.matches(this.getItem(i), other.getItem(i))) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public UtilityBeltInventory clone() { | ||
UtilityBeltInventory inv = new UtilityBeltInventory(); | ||
for (int i = 0; i < this.getContainerSize(); i++) { | ||
inv.setItem(i, this.getItem(i).copy()); | ||
} | ||
|
||
return inv; | ||
} | ||
public record UtilityBeltInventory(List<ItemStack> items) { | ||
|
||
public static final UtilityBeltInventory EMPTY = new UtilityBeltInventory(NonNullList.withSize(4, ItemStack.EMPTY)); | ||
|
||
public UtilityBeltInventory { | ||
if (items.size() != 4) { | ||
throw new IllegalArgumentException("Utility belt inventory must have exactly 4 items"); | ||
} | ||
} | ||
|
||
public ItemStack getItem(int index) { | ||
return items.get(index); | ||
} | ||
|
||
public int getContainerSize() { | ||
return 4; | ||
} | ||
|
||
public UtilityBeltInventory clone() { | ||
return new UtilityBeltInventory(new ArrayList<>(items)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof UtilityBeltInventory other) { | ||
for (int i = 0; i < items.size(); i++) { | ||
if (!ItemStack.matches(items.get(i), other.items.get(i))) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashStackList(items); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return invToString("UtilityBeltInventory[", items); | ||
} | ||
|
||
public static class Mutable extends SimpleContainer { | ||
|
||
public Mutable(UtilityBeltInventory inv) { | ||
super(4); | ||
|
||
for (int i = 0; i < inv.getContainerSize(); i++) { | ||
this.setItem(i, inv.getItem(i)); | ||
} | ||
} | ||
|
||
public UtilityBeltInventory toImmutable() { | ||
return new UtilityBeltInventory(new ArrayList<>(this.getItems())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof UtilityBeltInventory other) { | ||
for (int i = 0; i < this.getItems().size(); i++) { | ||
if (!ItemStack.matches(this.getItems().get(i), other.items.get(i))) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return invToString("UtilityBeltInventory$Mutable[", this.getItems()); | ||
} | ||
} | ||
|
||
private static String invToString(String prefix, List<ItemStack> items) { | ||
StringBuilder string = new StringBuilder(prefix); | ||
|
||
for (int i = 0; i < items.size(); i++) { | ||
string.append(items.get(i)); | ||
if (i < items.size() - 1) { | ||
string.append(", "); | ||
} | ||
} | ||
|
||
return string.toString(); | ||
} | ||
|
||
// Methods added for 1.20.6 --> 1.20.4 backporting ease | ||
private static int hashItemAndTag(@Nullable ItemStack arg) { | ||
if (arg != null) { | ||
int i = 31 + arg.getItem().hashCode(); | ||
return 31 * i + (arg.getTag() == null ? 0 : arg.getTag().hashCode()); | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
private static int hashStackList(List<ItemStack> list) { | ||
int i = 0; | ||
|
||
for (ItemStack itemstack : list) { | ||
i = i * 31 + hashItemAndTag(itemstack); | ||
} | ||
|
||
return i; | ||
} | ||
|
||
public static UtilityBeltInventory fromTag(ListTag listTag) { | ||
UtilityBeltInventory.Mutable inv = new UtilityBeltInventory.Mutable(UtilityBeltInventory.EMPTY); | ||
|
||
for (int i = 0; i < listTag.size(); ++i) { | ||
ItemStack itemStack = ItemStack.of(listTag.getCompound(i)); | ||
if (!itemStack.isEmpty()) { | ||
inv.setItem(i, itemStack); | ||
} | ||
} | ||
|
||
return inv.toImmutable(); | ||
} | ||
|
||
public ListTag toTag() { | ||
ListTag listTag = new ListTag(); | ||
|
||
for (int i = 0; i < this.getContainerSize(); ++i) { | ||
ItemStack itemStack = this.getItem(i); | ||
listTag.add(itemStack.save(new CompoundTag())); | ||
} | ||
|
||
return listTag; | ||
} | ||
} |
Oops, something went wrong.