Leaderboard
Popular Content
Showing content with the highest reputation on 05/12/24 in all areas
-
1 point
-
There are a ton of guides, and I felt like I just needed to make one of how I mod using 1.12.2. Table of Contents: 1. Getting Started 2. Registries 3. Proxies 3. Basic Block/Item 4. GUIs (There will be more coming, I am writing this as I learn.) Getting Started: Download Forge mdk Unzip it Run gradlew setupDecompWorkspace for Eclipse: Run gradlew eclipse in cmd.exe for Intellij: Run gradlew setupDevWorkspace ideaModule Import build.gradle as a project Go to File -> Project Structure -> Modules Click '+' and click import, choose (folder name).iml. (There is only one in the folder) Close intellij run gradlew genIntellijruns Open Intellij Run -> Profile -> Edit Configurations Choose server/client Set "Use classpath of module" to (folder name)_main Apply Registries: Registries are Forge's way of injecting instances of your classes into the game. Basic setup: You need a class annotated with EventBusSubscriber From there you add methods annotated with EventHandler to register blocks. Example: @EventHandler public static void registerBlocks(RegistryEvent.Register<Block> event) { event.getRegistry().registerAll(blocks); } blocks is a List of all blocks in the mod. Note: When registering blocks, you also have to register an ItemBlock. To do this use the Item registry event, and register a new instance of ItemBlock(you have to set its registry name). Registering models: (This may need to be client side only, if it is please tell me) @SubscribeEvent public static void registerModels(ModelRegistryEvent event) { for (Block block : blocks) { if (block.getRegistryName() != null) ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0, new ModelResourceLocation(block.getRegistryName(),"inventory")); } } Pretty simple, nothing too hard. ObjectHolder: What is ObjectHolder? ObjectHolder is used to get instances of your block. Use: @ObjectHolder("modid") public class ModBlocks { @ObjectHolder("mod_block") public static final Block MOD_BLOCK = null; After the block is registered, an instance of it will be injected into MOD_BLOCK. Proxies: Minecraft runs on 2 sides. Client and server. Certain things are only client side, some are only server side. Setting up proxies: Proxies require 2 classes, Client and Server. The SidedProxy annotation requires 2 arguments, the path to your client and server proxy (ex. "com.author.mod.proxies.ClientProxy") Then you can use the SidedProxy annotation with the argument saying what side it is to tell Forge to run that code on a certain side. If you want to check what side a method is, use world.isRemote(), true means client side, false means server side. Blocks/Items: This is pretty simple, just make a class extend Block/Item, put in the necessary methods and constructors (setRegistryName() is a big one). Then register an instance of that block/item in the appropriate registry. When making a texture/model, you need to have the appropriate files. (BlockState for a block, block model for blocks, item model for blocks and items, and textures) I suggest looking at the default MC asset files when making textures. I won't get too much into detail here because it's not that hard to do. GUIs: GUIs are a bit harder. A GUI can either be a container, or a screen. A GUI requires 2-3 classes bare minimum, depending on what type it is. Simple screen: Create a class that implements IGuiHandler. That class needs 2 overrided methods. Example: @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { return new Container(player.inventory, world); } @Override public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { return new Gui(new Container(player.inventory, world)); } Now you gotta register it in your main class's init method: NetworkRegistry.INSTANCE.registerGuiHandler(instance, new WorkbenchGuiHandler()); (instance is the instance of your main class) Note: the instance should be obtained by making a variable and annotating it with the Instance annotation, and your modid as an argument. Now, we have to make the actual GUI. For my example I am going to be making a custom crafting table. Here is the GUI screen with the crafting table background: GuiScreen(Container inventorySlotsIn) { super(inventorySlotsIn); } //This is for everything behind the items @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); //Makes the background darker like in a normal craftring table this.drawDefaultBackground(); //Size of the GUI image int i = (this.width - this.xSize) / 2; int j = (this.height - this.ySize) / 2; //Load the default crafting GUI texture this.mc.getTextureManager().bindTexture(new ResourceLocation("textures/gui/container/crafting_table.png")); //Draw it this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize); } //Now for on top of the items @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { //Draw the name at the top, getting the name from the lang file. this.fontRenderer.drawString(I18n.format("gui.custom_workbench.name"), 28, 6, 4210752); } Now we need a container to contain all of the items. the container has to draw the player's inventory, the inventory, listen for click events, etc... just saying, this one's a big class. Commented code is code for making the inventory a crafting table public class GUIContainer extends Container { private World world; private InventoryCrafting matrix; private InventoryCraftResult result; private EntityPlayer player; GUIContainer(InventoryPlayer playerInventory, World world) { //matrix = new InventoryCrafting(this, 3, 3); player = playerInventory.player; this.world = world; //result = new InventoryCraftResult(); /* int index = 1; Output slot, code down below. addSlotToContainer(new CraftResultSlot(matrix, 0, 124, 35)); Crafting matrix for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { addSlotToContainer(new Slot(matrix, index, 30 + j * 18, 17 + i * 18)); index += 1; } } */ //Player's inventory for (int k = 0; k < 3; k++) { for (int i = 0; i < 9; i++) { addSlotToContainer(new Slot(playerInventory, index, 8 + i * 18, 84 + k * 18)); index += 1; } } //Player's hotbar for (int i = 0; i < 9; i++) { addSlotToContainer(new Slot(playerInventory, i, 8 + i * 18, 142)); } } //Checking if player can open the inventory @Override @ParametersAreNonnullByDefault public boolean canInteractWith(EntityPlayer playerIn) { /* Code to check if it is a custom crafting table Getting the block the player is looking at, with a max distance of 6 RayTraceResult ray = playerIn.rayTrace(6, 1.0f); if (ray == null) return false; //Get the position of the block BlockPos pos = ray.getBlockPos(); //Get the world World world = playerIn.getEntityWorld(); //And the block Block block = world.getBlockState(pos).getBlock(); //Check if it is our block return block.equals(ModBlocks.CUSTOM_WORKBENCH); */ return true; } @Override public void onContainerClosed(EntityPlayer playerIn) { super.onContainerClosed(playerIn); /* drop the items in the crafting grid if (!world.isRemote) { for (int i = 0; i < 9; ++i) { ItemStack itemstack = matrix.removeStackFromSlot(i); if (!itemstack.isEmpty()) { playerIn.dropItem(itemstack, false); } } } */ } //Code when item is shift clicked @Override @Nonnull public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) { ItemStack itemstack = ItemStack.EMPTY; Slot slot = inventorySlots.get(index); if (slot != null && slot.getHasStack()) { ItemStack currentItem = slot.getStack(); itemstack = currentItem.copy(); /* if (index == 0) { currentItem.getItem().onCreated(currentItem, world, playerIn); if (!mergeItemStack(currentItem, 37, 46, false)) { return ItemStack.EMPTY; } onPickupFromSlot(recipe); slot.onSlotChange(currentItem, itemstack); } */ if (index >= 11 && index < 38) { //if (!mergeItemStack(currentItem, 37, 46, false)) { //if (recipe != null) //Put correct output in output slot //putStackInSlot(0, recipe.output); //return ItemStack.EMPTY; //} //This might not work! if(!mergeItemStack(currentItem, 0, 8, false)) { return ItemStack.EMPTY; } } //else if (index >= 38 && index < 46) { //if (!mergeItemStack(currentItem, 10, 37, false)) { //return ItemStack.EMPTY; //} //} else if (!mergeItemStack(currentItem, 10, 46, false)) { //return ItemStack.EMPTY; //} if (currentItem.isEmpty()) { slot.putStack(ItemStack.EMPTY); } else { slot.onSlotChanged(); } if (currentItem.getCount() == itemstack.getCount()) { return ItemStack.EMPTY; } slot.onTake(playerIn, currentItem); } return itemstack; } @Override public boolean canMergeSlot(ItemStack stack, Slot slotIn) { return /*slotIn.inventory != result && */super.canMergeSlot(stack, slotIn); } /* @Override public void onCraftMatrixChanged(IInventory in) { IRecipe recipe = CraftingManager.findMatchingRecipe(matrix, world); if (recipe != null) putStackInSlot(0, recipe.getRecipeOutput()); } */ } If you want the CraftResultSlot code, here it is: public class CraftResultSlot extends Slot { public CraftResultSlot(InventoryCrafting matrix, int index, int x, int y) { super(matrix, index, x, y); } //Disable items getting put in the slot @Override public boolean isItemValid(ItemStack stack) { return false; } } Please comment anything i missed, things I should add, errors, etc... I will expand this soon!1 point