Skip to content

Commit

Permalink
refactor lectern menu event
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Nov 9, 2024
1 parent b4939da commit 5404350
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import java.util.function.Supplier;

public class CustomLecternMenuEvent {
public void register(LecternMenuRegistry.MenuFactory factory, ItemLike item) {
public void register(LecternMenuRegistry.MenuProviderFactory factory, ItemLike item) {
LecternMenuRegistry.register(factory, item);
}

public void register(LecternMenuRegistry.MenuFactory factory, Supplier<ItemLike> item) {
public void register(LecternMenuRegistry.MenuProviderFactory factory, Supplier<ItemLike> item) {
LecternMenuRegistry.register(factory, item);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.LecternBlockEntity;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

public class LecternMenuRegistry {
private static final Map<Item, MenuFactory> FACTORIES = new Object2ObjectOpenHashMap<>();
private static final Map<Item, MenuProviderFactory> FACTORIES = new Object2ObjectOpenHashMap<>();

public static void register(MenuFactory factory, Supplier<ItemLike> itemLike) {
public static void register(MenuProviderFactory factory, Supplier<ItemLike> itemLike) {
Preconditions.checkNotNull(itemLike, "Item for menu is null or doesn't exist");
Item item = Preconditions.checkNotNull(itemLike.get().asItem(), "Item for menu is null or doesn't exist");

Expand All @@ -28,24 +31,23 @@ public static void register(MenuFactory factory, Supplier<ItemLike> itemLike) {
}
}

public static void register(MenuFactory factory, ItemLike item) {
public static void register(MenuProviderFactory factory, ItemLike item) {
register(factory, () -> item);
}

public static Set<Item> validItems() {
return ImmutableSet.copyOf(FACTORIES.keySet());
}

@Nullable
public static MenuFactory get(ItemStack stack) {
public static Optional<MenuProviderFactory> get(ItemStack stack) {
if(stack.isEmpty())
return null;
return Optional.empty();

return FACTORIES.get(stack.getItem());
return Optional.ofNullable(FACTORIES.get(stack.getItem()));
}

@FunctionalInterface
public interface MenuFactory {
@Nullable AbstractContainerMenu create(int containerId, Inventory inventory, Player player);
public interface MenuProviderFactory {
@Nullable MenuProvider create(Level level, BlockPos pos, Player player, LecternBlockEntity blockEntity, ItemStack stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import net.minecraft.core.BlockPos;
import net.minecraft.world.Clearable;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand All @@ -16,8 +13,6 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(LecternBlockEntity.class)
public abstract class LecternBlockEntityMixin extends BlockEntity implements Clearable, MenuProvider {
Expand All @@ -29,13 +24,4 @@ public abstract class LecternBlockEntityMixin extends BlockEntity implements Cle
private boolean allItemsAreBooks(boolean original) {
return original || LecternMenuRegistry.validItems().contains(getBook().getItem());
}

@Inject(method = "createMenu", at = @At("HEAD"), cancellable = true)
private void applyCustomLecternMenus(int containerId, Inventory inventory, Player player, CallbackInfoReturnable<AbstractContainerMenu> info) {
LecternMenuRegistry.MenuFactory factory = LecternMenuRegistry.get(getBook());

System.out.println(factory);
if(factory != null)
info.setReturnValue(factory.create(containerId, inventory, player));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.upcraft.sparkweave.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import dev.upcraft.sparkweave.event.LecternMenuRegistry;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.LecternBlock;
import net.minecraft.world.level.block.entity.LecternBlockEntity;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

import java.util.OptionalInt;

@Mixin(LecternBlock.class)
public class LecternBlockMixin {

@WrapOperation(method = "openScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;openMenu(Lnet/minecraft/world/MenuProvider;)Ljava/util/OptionalInt;"))
private OptionalInt customMenuProvider(Player instance, MenuProvider menu, Operation<OptionalInt> original) {
if (menu instanceof LecternBlockEntity lectern) {
var provider = LecternMenuRegistry.get(lectern.getBook()).map(factory -> sparkweave$createProvider(factory, instance, lectern)).orElse(menu);
return original.call(instance, provider);
}

return original.call(instance, menu);
}

@Unique
@Nullable
private static MenuProvider sparkweave$createProvider(LecternMenuRegistry.MenuProviderFactory factory, Player player, LecternBlockEntity lectern) {
return factory.create(player.level(), lectern.getBlockPos(), player, lectern, lectern.getBook());
}
}
35 changes: 18 additions & 17 deletions Common/src/main/resources/sparkweave.mixins.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"required": true,
"minVersion": "0.8",
"package": "dev.upcraft.sparkweave.mixin",
"plugin": "dev.upcraft.sparkweave.SparkweaveMixinConfigPlugin",
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_21",
"mixins": [
"BlockEntityMixin",
"LecternBlockEntityMixin"
],
"client": [
"client.LecternRendererMixin",
"client.MainMixin",
"client.RenderLayerMixin"
],
"injectors": {
"defaultRequire": 1
"required": true,
"minVersion": "0.8",
"package": "dev.upcraft.sparkweave.mixin",
"plugin": "dev.upcraft.sparkweave.SparkweaveMixinConfigPlugin",
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_21",
"mixins": [
"BlockEntityMixin",
"LecternBlockEntityMixin",
"LecternBlockMixin"
],
"client": [
"client.LecternRendererMixin",
"client.MainMixin",
"client.RenderLayerMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 5404350

Please sign in to comment.