Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SlimefunItemStack reimplementation #4251

Open
wants to merge 53 commits into
base: walshy/mc-1.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6f2f30d
Fix tests
Intybyte Oct 22, 2024
a62ecff
Use delegation... this is gonna be a long pr
Intybyte Oct 22, 2024
75d9c75
Address review
Intybyte Oct 23, 2024
c8efc79
Refactor some more stuff
Intybyte Oct 23, 2024
70c96e0
More fixed, most notably ChestMenuUtils
Intybyte Oct 25, 2024
e7aefc6
More refactoring
Intybyte Oct 25, 2024
b646376
Refactor getDelegate to item, and some small changes
Intybyte Nov 4, 2024
787851c
Merge branch 'refs/heads/walshy/mc-1.21' into walshy/mc-1.21-sfi
Intybyte Nov 9, 2024
35396d0
Small error on merge
Intybyte Nov 9, 2024
dfa4e94
Merge fixes + more fixes
Intybyte Nov 9, 2024
3435390
GrindStone fix
Intybyte Nov 9, 2024
3512cd3
MagicEyeOfEnder fix
Intybyte Nov 9, 2024
af27545
NetherStarReactor fix
Intybyte Nov 9, 2024
1b73f98
NuclearReactor fix
Intybyte Nov 9, 2024
1f28097
OilPump fix
Intybyte Nov 9, 2024
cc45f7d
OreCrusher fix
Intybyte Nov 9, 2024
5e25492
OrganicFertilizer fix
Intybyte Nov 9, 2024
8969397
OrganicFood fix
Intybyte Nov 9, 2024
3497af0
TreeGrowthAccelerator fix
Intybyte Nov 9, 2024
7749d43
Add method for SlimefunItemStack
Intybyte Nov 9, 2024
69ecf93
Smeltery Fix
Intybyte Nov 9, 2024
810ad17
Smeltery Fix
Intybyte Nov 9, 2024
a5094b1
SlimefunItemInteractListener Fix
Intybyte Nov 9, 2024
5d1952d
SaltResource Fix
Intybyte Nov 9, 2024
e10da88
Yes. I wrote a parser just for this.
Intybyte Nov 10, 2024
8c25fff
Couldn't write a parser for the finishing touches tho...
Intybyte Nov 10, 2024
a85442f
Fix slimefunItem + TODO
Intybyte Nov 12, 2024
e94b355
Fix RecipeType
Intybyte Nov 12, 2024
64c5e67
Fix AncientAltarListener
Intybyte Nov 12, 2024
e0e850b
Fix ArmorTask
Intybyte Nov 12, 2024
81c600d
Fix DeathpointListener
Intybyte Nov 12, 2024
8d940bd
Fix DefaultItemGroups
Intybyte Nov 12, 2024
45d3231
Fix GadgetsListener
Intybyte Nov 12, 2024
0cd88ca
Fix Resources
Intybyte Nov 12, 2024
e141d41
Fix Resources 2
Intybyte Nov 12, 2024
eec08ce
Fix SlimefunUtils
Intybyte Nov 12, 2024
ff3b5ec
Fix Some tests to call .item();
Intybyte Nov 12, 2024
b013127
Ok lets fire those tests
Intybyte Nov 12, 2024
f409400
Forgot those
Intybyte Nov 12, 2024
2683be5
Looks like there is no reason to keep that there
Intybyte Nov 12, 2024
f4c0864
TODOs
Intybyte Nov 12, 2024
861d303
SlimefunItemStack can't be casted to ItemStack anymore
Intybyte Nov 12, 2024
26b5a43
Fix null throwing
Intybyte Nov 12, 2024
60f183b
Fix init
Intybyte Nov 12, 2024
9ff36e5
Delombok, convert to equivalent without new dependency
Intybyte Nov 16, 2024
16260d6
Fixed soulbound testing
Intybyte Nov 16, 2024
415f690
Use cloneItem here
Intybyte Nov 16, 2024
3c1d8fd
.item() and getItem() now creates a defensive copy
Intybyte Nov 16, 2024
52c80c8
Fix RecipeType
Intybyte Nov 23, 2024
8214370
Merge branch 'refs/heads/walshy/mc-1.21' into walshy/mc-1.21-sfi
Intybyte Nov 23, 2024
c8f9181
Remove unused code
Intybyte Nov 25, 2024
25da22e
1.21.3 Bundles aren't allowed
Intybyte Nov 30, 2024
84a5bbc
Required false
Intybyte Nov 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ public void openTransmitterControlPanel(@Nonnull Player p) {
menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler());
}

menu.addItem(2, CustomItemStack.create(SlimefunItems.GPS_TRANSMITTER, im -> {
menu.addItem(2, CustomItemStack.create(SlimefunItems.GPS_TRANSMITTER.item(), im -> {
im.setDisplayName(ChatColor.GRAY + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.transmitters"));
im.setLore(null);
}));

menu.addMenuClickHandler(2, ChestMenuUtils.getEmptyClickHandler());

int complexity = getNetworkComplexity(p.getUniqueId());
menu.addItem(4, CustomItemStack.create(SlimefunItems.GPS_CONTROL_PANEL, "&7Network Info", "", "&8\u21E8 &7Status: " + getStatusText(p, complexity), "&8\u21E8 &7Complexity: &f" + complexity));
menu.addItem(4, CustomItemStack.create(SlimefunItems.GPS_CONTROL_PANEL.item(), "&7Network Info", "", "&8\u21E8 &7Status: " + getStatusText(p, complexity), "&8\u21E8 &7Complexity: &f" + complexity));
menu.addMenuClickHandler(4, ChestMenuUtils.getEmptyClickHandler());

menu.addItem(6, CustomItemStack.create(HeadTexture.GLOBE_OVERWORLD.getAsItemStack(), "&7" + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.waypoints"), "", ChatColor.GRAY + "\u21E8 " + Slimefun.getLocalization().getMessage(p, "guide.tooltips.open-itemgroup")));
Expand All @@ -177,7 +177,7 @@ public void openTransmitterControlPanel(@Nonnull Player p) {
if (sfi instanceof GPSTransmitter transmitter) {
int slot = inventory[index];

menu.addItem(slot, CustomItemStack.create(SlimefunItems.GPS_TRANSMITTER, "&bGPS Transmitter", "&8\u21E8 &7World: &f" + l.getWorld().getName(), "&8\u21E8 &7X: &f" + l.getX(), "&8\u21E8 &7Y: &f" + l.getY(), "&8\u21E8 &7Z: &f" + l.getZ(), "", "&8\u21E8 &7Signal Strength: &f" + transmitter.getMultiplier(l.getBlockY()), "&8\u21E8 &7Ping: &f" + NumberUtils.roundDecimalNumber(1000D / l.getY()) + "ms"));
menu.addItem(slot, CustomItemStack.create(SlimefunItems.GPS_TRANSMITTER.item(), "&bGPS Transmitter", "&8\u21E8 &7World: &f" + l.getWorld().getName(), "&8\u21E8 &7X: &f" + l.getX(), "&8\u21E8 &7Y: &f" + l.getY(), "&8\u21E8 &7Z: &f" + l.getZ(), "", "&8\u21E8 &7Signal Strength: &f" + transmitter.getMultiplier(l.getBlockY()), "&8\u21E8 &7Ping: &f" + NumberUtils.roundDecimalNumber(1000D / l.getY()) + "ms"));
menu.addMenuClickHandler(slot, ChestMenuUtils.getEmptyClickHandler());

index++;
Expand Down Expand Up @@ -232,14 +232,14 @@ public void openWaypointControlPanel(@Nonnull Player p) {
menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler());
}

menu.addItem(2, CustomItemStack.create(SlimefunItems.GPS_TRANSMITTER, "&7" + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.transmitters"), "", ChatColor.GRAY + "\u21E8 " + Slimefun.getLocalization().getMessage(p, "guide.tooltips.open-itemgroup")));
menu.addItem(2, CustomItemStack.create(SlimefunItems.GPS_TRANSMITTER.item(), "&7" + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.transmitters"), "", ChatColor.GRAY + "\u21E8 " + Slimefun.getLocalization().getMessage(p, "guide.tooltips.open-itemgroup")));
menu.addMenuClickHandler(2, (pl, slot, item, action) -> {
openTransmitterControlPanel(pl);
return false;
});

int complexity = getNetworkComplexity(p.getUniqueId());
menu.addItem(4, CustomItemStack.create(SlimefunItems.GPS_CONTROL_PANEL, "&7Network Info", "", "&8\u21E8 &7Status: " + (complexity > 0 ? "&2&lONLINE" : "&4&lOFFLINE"), "&8\u21E8 &7Complexity: &f" + complexity));
menu.addItem(4, CustomItemStack.create(SlimefunItems.GPS_CONTROL_PANEL.item(), "&7Network Info", "", "&8\u21E8 &7Status: " + (complexity > 0 ? "&2&lONLINE" : "&4&lOFFLINE"), "&8\u21E8 &7Complexity: &f" + complexity));
menu.addMenuClickHandler(4, ChestMenuUtils.getEmptyClickHandler());

menu.addItem(6, CustomItemStack.create(HeadTexture.GLOBE_OVERWORLD.getAsItemStack(), "&7" + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.waypoints")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import io.github.thebusybiscuit.slimefun4.api.exceptions.IncompatibleItemHandlerException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.MissingDependencyException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.UnregisteredItemException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.WrongItemStackException;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
Expand Down Expand Up @@ -153,7 +152,7 @@ public SlimefunItem(ItemGroup itemGroup, SlimefunItemStack item, RecipeType reci
Validate.notNull(recipeType, "'recipeType' is not allowed to be null!");

this.itemGroup = itemGroup;
this.itemStackTemplate = item;
this.itemStackTemplate = item.item();
this.id = item.getItemId();
this.recipeType = recipeType;
this.recipe = recipe;
Expand Down Expand Up @@ -219,7 +218,7 @@ protected SlimefunItem(ItemGroup itemGroup, ItemStack item, String id, RecipeTyp
* @return The {@link ItemStack} that this {@link SlimefunItem} represents
*/
public @Nonnull ItemStack getItem() {
return itemStackTemplate;
return itemStackTemplate.clone();
}

/**
Expand Down Expand Up @@ -500,11 +499,6 @@ public void register(@Nonnull SlimefunAddon addon) {
this.itemHandlers.clear();
}

// Lock the SlimefunItemStack from any accidental manipulations
if (itemStackTemplate instanceof SlimefunItemStack stack && isItemStackImmutable()) {
stack.lock();
}

postRegister();

// handle runtime-registrations / auto-loading
Expand Down Expand Up @@ -580,20 +574,6 @@ private void loadItemHandlers() {
}
}

/**
* This method returns whether the original {@link SlimefunItemStack} of this
* {@link SlimefunItem} is immutable.
*
* If <code>true</code> is returned, then any changes to the original {@link SlimefunItemStack}
* will be rejected with a {@link WrongItemStackException}.
* This ensures integrity so developers don't accidentally damage the wrong {@link ItemStack}.
*
* @return Whether the original {@link SlimefunItemStack} is immutable.
*/
protected boolean isItemStackImmutable() {
return true;
}

/**
* This method checks if the dependencies have been set up correctly.
*
Expand Down Expand Up @@ -773,11 +753,6 @@ public boolean isItem(@Nullable ItemStack item) {
return false;
}

// If the given item is a SlimefunitemStack, simply compare the id
if (item instanceof SlimefunItemStack stack) {
return getId().equals(stack.getItemId());
}

if (item.hasItemMeta()) {
Optional<String> itemId = Slimefun.getItemDataService().getItemData(item);

Expand Down Expand Up @@ -912,14 +887,6 @@ public final void addOfficialWikipage(@Nonnull String page) {
* @return This item's name in {@link ItemStack} form
*/
public final @Nonnull String getItemName() {
if (itemStackTemplate instanceof SlimefunItemStack) {
Optional<String> name = ((SlimefunItemStack) itemStackTemplate).getItemMetaSnapshot().getDisplayName();

if (name.isPresent()) {
return name.get();
}
}

return ItemUtils.getItemName(itemStackTemplate);
}

Expand Down Expand Up @@ -1185,6 +1152,19 @@ public final int hashCode() {
return Optional.ofNullable(getById(id));
}

public static @Nullable SlimefunItem getByItem(@Nullable SlimefunItemStack slimefunItemStack) {
if (slimefunItemStack == null) {
return null;
}

var delegate = slimefunItemStack.item();
if (delegate.getType() == Material.AIR) {
return null;
}

return getById(slimefunItemStack.getItemId());
}

/**
* Retrieve a {@link SlimefunItem} from an {@link ItemStack}.
*
Expand All @@ -1197,10 +1177,6 @@ public final int hashCode() {
return null;
}

if (item instanceof SlimefunItemStack stack) {
return getById(stack.getItemId());
}

Optional<String> itemID = Slimefun.getItemDataService().getItemData(item);

return itemID.map(SlimefunItem::getById).orElse(null);
Expand Down
Loading
Loading