feat: implement very simple crafting output slot
Note that this is likely full of bugs that we'll find later on, but this should be good enough to merge for now as a simple example.
This commit is contained in:
1 parent
b888aa5a78
commit
430c59aed4
4 files changed
+78
-12
No files matched your search
+4
-4
@@ -1,5 +1,6 @@
|
|||||||
package com.uncreativebunch.magicmod.block.entity;
|
package com.uncreativebunch.magicmod.block.entity;
|
||||||
|
|
||||||
|
import com.uncreativebunch.magicmod.inventory.InputInventory;
|
||||||
import com.uncreativebunch.magicmod.inventory.OutputInventory;
|
import com.uncreativebunch.magicmod.inventory.OutputInventory;
|
||||||
import com.uncreativebunch.magicmod.recipe.MagicShapedInput;
|
import com.uncreativebunch.magicmod.recipe.MagicShapedInput;
|
||||||
import com.uncreativebunch.magicmod.recipe.ModRecipeTypes;
|
import com.uncreativebunch.magicmod.recipe.ModRecipeTypes;
|
||||||
@@ -9,7 +10,6 @@ import net.minecraft.block.BlockState;
|
|||||||
import net.minecraft.block.entity.BlockEntity;
|
import net.minecraft.block.entity.BlockEntity;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
import net.minecraft.inventory.SimpleInventory;
|
|
||||||
import net.minecraft.recipe.RecipeEntry;
|
import net.minecraft.recipe.RecipeEntry;
|
||||||
import net.minecraft.recipe.ServerRecipeManager;
|
import net.minecraft.recipe.ServerRecipeManager;
|
||||||
import net.minecraft.screen.NamedScreenHandlerFactory;
|
import net.minecraft.screen.NamedScreenHandlerFactory;
|
||||||
@@ -27,7 +27,7 @@ public class MagicalCrafterBlockEntity extends BlockEntity implements NamedScree
|
|||||||
/**
|
/**
|
||||||
* The input inventory for crafting ingredients. It goes in the order core, material, and grip.
|
* The input inventory for crafting ingredients. It goes in the order core, material, and grip.
|
||||||
*/
|
*/
|
||||||
private final SimpleInventory inputInventory = new SimpleInventory(3);
|
private final InputInventory inputInventory = new InputInventory(3);
|
||||||
/**
|
/**
|
||||||
* The output inventory for crafting ingredients.
|
* The output inventory for crafting ingredients.
|
||||||
*/
|
*/
|
||||||
@@ -40,9 +40,9 @@ public class MagicalCrafterBlockEntity extends BlockEntity implements NamedScree
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the input inventory for this block entity.
|
* Gets the input inventory for this block entity.
|
||||||
* @return A {@link SimpleInventory} that represents the input inventory.
|
* @return A {@link InputInventory} that represents the input inventory.
|
||||||
*/
|
*/
|
||||||
public SimpleInventory getInputInventory() { return inputInventory; }
|
public InputInventory getInputInventory() { return inputInventory; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the output inventory for this block entity.
|
* Gets the output inventory for this block entity.
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.uncreativebunch.magicmod.inventory;
|
||||||
|
|
||||||
|
import net.minecraft.inventory.SimpleInventory;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
public class InputInventory extends SimpleInventory {
|
||||||
|
public InputInventory(int size) {
|
||||||
|
super(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canInsert(ItemStack stack) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes basic crafting logic and decrements all input stacks.
|
||||||
|
*/
|
||||||
|
public void onCrafted() {
|
||||||
|
for (var stack : this) {
|
||||||
|
stack.decrement(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package com.uncreativebunch.magicmod.screen;
|
||||||
|
|
||||||
|
import com.uncreativebunch.magicmod.inventory.InputInventory;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.inventory.Inventory;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.screen.slot.Slot;
|
||||||
|
|
||||||
|
public class MagicResultSlot extends Slot {
|
||||||
|
/**
|
||||||
|
* The crafting input inventory. There is not a current need for custom logic, but this may come in the future.
|
||||||
|
*/
|
||||||
|
protected InputInventory inputInventory;
|
||||||
|
|
||||||
|
public MagicResultSlot(InputInventory inputInventory, Inventory outputInventory, int index, int x, int y) {
|
||||||
|
super(outputInventory, index, x, y);
|
||||||
|
this.inputInventory = inputInventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs when the player takes the item out of the slot.
|
||||||
|
* This slot will run the crafting logic of the {@link InputInventory}.
|
||||||
|
* @param player The player that took the item.
|
||||||
|
* @param stack The item that was taken.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onTakeItem(PlayerEntity player, ItemStack stack) {
|
||||||
|
this.inputInventory.onCrafted();
|
||||||
|
|
||||||
|
super.onTakeItem(player, stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns false, as the result slot does not allow items to be inserted.
|
||||||
|
* @param stack The item to insert.
|
||||||
|
* @return False.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean canInsert(ItemStack stack) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +1,16 @@
|
|||||||
package com.uncreativebunch.magicmod.screen;
|
package com.uncreativebunch.magicmod.screen;
|
||||||
|
|
||||||
|
import com.uncreativebunch.magicmod.inventory.InputInventory;
|
||||||
import com.uncreativebunch.magicmod.inventory.OutputInventory;
|
import com.uncreativebunch.magicmod.inventory.OutputInventory;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
import net.minecraft.inventory.CraftingInventory;
|
|
||||||
import net.minecraft.inventory.SimpleInventory;
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.screen.ScreenHandler;
|
import net.minecraft.screen.ScreenHandler;
|
||||||
import net.minecraft.screen.ScreenHandlerContext;
|
|
||||||
import net.minecraft.screen.slot.Slot;
|
import net.minecraft.screen.slot.Slot;
|
||||||
|
|
||||||
public class MagicalCraftingScreenHandler extends ScreenHandler {
|
public class MagicalCraftingScreenHandler extends ScreenHandler {
|
||||||
|
|
||||||
private final SimpleInventory inputInventory;
|
private final InputInventory inputInventory;
|
||||||
private final OutputInventory outputInventory;
|
private final OutputInventory outputInventory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,7 +19,7 @@ public class MagicalCraftingScreenHandler extends ScreenHandler {
|
|||||||
* @param playerInventory The player's inventory.
|
* @param playerInventory The player's inventory.
|
||||||
*/
|
*/
|
||||||
public MagicalCraftingScreenHandler(int syncId, PlayerInventory playerInventory) {
|
public MagicalCraftingScreenHandler(int syncId, PlayerInventory playerInventory) {
|
||||||
this(syncId, playerInventory, new SimpleInventory(3), new OutputInventory(1));
|
this(syncId, playerInventory, new InputInventory(3), new OutputInventory(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,9 +27,10 @@ public class MagicalCraftingScreenHandler extends ScreenHandler {
|
|||||||
* It handles creating the inventory slots and syncs all information from the server to the client.
|
* It handles creating the inventory slots and syncs all information from the server to the client.
|
||||||
* @param syncId The sync ID that's sent from the server.
|
* @param syncId The sync ID that's sent from the server.
|
||||||
* @param playerInventory The player's inventory.
|
* @param playerInventory The player's inventory.
|
||||||
* @param screenHandlerContext The context of the screen handler. Not sure if this is needed anymore.
|
* @param inputInventory The crafting inventory.
|
||||||
|
* @param outputInventory The output inventory.
|
||||||
*/
|
*/
|
||||||
public MagicalCraftingScreenHandler(int syncId, PlayerInventory playerInventory, SimpleInventory inputInventory,
|
public MagicalCraftingScreenHandler(int syncId, PlayerInventory playerInventory, InputInventory inputInventory,
|
||||||
OutputInventory outputInventory) {
|
OutputInventory outputInventory) {
|
||||||
super(ModScreenHandlers.MAGICAL_CRAFTING_SCREEN_HANDLER, syncId);
|
super(ModScreenHandlers.MAGICAL_CRAFTING_SCREEN_HANDLER, syncId);
|
||||||
this.inputInventory = inputInventory;
|
this.inputInventory = inputInventory;
|
||||||
@@ -43,6 +42,7 @@ public class MagicalCraftingScreenHandler extends ScreenHandler {
|
|||||||
|
|
||||||
// Ensure that the inventory's custom logic runs (if any)
|
// Ensure that the inventory's custom logic runs (if any)
|
||||||
inputInventory.onOpen(playerInventory.player);
|
inputInventory.onOpen(playerInventory.player);
|
||||||
|
outputInventory.onOpen(playerInventory.player);
|
||||||
|
|
||||||
// Place all inventory slots within the screen. This piece doesn't actually render the background of the slots,
|
// Place all inventory slots within the screen. This piece doesn't actually render the background of the slots,
|
||||||
// as this is handled by the client-side application (the Screen), but it does tell the screen where to render.
|
// as this is handled by the client-side application (the Screen), but it does tell the screen where to render.
|
||||||
@@ -55,7 +55,7 @@ public class MagicalCraftingScreenHandler extends ScreenHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO I don't actually know where the output slot should be located. Get correct coords.
|
// TODO I don't actually know where the output slot should be located. Get correct coords.
|
||||||
this.addSlot(new Slot(outputInventory, 0, 80, 35));
|
this.addSlot(new MagicResultSlot(inputInventory, outputInventory, 0, 80, 35));
|
||||||
|
|
||||||
// Add all player inventory slots. Start with the main inventory, then the hotbar.
|
// Add all player inventory slots. Start with the main inventory, then the hotbar.
|
||||||
for (m = 0; m < 3; m++) {
|
for (m = 0; m < 3; m++) {
|
||||||
|
|||||||
Reference in new issue
Block a user