Skip to content

Commit

Permalink
add menus
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Jan 14, 2024
1 parent b34bc94 commit 9a17b95
Show file tree
Hide file tree
Showing 19 changed files with 166 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public String getName() {
return "Ender IO Data (" + modid + ")";
}


void onGatherData(GatherDataEvent event) {
if (event.includeServer()) {
for (TriFunction<PackOutput, ExistingFileHelper, CompletableFuture<HolderLookup.Provider>, DataProvider> function : this.serverSubProviderConsumers) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/example/regilite/events/ColorEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public Items(EnderItemRegistry items) {
public void registerItemColor(RegisterColorHandlersEvent.Item event) {
for (DeferredHolder<Item, ? extends Item> item : items.getEntries()) {
if (item instanceof EnderDeferredItem) {
Supplier<ItemColor> colorSupplier = ((EnderDeferredItem<Item>) item).getColorSupplier();
IItemColor colorSupplier = ((EnderDeferredItem<Item>) item).getColorSupplier();
if (colorSupplier != null)
event.register(colorSupplier.get(), item.get());
event.register(colorSupplier::getColor, item.get());
}
}
}
Expand All @@ -40,9 +40,9 @@ public Blocks(EnderBlockRegistry blocks) {
public void registerBlockColor(RegisterColorHandlersEvent.Block event) {
for (DeferredHolder<Block, ? extends Block> block : blocks.getEntries()) {
if (block instanceof EnderDeferredBlock) {
Supplier<BlockColor> colorSupplier = ((EnderDeferredBlock<Block>) block).getColorSupplier();
IBlockColor colorSupplier = ((EnderDeferredBlock<Block>) block).getColorSupplier();
if (colorSupplier != null)
event.register(colorSupplier.get(), block.get());
event.register(colorSupplier::getColor, block.get());
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/example/regilite/events/IBlockColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.regilite.events;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;

import javax.annotation.Nullable;

public interface IBlockColor {
int getColor(BlockState state, @Nullable BlockAndTintGetter getter, @Nullable BlockPos pos, int layer);
}
7 changes: 7 additions & 0 deletions src/main/java/com/example/regilite/events/IItemColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.regilite.events;

import net.minecraft.world.item.ItemStack;

public interface IItemColor {
int getColor(ItemStack stack, int layer);
}
11 changes: 11 additions & 0 deletions src/main/java/com/example/regilite/events/IScreenConstructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.regilite.events;

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;

public interface IScreenConstructor<T extends AbstractContainerMenu, U extends Screen & MenuAccess<T>> {
U create(T p_96215_, Inventory p_96216_, Component p_96217_);
}
4 changes: 2 additions & 2 deletions src/main/java/com/example/regilite/events/ScreenEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public ScreenEvents(EnderMenuRegistry registry) {
public <T extends AbstractContainerMenu> void genericScreenEvent(FMLClientSetupEvent event) {
for (DeferredHolder<MenuType<?>, ? extends MenuType<?>> menu : registry.getEntries()) {
if (menu instanceof EnderDeferredMenu) {
Supplier<? extends MenuScreens.ScreenConstructor<T, ? extends AbstractContainerScreen<T>>> screen = ((EnderDeferredMenu<T>) menu).getScreenConstructor();
IScreenConstructor<T, ? extends AbstractContainerScreen<T>> screen = ((EnderDeferredMenu<T>) menu).getScreenConstructor();
if (screen != null) {
event.enqueueWork(
() -> MenuScreens.register(((EnderDeferredMenu<T>) menu).get(), screen.get())
() -> MenuScreens.register(((EnderDeferredMenu<T>) menu).get(), screen::create)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.regilite.data.EnderBlockLootProvider;
import com.example.regilite.data.EnderDataProvider;
import com.example.regilite.events.IBlockColor;
import net.minecraft.client.color.block.BlockColor;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -29,7 +30,7 @@ public class EnderDeferredBlock<T extends Block> extends DeferredBlock<T> implem
@Nullable
private BiConsumer<BlockStateProvider, T> blockStateProvider = BlockStateProvider::simpleBlock;
@Nullable
private Supplier<BlockColor> colorSupplier;
private IBlockColor colorSupplier;
protected EnderDeferredBlock(ResourceKey<Block> key) {
super(key);
EnderDataProvider.getInstance(getId().getNamespace()).addTranslation(supplier, StringUtils.capitalize(getId().getPath().replace('_', ' ')));
Expand Down Expand Up @@ -71,11 +72,11 @@ public BiConsumer<BlockStateProvider, T> getBlockStateProvider() {
}

@Nullable
public Supplier<BlockColor> getColorSupplier() {
public IBlockColor getColorSupplier() {
return colorSupplier;
}

public EnderDeferredBlock<T> setColorSupplier(@Nullable Supplier<BlockColor> colorSupplier) {
public EnderDeferredBlock<T> setColorSupplier(@Nullable IBlockColor colorSupplier) {
this.colorSupplier = colorSupplier;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.regilite.data.EnderDataProvider;
import com.example.regilite.data.EnderItemModelProvider;
import com.example.regilite.events.IItemColor;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -31,7 +32,7 @@ public class EnderDeferredItem<T extends Item> extends DeferredItem<T> implement
protected Map<ResourceKey<CreativeModeTab>, Consumer<CreativeModeTab.Output>> tab = new HashMap<>();
@Nullable
protected BiConsumer<EnderItemModelProvider, T> modelProvider = EnderItemModelProvider::basicItem;
protected Supplier<ItemColor> colorSupplier;
protected IItemColor colorSupplier;

protected EnderDeferredItem(ResourceKey<Item> key) {
super(key);
Expand Down Expand Up @@ -81,11 +82,11 @@ public BiConsumer<EnderItemModelProvider, T> getModelProvider() {
return modelProvider;
}

public Supplier<ItemColor> getColorSupplier() {
public IItemColor getColorSupplier() {
return colorSupplier;
}

public EnderDeferredItem<T> setColorSupplier(Supplier<ItemColor> colorSupplier) {
public EnderDeferredItem<T> setColorSupplier(IItemColor colorSupplier) {
this.colorSupplier = colorSupplier;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.regilite.registry;

import com.example.regilite.events.IScreenConstructor;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.resources.ResourceKey;
Expand All @@ -11,18 +12,18 @@

public class EnderDeferredMenu<T extends AbstractContainerMenu> extends DeferredHolder<MenuType<? extends AbstractContainerMenu>, MenuType<T>> {

private Supplier<MenuScreens.ScreenConstructor<T, ? extends AbstractContainerScreen<T>>> screenConstructor;
private IScreenConstructor<T, ? extends AbstractContainerScreen<T>> screenConstructor;

protected EnderDeferredMenu(ResourceKey<MenuType<? extends AbstractContainerMenu>> key) {
super(key);
}

public EnderDeferredMenu<T> setScreenConstructor(Supplier<MenuScreens.ScreenConstructor<T, ? extends AbstractContainerScreen<T>>> screenConstructor) {
public EnderDeferredMenu<T> setScreenConstructor(IScreenConstructor<T, ? extends AbstractContainerScreen<T>> screenConstructor) {
this.screenConstructor = screenConstructor;
return this;
}

public Supplier<MenuScreens.ScreenConstructor<T, ? extends AbstractContainerScreen<T>>> getScreenConstructor() {
public IScreenConstructor<T, ? extends AbstractContainerScreen<T>> getScreenConstructor() {
return screenConstructor;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.regilite.registry;

import com.example.regilite.events.IScreenConstructor;
import com.example.regilite.events.ScreenEvents;
import com.example.regilite.mixin.DeferredRegisterAccessor;
import net.minecraft.client.gui.screens.MenuScreens;
Expand All @@ -13,6 +14,7 @@
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.network.IContainerFactory;
import net.neoforged.neoforge.registries.DeferredRegister;

import java.util.Objects;
Expand Down Expand Up @@ -46,14 +48,18 @@ public <I extends AbstractContainerMenu> EnderDeferredMenu<I> registerMenu(Strin
return registerMenu(name, key -> sup.get());
}

public <I extends AbstractContainerMenu> EnderDeferredMenu<I> registerMenu(String name, MenuType.MenuSupplier<I> sup) {
public <I extends AbstractContainerMenu> EnderDeferredMenu<I> registerMenu(String name, IContainerFactory<I> sup) {
return registerMenu(name, () -> new MenuType<>(sup, FeatureFlags.DEFAULT_FLAGS));
}

public <I extends AbstractContainerMenu> EnderDeferredMenu<I> registerMenu(String name, MenuType.MenuSupplier<I> sup, Supplier<MenuScreens.ScreenConstructor<I, ? extends AbstractContainerScreen<I>>> screen) {
public <I extends AbstractContainerMenu> EnderDeferredMenu<I> registerMenu(String name, IContainerFactory<I> sup, IScreenConstructor<I, ? extends AbstractContainerScreen<I>> screen) {
return registerMenu(name, sup).setScreenConstructor(screen);
}

public static EnderMenuRegistry createRegistry(String modid) {
return new EnderMenuRegistry(modid);
}

@Override
public void register(IEventBus bus) {
super.register(bus);
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/example/examplemod/Blocks.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.examplemod;

import com.example.examplemod.exampleclasses.ExampleBlock;
import com.example.examplemod.exampleclasses.ExampleColors;
import com.example.regilite.data.EnderBlockLootProvider;
import com.example.regilite.data.EnderItemModelProvider;
import com.example.regilite.registry.EnderBlockRegistry;
Expand All @@ -14,6 +15,8 @@
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.model.generators.BlockStateProvider;

import java.util.function.Supplier;

public class Blocks {
public static final EnderBlockRegistry BLOCKS = EnderBlockRegistry.createRegistry(ExampleMod.MODID);
public static final EnderItemRegistry ITEMS = EnderItemRegistry.createRegistry(ExampleMod.MODID);
Expand All @@ -22,6 +25,7 @@ public class Blocks {
.registerBlock("example_block", ExampleBlock::new, BlockBehaviour.Properties.of().mapColor(MapColor.STONE))
.addBlockTags(BlockTags.MUSHROOM_GROW_BLOCK, BlockTags.LOGS)
.setTranslation("Test Example Block")
.setColorSupplier(ExampleColors.BLOCK)
.setBlockStateProvider(BlockStateProvider::simpleBlock)
.setLootTable(EnderBlockLootProvider::dropSelf)
.createBlockItem(ITEMS)
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/example/examplemod/ExampleMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public ExampleMod(IEventBus modEventBus) {
CreativeTabs.register(modEventBus);
Fluids.register(modEventBus);
BlockEntities.register(modEventBus);
Menus.register(modEventBus);
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/example/examplemod/Items.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.examplemod;

import com.example.examplemod.exampleclasses.ExampleColors;
import com.example.regilite.registry.EnderDeferredItem;
import com.example.regilite.registry.EnderItemRegistry;
import net.minecraft.tags.ItemTags;
Expand All @@ -14,6 +15,7 @@ public class Items {
.alwaysEat().nutrition(1).saturationMod(2f).build()))
.addItemTags(ItemTags.WOOL)
.setTranslation("Test Example Item")
.setColorSupplier(ExampleColors.ITEM)
.setTab(null);

public static void register(IEventBus modEventBus) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/example/examplemod/Menus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.examplemod;

import com.example.examplemod.exampleclasses.ExampleMenu;
import com.example.examplemod.exampleclasses.ExampleScreen;
import com.example.regilite.registry.EnderDeferredMenu;
import com.example.regilite.registry.EnderMenuRegistry;
import net.neoforged.bus.api.IEventBus;

public class Menus {

private static final EnderMenuRegistry MENUS = EnderMenuRegistry.createRegistry(ExampleMod.MODID);

public static final EnderDeferredMenu<ExampleMenu> EXAMPLE_MENU = MENUS.registerMenu("example", ExampleMenu::new, ExampleScreen::new);

public static void register(IEventBus modbus) {
MENUS.register(modbus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import com.example.examplemod.BlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

public class ExampleBlock extends Block implements EntityBlock {
Expand All @@ -18,4 +26,23 @@ public ExampleBlock(Properties p_49795_) {
public BlockEntity newBlockEntity(BlockPos p_153215_, BlockState p_153216_) {
return BlockEntities.EXAMPLE_BLOCKENTITY.create(p_153215_, p_153216_);
}

@Override
public InteractionResult use(BlockState p_60503_, Level p_60504_, BlockPos p_60505_, Player p_60506_, InteractionHand p_60507_, BlockHitResult p_60508_) {
BlockEntity be = p_60504_.getBlockEntity(p_60505_);
if (be instanceof ExampleBlockentity exampleBlockentity) {
MenuProvider menuprovider = this.getMenuProvider(p_60503_, p_60504_, p_60505_);
if (menuprovider != null && p_60506_ instanceof ServerPlayer serverPlayer) {
NetworkHooks.openScreen(serverPlayer, menuprovider, buf -> buf.writeBlockPos(p_60505_));
}
}
return super.use(p_60503_, p_60504_, p_60505_, p_60506_, p_60507_, p_60508_);
}

@Nullable
@Override
public MenuProvider getMenuProvider(BlockState p_49234_, Level p_49235_, BlockPos p_49236_) {
BlockEntity blockentity = p_49235_.getBlockEntity(p_49236_);
return blockentity instanceof MenuProvider ? (MenuProvider)blockentity : null;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
package com.example.examplemod.exampleclasses;

import com.example.examplemod.BlockEntities;
import com.example.examplemod.Menus;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
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.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

public class ExampleBlockentity extends BlockEntity {
public class ExampleBlockentity extends BlockEntity implements MenuProvider {
public ExampleBlockentity(BlockPos p_155229_, BlockState p_155230_) {
super(BlockEntities.EXAMPLE_BLOCKENTITY.get(), p_155229_, p_155230_);
}

@Override
public Component getDisplayName() {
return Component.translatable("test");
}

@Nullable
@Override
public AbstractContainerMenu createMenu(int p_39954_, Inventory p_39955_, Player p_39956_) {
return new ExampleMenu(this, p_39954_, p_39955_);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package com.example.examplemod.exampleclasses;

import com.example.examplemod.Blocks;
import com.example.regilite.events.IBlockColor;
import com.example.regilite.events.IItemColor;
import net.minecraft.client.color.block.BlockColor;
import net.minecraft.client.color.item.ItemColor;

import java.util.function.Supplier;

public class ExampleColors {

public static IBlockColor BLOCK = (state, getter, pos, layer) -> 0;

public static IItemColor ITEM = (stack, layer) -> 0;

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.example.examplemod.exampleclasses;

import com.example.examplemod.Menus;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

public class ExampleMenu extends AbstractContainerMenu {
protected ExampleMenu(@Nullable MenuType<?> p_38851_, int p_38852_) {
super(p_38851_, p_38852_);
public ExampleMenu(int windowId, Inventory inv, FriendlyByteBuf data) {
super(Menus.EXAMPLE_MENU.get(), windowId);
}

public ExampleMenu(ExampleBlockentity be, int windowId, Inventory inv) {
super(Menus.EXAMPLE_MENU.get(), windowId);
}

@Override
Expand All @@ -18,6 +25,6 @@ public ItemStack quickMoveStack(Player p_38941_, int p_38942_) {

@Override
public boolean stillValid(Player p_38874_) {
return false;
return true;
}
}
Loading

0 comments on commit 9a17b95

Please sign in to comment.