use holders
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
package dev.xkmc.l2core.base.menu.base;
 | 
			
		||||
 | 
			
		||||
import dev.xkmc.l2serial.util.Wrappers;
 | 
			
		||||
import net.minecraft.core.RegistryAccess;
 | 
			
		||||
import net.minecraft.server.level.ServerPlayer;
 | 
			
		||||
import net.minecraft.world.Container;
 | 
			
		||||
import net.minecraft.world.SimpleContainer;
 | 
			
		||||
@@ -87,11 +88,14 @@ public class BaseContainerMenu<T extends BaseContainerMenu<T>> extends AbstractC
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public final Inventory inventory;
 | 
			
		||||
	public final Player player;
 | 
			
		||||
	public final Container container;
 | 
			
		||||
	public final SpriteManager sprite;
 | 
			
		||||
	public final RegistryAccess access;
 | 
			
		||||
	protected int added = 0;
 | 
			
		||||
	protected final boolean isVirtual;
 | 
			
		||||
 | 
			
		||||
	private final SpriteManager sprite;
 | 
			
		||||
 | 
			
		||||
	private boolean updating = false;
 | 
			
		||||
 | 
			
		||||
	private final Map<SlotKey, Slot> slotMap = new TreeMap<>(SlotKey.COMPARATOR);
 | 
			
		||||
@@ -109,14 +113,20 @@ public class BaseContainerMenu<T extends BaseContainerMenu<T>> extends AbstractC
 | 
			
		||||
	protected BaseContainerMenu(MenuType<?> type, int wid, Inventory plInv, SpriteManager manager, Function<T, SimpleContainer> factory, boolean isVirtual) {
 | 
			
		||||
		super(type, wid);
 | 
			
		||||
		this.inventory = plInv;
 | 
			
		||||
		this.player = plInv.player;
 | 
			
		||||
		container = factory.apply(Wrappers.cast(this));
 | 
			
		||||
		sprite = manager;
 | 
			
		||||
		int x = manager.get().getPlInvX();
 | 
			
		||||
		int y = manager.get().getPlInvY();
 | 
			
		||||
		access = plInv.player.level().registryAccess();
 | 
			
		||||
		int x = getLayout().getPlInvX();
 | 
			
		||||
		int y = getLayout().getPlInvY();
 | 
			
		||||
		this.bindPlayerInventory(plInv, x, y);
 | 
			
		||||
		this.isVirtual = isVirtual;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public MenuLayoutConfig getLayout() {
 | 
			
		||||
		return sprite.get(access);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Binds player inventory. Should not be called by others, but permits override.
 | 
			
		||||
	 */
 | 
			
		||||
@@ -146,7 +156,7 @@ public class BaseContainerMenu<T extends BaseContainerMenu<T>> extends AbstractC
 | 
			
		||||
	 * Add new slots, with item input predicate
 | 
			
		||||
	 */
 | 
			
		||||
	protected void addSlot(String name, Predicate<ItemStack> pred) {
 | 
			
		||||
		sprite.get().getSlot(name, (x, y) -> new PredSlot(container, added++, x, y, pred), this::addSlot);
 | 
			
		||||
		getLayout().getSlot(name, (x, y) -> new PredSlot(container, added++, x, y, pred), this::addSlot);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
@@ -155,7 +165,7 @@ public class BaseContainerMenu<T extends BaseContainerMenu<T>> extends AbstractC
 | 
			
		||||
	 */
 | 
			
		||||
	protected void addSlot(String name, BiPredicate<Integer, ItemStack> pred) {
 | 
			
		||||
		int current = added;
 | 
			
		||||
		sprite.get().getSlot(name, (x, y) -> {
 | 
			
		||||
		getLayout().getSlot(name, (x, y) -> {
 | 
			
		||||
			int i = added - current;
 | 
			
		||||
			var ans = new PredSlot(container, added, x, y, e -> pred.test(i, e));
 | 
			
		||||
			added++;
 | 
			
		||||
@@ -167,7 +177,7 @@ public class BaseContainerMenu<T extends BaseContainerMenu<T>> extends AbstractC
 | 
			
		||||
	 * Add new slots, with other modifications to the slot.
 | 
			
		||||
	 */
 | 
			
		||||
	protected void addSlot(String name, Predicate<ItemStack> pred, Consumer<PredSlot> modifier) {
 | 
			
		||||
		sprite.get().getSlot(name, (x, y) -> {
 | 
			
		||||
		getLayout().getSlot(name, (x, y) -> {
 | 
			
		||||
			PredSlot s = new PredSlot(container, added++, x, y, pred);
 | 
			
		||||
			modifier.accept(s);
 | 
			
		||||
			return s;
 | 
			
		||||
@@ -180,7 +190,7 @@ public class BaseContainerMenu<T extends BaseContainerMenu<T>> extends AbstractC
 | 
			
		||||
	 */
 | 
			
		||||
	protected void addSlot(String name, BiPredicate<Integer, ItemStack> pred, BiConsumer<Integer, PredSlot> modifier) {
 | 
			
		||||
		int current = added;
 | 
			
		||||
		sprite.get().getSlot(name, (x, y) -> {
 | 
			
		||||
		getLayout().getSlot(name, (x, y) -> {
 | 
			
		||||
			int i = added - current;
 | 
			
		||||
			var ans = new PredSlot(container, added, x, y, e -> pred.test(i, e));
 | 
			
		||||
			modifier.accept(i, ans);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,5 @@
 | 
			
		||||
package dev.xkmc.l2core.base.menu.base;
 | 
			
		||||
 | 
			
		||||
import dev.xkmc.l2core.util.Proxy;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.gui.GuiGraphics;
 | 
			
		||||
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
 | 
			
		||||
@@ -11,8 +10,8 @@ public abstract class BaseContainerScreen<T extends BaseContainerMenu<T>> extend
 | 
			
		||||
 | 
			
		||||
	public BaseContainerScreen(T cont, Inventory plInv, Component title) {
 | 
			
		||||
		super(cont, plInv, title);
 | 
			
		||||
		this.imageHeight = menu.sprite.get().getHeight();
 | 
			
		||||
		this.inventoryLabelY = menu.sprite.get().getPlInvY() - 11;
 | 
			
		||||
		this.imageHeight = menu.getLayout().getHeight();
 | 
			
		||||
		this.inventoryLabelY = menu.getLayout().getPlInvY() - 11;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
@@ -22,7 +21,7 @@ public abstract class BaseContainerScreen<T extends BaseContainerMenu<T>> extend
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	protected boolean click(int btn) {
 | 
			
		||||
		if (menu.clickMenuButton(Proxy.getClientPlayer(), btn) && Minecraft.getInstance().gameMode != null) {
 | 
			
		||||
		if (menu.clickMenuButton(menu.player, btn) && Minecraft.getInstance().gameMode != null) {
 | 
			
		||||
			Minecraft.getInstance().gameMode.handleInventoryButtonClick(this.menu.containerId, btn);
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package dev.xkmc.l2core.base.menu.base;
 | 
			
		||||
 | 
			
		||||
import dev.xkmc.l2core.init.L2LibReg;
 | 
			
		||||
import net.minecraft.core.RegistryAccess;
 | 
			
		||||
import net.minecraft.resources.ResourceLocation;
 | 
			
		||||
 | 
			
		||||
public record SpriteManager(ResourceLocation id) {
 | 
			
		||||
@@ -9,8 +10,8 @@ public record SpriteManager(ResourceLocation id) {
 | 
			
		||||
		this(ResourceLocation.fromNamespaceAndPath(modid, path));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public MenuLayoutConfig get() {
 | 
			
		||||
		return L2LibReg.MENU_LAYOUT.get(id);
 | 
			
		||||
	public MenuLayoutConfig get(RegistryAccess access) {
 | 
			
		||||
		return L2LibReg.MENU_LAYOUT.get(access, id).value();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,29 +1,34 @@
 | 
			
		||||
package dev.xkmc.l2core.init.reg.datapack;
 | 
			
		||||
 | 
			
		||||
import com.mojang.datafixers.util.Pair;
 | 
			
		||||
import dev.xkmc.l2core.util.Proxy;
 | 
			
		||||
import net.minecraft.core.Holder;
 | 
			
		||||
import net.minecraft.core.RegistryAccess;
 | 
			
		||||
import net.neoforged.neoforge.registries.datamaps.DataMapType;
 | 
			
		||||
import net.neoforged.neoforge.registries.datamaps.RegisterDataMapTypesEvent;
 | 
			
		||||
import org.jetbrains.annotations.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.stream.Stream;
 | 
			
		||||
 | 
			
		||||
public record DataMapReg<K, V>(DataMapType<K, V> reg) implements ValSet<K, V> {
 | 
			
		||||
public record DataMapReg<K, V>(DataMapType<K, V> reg) {
 | 
			
		||||
 | 
			
		||||
	public void register(final RegisterDataMapTypesEvent event) {
 | 
			
		||||
		event.register(reg);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Nullable
 | 
			
		||||
	public V get(K key) {
 | 
			
		||||
		var registry = Proxy.getRegistryAccess().registry(reg.registryKey()).get();
 | 
			
		||||
		return registry.getData(reg, registry.getResourceKey(key).get());
 | 
			
		||||
	public V get(RegistryAccess access, Holder<K> key) {
 | 
			
		||||
		var registry = access.registry(reg.registryKey());
 | 
			
		||||
		if (registry.isEmpty()) return null;
 | 
			
		||||
		var id = key.unwrapKey();
 | 
			
		||||
		if (id.isEmpty()) return null;
 | 
			
		||||
		return registry.get().getData(reg, id.get());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public Stream<Pair<K, V>> getAll() {
 | 
			
		||||
		var registry = Proxy.getRegistryAccess().registry(reg.registryKey()).get();
 | 
			
		||||
		return registry.getDataMap(reg).entrySet().stream()
 | 
			
		||||
				.map(e -> Pair.of(registry.get(e.getKey()), e.getValue()));
 | 
			
		||||
	public Stream<Pair<Holder<K>, V>> getAll(RegistryAccess access) {
 | 
			
		||||
		var registry = access.registry(reg.registryKey());
 | 
			
		||||
		if (registry.isEmpty()) return Stream.empty();
 | 
			
		||||
		return registry.get().getDataMap(reg).entrySet().stream()
 | 
			
		||||
				.map(e -> Pair.of(registry.get().getHolderOrThrow(e.getKey()), e.getValue()));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,28 +1,33 @@
 | 
			
		||||
package dev.xkmc.l2core.init.reg.datapack;
 | 
			
		||||
 | 
			
		||||
import com.mojang.datafixers.util.Pair;
 | 
			
		||||
import com.mojang.serialization.Codec;
 | 
			
		||||
import dev.xkmc.l2core.util.Proxy;
 | 
			
		||||
import net.minecraft.core.Holder;
 | 
			
		||||
import net.minecraft.core.Registry;
 | 
			
		||||
import net.minecraft.core.RegistryAccess;
 | 
			
		||||
import net.minecraft.resources.ResourceKey;
 | 
			
		||||
import net.minecraft.resources.ResourceLocation;
 | 
			
		||||
import net.neoforged.neoforge.registries.DataPackRegistryEvent;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.util.stream.Stream;
 | 
			
		||||
 | 
			
		||||
public record DatapackReg<T>(ResourceKey<Registry<T>> key, Codec<T> codec) implements ValSet<ResourceLocation, T> {
 | 
			
		||||
public record DatapackReg<T>(ResourceKey<Registry<T>> key, Codec<T> codec) {
 | 
			
		||||
 | 
			
		||||
	public void onRegister(DataPackRegistryEvent.NewRegistry event) {
 | 
			
		||||
		event.dataPackRegistry(key, codec, codec);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public T get(ResourceLocation id) {
 | 
			
		||||
		return Proxy.getRegistryAccess().registry(key).get().get(id);
 | 
			
		||||
	@Nullable
 | 
			
		||||
	public Holder<T> get(RegistryAccess access, ResourceLocation id) {
 | 
			
		||||
		var reg = access.registry(key);
 | 
			
		||||
		if (reg.isEmpty()) return null;
 | 
			
		||||
		return reg.get().getHolder(id).orElse(null);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public Stream<Pair<ResourceLocation, T>> getAll() {
 | 
			
		||||
		return Proxy.getRegistryAccess().registry(key).get().entrySet()
 | 
			
		||||
				.stream().map(e -> Pair.of(e.getKey().location(), e.getValue()));
 | 
			
		||||
	public Stream<Holder<T>> getAll(RegistryAccess access) {
 | 
			
		||||
		var reg = access.registry(key);
 | 
			
		||||
		if (reg.isEmpty()) return Stream.empty();
 | 
			
		||||
		return reg.get().holders().map(e -> e);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +0,0 @@
 | 
			
		||||
package dev.xkmc.l2core.init.reg.datapack;
 | 
			
		||||
 | 
			
		||||
import com.mojang.datafixers.util.Pair;
 | 
			
		||||
import org.jetbrains.annotations.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.stream.Stream;
 | 
			
		||||
 | 
			
		||||
public interface ValSet<K, V> {
 | 
			
		||||
 | 
			
		||||
	@Nullable
 | 
			
		||||
	V get(K k);
 | 
			
		||||
 | 
			
		||||
	Stream<Pair<K, V>> getAll();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -13,6 +13,7 @@ import java.util.Optional;
 | 
			
		||||
 | 
			
		||||
public class Proxy {
 | 
			
		||||
 | 
			
		||||
	@Deprecated
 | 
			
		||||
	public static RegistryAccess getRegistryAccess() {
 | 
			
		||||
		if (FMLEnvironment.dist == Dist.CLIENT) {
 | 
			
		||||
			return Minecraft.getInstance().level.registryAccess();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user