Skip to content

Commit

Permalink
Simplify inventory handling
Browse files Browse the repository at this point in the history
* Rename view-only inventory for clarity
* Repackage menus and Bukkit implementations for clarity
* Reduce duplicate code somewhat
  • Loading branch information
Jikoo committed Jul 15, 2024
1 parent bca080b commit 64899e5
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 364 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lishid.openinv.internal;

public interface InternalOwned<T> {

T getOwnerHandle();

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.lishid.openinv.internal;

public interface ViewOnly {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.lishid.openinv.internal.v1_21_R1.container;

import com.lishid.openinv.internal.IAnySilentContainer;
import com.lishid.openinv.internal.v1_21_R1.container.menu.OpenChestMenu;
import com.lishid.openinv.internal.v1_21_R1.player.PlayerManager;
import com.lishid.openinv.util.ReflectionHelper;
import com.lishid.openinv.util.lang.LanguageManager;
Expand Down Expand Up @@ -103,7 +104,7 @@ public boolean activateContainer(
PlayerEnderChestContainer enderChest = player.getEnderChestInventory();
enderChest.setActiveChest(enderChestTile);
player.openMenu(new SimpleMenuProvider((containerCounter, playerInventory, ignored) -> {
MenuType<?> containers = OpenContainerMenu.getContainers(enderChest.getContainerSize());
MenuType<?> containers = OpenChestMenu.getChestMenuType(enderChest.getContainerSize());
int rows = enderChest.getContainerSize() / 9;
return new ChestMenu(containers, containerCounter, playerInventory, enderChest, rows);
}, Component.translatable("container.enderchest")));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.lishid.openinv.internal.v1_21_R1.container;

import com.lishid.openinv.internal.ISpecialEnderChest;
import com.lishid.openinv.internal.InternalOwned;
import com.lishid.openinv.internal.v1_21_R1.container.menu.OpenEnderChestMenu;
import com.lishid.openinv.internal.v1_21_R1.player.PlayerManager;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
Expand All @@ -25,7 +27,8 @@
import java.util.ArrayList;
import java.util.List;

public class OpenEnderChest implements Container, StackedContentsCompatible, MenuProvider, ISpecialEnderChest {
public class OpenEnderChest implements Container, StackedContentsCompatible, MenuProvider,
InternalOwned<ServerPlayer>, ISpecialEnderChest {

private CraftInventory inventory;
private @NotNull ServerPlayer owner;
Expand All @@ -38,6 +41,7 @@ public OpenEnderChest(@NotNull org.bukkit.entity.Player player) {
this.items = owner.getEnderChestInventory().items;
}

@Override
public @NotNull ServerPlayer getOwnerHandle() {
return owner;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.lishid.openinv.internal.v1_21_R1.container;

import com.lishid.openinv.internal.ISpecialPlayerInventory;
import com.lishid.openinv.internal.InternalOwned;
import com.lishid.openinv.internal.v1_21_R1.container.bukkit.OpenPlayerInventory;
import com.lishid.openinv.internal.v1_21_R1.container.menu.OpenInventoryMenu;
import com.lishid.openinv.internal.v1_21_R1.player.PlayerManager;
import com.lishid.openinv.internal.v1_21_R1.container.slot.Content;
import com.lishid.openinv.internal.v1_21_R1.container.slot.ContentCrafting;
Expand Down Expand Up @@ -37,7 +40,7 @@
import java.util.ArrayList;
import java.util.List;

public class OpenInventory implements Container, MenuProvider, ISpecialPlayerInventory {
public class OpenInventory implements Container, MenuProvider, InternalOwned<ServerPlayer>, ISpecialPlayerInventory {

private final List<Content> slots;
private final int size;
Expand Down Expand Up @@ -203,10 +206,6 @@ public InventoryType.SlotType getSlotType(int index) {
return slots.get(index).getSlotType();
}

public ServerPlayer getOwnerHandle() {
return owner;
}

public @NotNull Component getTitle(@Nullable ServerPlayer viewer) {
MutableComponent component = Component.empty();
// Prefix for use with custom bitmap image fonts.
Expand All @@ -230,6 +229,11 @@ public ServerPlayer getOwnerHandle() {
return component;
}

@Override
public ServerPlayer getOwnerHandle() {
return owner;
}

@Override
public @NotNull org.bukkit.inventory.Inventory getBukkitInventory() {
if (bukkitEntity == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lishid.openinv.internal.v1_21_R1.container;
package com.lishid.openinv.internal.v1_21_R1.container.bukkit;

import com.lishid.openinv.internal.ViewOnly;
import net.minecraft.world.Container;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftInventory;
Expand All @@ -14,9 +15,9 @@
/**
* A locked down "empty" inventory that rejects plugin interaction.
*/
public class OpenViewInventory extends CraftInventory {
public class OpenDummyInventory extends CraftInventory implements ViewOnly {

public OpenViewInventory(Container inventory) {
public OpenDummyInventory(Container inventory) {
super(inventory);
}

Expand All @@ -30,16 +31,19 @@ public void setItem(int index, @Nullable ItemStack item) {

}

@SuppressWarnings("NonApiType")
@Override
public @NotNull HashMap<Integer, ItemStack> addItem(@NotNull ItemStack... items) throws IllegalArgumentException {
return arrayToHashMap(items);
}

@SuppressWarnings("NonApiType")
@Override
public @NotNull HashMap<Integer, ItemStack> removeItem(@NotNull ItemStack... items) throws IllegalArgumentException {
return arrayToHashMap(items);
}

@SuppressWarnings("NonApiType")
private static @NotNull HashMap<Integer, ItemStack> arrayToHashMap(@NotNull ItemStack[] items) {
HashMap<Integer, ItemStack> ignored = new HashMap<>();
for (int index = 0; index < items.length; ++index) {
Expand Down Expand Up @@ -93,12 +97,14 @@ public boolean containsAtLeast(@Nullable ItemStack item, int amount) {
return false;
}

@SuppressWarnings("NonApiType")
@Override
public @NotNull HashMap<Integer, ItemStack> all(
@NotNull Material material) throws IllegalArgumentException {
return new HashMap<>();
}

@SuppressWarnings("NonApiType")
@Override
public @NotNull HashMap<Integer, ItemStack> all(@Nullable ItemStack item) {
return new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lishid.openinv.internal.v1_21_R1.container;
package com.lishid.openinv.internal.v1_21_R1.container.bukkit;

import com.google.common.base.Preconditions;
import com.lishid.openinv.internal.v1_21_R1.container.OpenInventory;
import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Inventory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lishid.openinv.internal.v1_21_R1.container;
package com.lishid.openinv.internal.v1_21_R1.container.bukkit;

import com.lishid.openinv.internal.v1_21_R1.container.OpenInventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

Expand Down
Loading

0 comments on commit 64899e5

Please sign in to comment.