Skip to content

Instantly share code, notes, and snippets.

@MrKelpy
Created July 29, 2022 23:04
Show Gist options
  • Save MrKelpy/aba155b55ac2216332cc66c725c19586 to your computer and use it in GitHub Desktop.
Save MrKelpy/aba155b55ac2216332cc66c725c19586 to your computer and use it in GitHub Desktop.
What i have now
package com.mrkelpy.miasmapothecary.blocks.MixingTable;
import com.mrkelpy.miasmapothecary.blocks.MixingTable.MixingTableBlockEntity;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
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.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Objects;
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class MixingTableBlock extends Block implements EntityBlock {
private static final BlockBehaviour.Properties BLOCK_PROPERTIES = buildProperties();
private static final VoxelShape SHAPE = box(0.0D, 0.0D, 0.0D, 16.0D, 15.0D, 16.0D);
private static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public MixingTableBlock() {
super(BLOCK_PROPERTIES);
this.registerDefaultState(this.getStateDefinition().any().setValue(FACING, Direction.NORTH));
}
/**
* Builds the properties for the current block.
*/
private static BlockBehaviour.Properties buildProperties() {
BlockBehaviour.Properties properties = BlockBehaviour.Properties.of(Material.STONE);
properties.color(MaterialColor.COLOR_BROWN);
properties.strength(2.5f, 6f);
properties.requiresCorrectToolForDrops();
return properties;
}
@Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
if (pPlayer instanceof ServerPlayer)
MixingTableMenu.openContainer((ServerPlayer) pPlayer, pPos);
return InteractionResult.SUCCESS;
}
@SuppressWarnings("deprecation")
@Override
public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
return SHAPE;
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
return Objects.requireNonNull(super.getStateForPlacement(pContext)).setValue(FACING, pContext.getHorizontalDirection().getOpposite());
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
pBuilder.add(FACING);
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return new MixingTableBlockEntity(pPos, pState);
}
}
package com.mrkelpy.miasmapothecary.blocks.MixingTable;
import com.mrkelpy.miasmapothecary.setup.Registration;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.OnlyIn;
import javax.annotation.ParametersAreNonnullByDefault;
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class MixingTableBlockEntity extends BlockEntity {
public MixingTableBlockEntity(BlockPos pos, BlockState state) {
super(Registration.MIXING_TABLE_BLOCK_ENTITY.get(), pos, state);
}
}
package com.mrkelpy.miasmapothecary.blocks.MixingTable;
public class MixingTableBlockScreen {
}
package com.mrkelpy.miasmapothecary.blocks.MixingTable;
import com.mrkelpy.miasmapothecary.setup.Registration;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraftforge.network.NetworkHooks;
import javax.annotation.ParametersAreNonnullByDefault;
/**
* This class implements the Menu for the MixingTableMenu. It is used to tell the server and client
* how the inventory works in that GUI
*/
@ParametersAreNonnullByDefault
public class MixingTableMenu extends AbstractContainerMenu {
/** The ContainerLevelAccess is just a pair of a Level and the BlockPos within the context.
*/
private final ContainerLevelAccess CLA;
public MixingTableMenu(int containerId, ContainerLevelAccess CLA) {
super(Registration.MIXING_TABLE_CONTAINER.get(), containerId);
this.CLA = CLA;
}
/**
* This is an utilitary method that opens the GUI for the specified player.
* @param player The player to the display the GUI to
* @param blockPos The block position from where the GUI was opened from
*/
public static void openContainer(ServerPlayer player, BlockPos blockPos) {
NetworkHooks.openGui(player, new MixingTableMenuProvider(blockPos));
}
/**
* Checks whether the player is still allowed to access the container.
* @return boolean
*/
@Override
public boolean stillValid(Player pPlayer) {
return stillValid(CLA, pPlayer, Registration.MIXING_TABLE_BLOCK.get());
}
}
package com.mrkelpy.miasmapothecary.blocks.MixingTable;
import com.mrkelpy.miasmapothecary.MiasmaApothecary;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
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.inventory.ContainerLevelAccess;
import net.minecraft.world.level.block.Block;
import org.jetbrains.annotations.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class MixingTableMenuProvider implements MenuProvider {
private final BlockPos menuBlockPos;
public MixingTableMenuProvider(BlockPos blockPos) {
this.menuBlockPos = blockPos;
}
@Override
public Component getDisplayName() {
return new TranslatableComponent(String.format("container.%s.mixing_table", MiasmaApothecary.MODID));
}
@Nullable
@Override
public AbstractContainerMenu createMenu(int pContainerId, Inventory pInventory, Player pPlayer) {
return new MixingTableMenu(pContainerId, ContainerLevelAccess.create(pPlayer.getLevel(), this.menuBlockPos));
}
}
package com.mrkelpy.miasmapothecary.setup;
import com.mrkelpy.miasmapothecary.MiasmaApothecary;
import com.mrkelpy.miasmapothecary.blocks.MixingTable.MixingTableBlock;
import com.mrkelpy.miasmapothecary.blocks.MixingTable.MixingTableBlockEntity;
import com.mrkelpy.miasmapothecary.blocks.MixingTable.MixingTableMenu;
import com.mrkelpy.miasmapothecary.items.MixingTableBlockItem;
import com.mrkelpy.miasmapothecary.other.CreativeTabMiasmaApothecary;
import net.minecraft.core.Registry;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.network.IContainerFactory;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
/**
* This class is responsible for registering every object into the game.
*/
public class Registration {
/**
* DEFERRED REGISTERS
*/
public static final DeferredRegister<Item> ITEMS =
DeferredRegister.create(ForgeRegistries.ITEMS, MiasmaApothecary.MODID);
public static final DeferredRegister<Block> BLOCKS =
DeferredRegister.create(ForgeRegistries.BLOCKS, MiasmaApothecary.MODID);
public static final DeferredRegister<RecipeSerializer<?>> RECIPE_SERIALIZERS =
DeferredRegister.create(ForgeRegistries.RECIPE_SERIALIZERS, MiasmaApothecary.MODID);
public static final DeferredRegister<RecipeType<?>> RECIPE_TYPES =
DeferredRegister.create(Registry.RECIPE_TYPE_REGISTRY, MiasmaApothecary.MODID);
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES =
DeferredRegister.create(Registry.BLOCK_ENTITY_TYPE_REGISTRY, MiasmaApothecary.MODID);
public static final DeferredRegister<MenuType<?>> MENU_TYPES =
DeferredRegister.create(Registry.MENU_REGISTRY, MiasmaApothecary.MODID);
/**
* This is a helper method for container registration.
* Instead of registering the Menu itself, we register a MenuType pointing to the menu.
* @param id The registration ID
* @param factory The factory for the menu.
*/
private static <T extends AbstractContainerMenu> RegistryObject<MenuType<T>> registerContainer(String id, IContainerFactory<T> factory) {
return MENU_TYPES.register(id, () -> IForgeMenuType.create(factory));
}
/**
* BLOCKS REGISTRATION
*/
public static final RegistryObject<Block> MIXING_TABLE_BLOCK = BLOCKS.register("mixing_table_block", MixingTableBlock::new);
/**
* ITEMS REGISTRATION
*/
public static final RegistryObject<Item> MIXING_TABLE = ITEMS.register("mixing_table", MixingTableBlockItem::new);
/**
* CREATIVE TAB REGISTRATION
*/
public static final CreativeModeTab CustomModTab = new CreativeTabMiasmaApothecary();
/**
* BLOCK ENTITY TYPE REGISTRATION
*/
public static final RegistryObject<BlockEntityType<?>> MIXING_TABLE_BLOCK_ENTITY =
BLOCK_ENTITY_TYPES.register("mixing_table_block_entity", () -> BlockEntityType.Builder.of(MixingTableBlockEntity::new, MIXING_TABLE_BLOCK.get()).build(null));
/**
* MENU TYPE REGISTRATION
*/
public static final RegistryObject<MenuType<MixingTableMenu>> MIXING_TABLE_CONTAINER =
registerContainer("mixing_table", (id, inventory, buff) -> new MixingTableMenu(id, ContainerLevelAccess.create(inventory.player.getLevel(), buff.readBlockPos())));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment