Skip to content

Commit

Permalink
Initial Pocket Generator work, it saves items
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Feb 19, 2024
1 parent d61559a commit 97ab83a
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// 1.20.4 2024-02-17T22:37:21.9268029 Item Models: justdirethings
// 1.20.4 2024-02-18T21:20:13.5012113 Item Models: justdirethings
af62c31381c904f86fde9370e374e01d828fa643 assets/justdirethings/models/item/fuel_canister.json
3f50e98f22d6abc6b3edab6c01b82deec145ec78 assets/justdirethings/models/item/pocket_generator.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.20.4 2024-02-18T14:01:33.7129519 Languages: en_us for mod: justdirethings
1155dbc06ee9cdfaaa0dea410bbeb38a8ed5d4c3 assets/justdirethings/lang/en_us.json
// 1.20.4 2024-02-18T21:20:13.5032131 Languages: en_us for mod: justdirethings
cc984028f4791799fac03e23e3e9217d5d527b36 assets/justdirethings/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// 1.20.4 2024-02-17T22:37:21.9338085 Recipes
// 1.20.4 2024-02-18T21:20:13.5042139 Recipes
08a464b9106de5bf0e3d81194dc205ed42ef7db4 data/justdirethings/advancements/recipes/misc/fuel_canister.json
591f2dd8d997ccead841061e70aedc4a4984a60d data/justdirethings/advancements/recipes/misc/pocket_generator.json
be43fb76c8ff5e0964f378abece4cf4d77ecd06e data/justdirethings/recipes/fuel_canister.json
5b4bd1dbd73529cb2521bec1dbc1f1786fb47281 data/justdirethings/recipes/pocket_generator.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"item.justdirethings.fuel_canister": "Fuel Canister",
"item.justdirethings.pocket_generator": "Pocket Generator",
"itemGroup.DeferredHolder{ResourceKey[minecraft:creative_mode_tab / justdirethings:justdirethings]}": "Just Dire Things",
"justdirethings.fuelcanisteramt": "Cook time (ticks): %d",
"justdirethings.fuelcanisteramtstack": "Stack Cook time (ticks): %d",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "justdirethings:item/pocketgenerator"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"parent": "minecraft:recipes/root",
"criteria": {
"has_fuel_canister": {
"conditions": {
"items": [
{
"items": [
"justdirethings:fuel_canister"
]
}
]
},
"trigger": "minecraft:inventory_changed"
},
"has_the_recipe": {
"conditions": {
"recipe": "justdirethings:pocket_generator"
},
"trigger": "minecraft:recipe_unlocked"
}
},
"requirements": [
[
"has_the_recipe",
"has_fuel_canister"
]
],
"rewards": {
"recipes": [
"justdirethings:pocket_generator"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "minecraft:crafting_shaped",
"category": "misc",
"group": "justdirethings",
"key": {
"c": {
"item": "minecraft:coal"
},
"f": {
"item": "justdirethings:fuel_canister"
},
"i": {
"tag": "forge:ingots/iron"
},
"r": {
"tag": "forge:dusts/redstone"
}
},
"pattern": [
"ici",
"rfr",
"ici"
],
"result": {
"item": "justdirethings:pocket_generator"
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/direwolf20/justdirethings/JustDireThings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.direwolf20.justdirethings;

import com.direwolf20.justdirethings.common.items.PocketGenerator;
import com.direwolf20.justdirethings.setup.ClientSetup;
import com.direwolf20.justdirethings.setup.Config;
import com.direwolf20.justdirethings.setup.ModSetup;
Expand All @@ -8,6 +9,8 @@
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import org.slf4j.Logger;

// The value here should match an entry in the META-INF/mods.toml file
Expand All @@ -21,9 +24,19 @@ public JustDireThings(IEventBus modEventBus) {
Registration.init(modEventBus);
Config.register();
ModSetup.CREATIVE_MODE_TABS.register(modEventBus);

modEventBus.addListener(this::registerCapabilities);
if (FMLLoader.getDist().isClient()) {
modEventBus.addListener(ClientSetup::init);
}
}

private void registerCapabilities(RegisterCapabilitiesEvent event) {
event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> {
if (itemStack.getItem() instanceof PocketGenerator pocketGenerator)
return pocketGenerator.getStackHandler(itemStack);
return null;
},
Registration.Pocket_Generator.get()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.direwolf20.justdirethings.client.screens;

import com.direwolf20.justdirethings.JustDireThings;
import com.direwolf20.justdirethings.common.containers.PocketGeneratorContainer;
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;

public class PocketGeneratorScreen extends AbstractContainerScreen<PocketGeneratorContainer> {
private final ResourceLocation GUI = new ResourceLocation(JustDireThings.MODID, "textures/gui/pocketgenerator.png");

protected final PocketGeneratorContainer container;
private ItemStack pocketGenerator;

public PocketGeneratorScreen(PocketGeneratorContainer container, Inventory inv, Component name) {
super(container, inv, name);
this.container = container;
this.pocketGenerator = container.pocketGeneratorItemStack;
}

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
super.render(guiGraphics, mouseX, mouseY, partialTicks);
this.renderTooltip(guiGraphics, mouseX, mouseY);
/*MutableComponent msg = Component.translatable("justdirethings.fuelcanisteritemsamt", MagicHelpers.formatted((float) FuelCanister.getFuelLevel(pocketGenerator) / 200));
guiGraphics.drawString(font, msg, this.getGuiLeft() + this.imageWidth / 2 - font.width(msg) / 2, getGuiTop() + 5, Color.DARK_GRAY.getRGB(), false);
msg = Component.translatable("justdirethings.fuelcanisteramt", MagicHelpers.formatted(FuelCanister.getFuelLevel(pocketGenerator)));
guiGraphics.drawString(font, msg, (this.getGuiLeft() + this.imageWidth / 2) - font.width(msg) / 2, getGuiTop() + 15, Color.DARK_GRAY.getRGB(), false);*/
}

@Override
protected void renderTooltip(GuiGraphics pGuiGraphics, int pX, int pY) {
super.renderTooltip(pGuiGraphics, pX, pY);
/*if (this.menu.getCarried().isEmpty() && this.hoveredSlot != null && this.hoveredSlot.hasItem()) {
ItemStack itemstack = this.hoveredSlot.getItem();
int fuelPerPiece = CommonHooks.getBurnTime(itemstack, RecipeType.SMELTING);
List<Component> tooltip = this.getTooltipFromContainerItem(itemstack);
if (container.handler.isItemValid(0, itemstack)) {
if (hasShiftDown()) {
tooltip.add(Component.translatable("justdirethings.fuelcanisteramt", MagicHelpers.formatted(fuelPerPiece)).withStyle(ChatFormatting.AQUA));
tooltip.add(Component.translatable("justdirethings.fuelcanisteramtstack", MagicHelpers.formatted(fuelPerPiece * itemstack.getCount())).withStyle(ChatFormatting.AQUA));
} else {
tooltip.add(Component.translatable("justdirethings.fuelcanisteritemsamt", MagicHelpers.formatted((float) fuelPerPiece / 200)).withStyle(ChatFormatting.AQUA));
tooltip.add(Component.translatable("justdirethings.fuelcanisteritemsamtstack", MagicHelpers.formatted((float) (fuelPerPiece * itemstack.getCount()) / 200)).withStyle(ChatFormatting.AQUA));
}
}
pGuiGraphics.renderTooltip(this.font, tooltip, itemstack.getTooltipImage(), itemstack, pX, pY);
}*/
}

@Override
protected void renderSlot(GuiGraphics pGuiGraphics, Slot pSlot) {
super.renderSlot(pGuiGraphics, pSlot);
if (!pSlot.getItem().isEmpty() && !container.handler.isItemValid(pSlot.getSlotIndex(), pSlot.getItem()))
pGuiGraphics.fill(RenderType.guiOverlay(), pSlot.x, pSlot.y, pSlot.x + 16, pSlot.y + Mth.ceil(16.0F), 0x7FFF0000);
}

@Override
public void init() {
super.init();
}

@Override
protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
super.renderLabels(guiGraphics, mouseX, mouseY);
}

@Override
protected void renderBg(GuiGraphics guiGraphics, float partialTicks, int mouseX, int mouseY) {
RenderSystem.setShaderTexture(0, GUI);
int relX = (this.width - this.imageWidth) / 2;
int relY = (this.height - this.imageHeight) / 2;
guiGraphics.blit(GUI, relX, relY, 0, 0, this.imageWidth, this.imageHeight);
}

@Override
public boolean isPauseScreen() {
return false;
}

@Override
public void onClose() {
super.onClose();
}

@Override
public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) {
InputConstants.Key mouseKey = InputConstants.getKey(p_keyPressed_1_, p_keyPressed_2_);
if (p_keyPressed_1_ == 256 || minecraft.options.keyInventory.isActiveAndMatches(mouseKey)) {
onClose();

return true;
}

return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
}

@Override
public boolean mouseClicked(double x, double y, int btn) {
return super.mouseClicked(x, y, btn);
}

public boolean mouseReleased(double p_mouseReleased_1_, double p_mouseReleased_3_, int p_mouseReleased_5_) {
return super.mouseReleased(p_mouseReleased_1_, p_mouseReleased_3_, p_mouseReleased_5_);
}

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double pScrollX, double pScrollY) {
return super.mouseScrolled(mouseX, mouseY, pScrollX, pScrollY);
}

private static MutableComponent getTrans(String key, Object... args) {
return Component.translatable(JustDireThings.MODID + "." + key, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.direwolf20.justdirethings.common.containers;

import com.direwolf20.justdirethings.common.items.PocketGenerator;
import com.direwolf20.justdirethings.setup.Registration;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.ItemStackHandler;

public class PocketGeneratorContainer extends BaseContainer {
public static final int SLOTS = 1;
public ItemStackHandler handler;
public ItemStack pocketGeneratorItemStack;
public Player playerEntity;

public PocketGeneratorContainer(int windowId, Inventory playerInventory, Player player, FriendlyByteBuf extraData) {
this(windowId, playerInventory, player, extraData.readItem());
}

public PocketGeneratorContainer(int windowId, Inventory playerInventory, Player player, ItemStack pocketGenerator) {
super(Registration.PocketGenerator_Container.get(), windowId);
playerEntity = player;
if (pocketGenerator.getItem() instanceof PocketGenerator pocketGeneratorItem)
this.handler = pocketGeneratorItem.getStackHandler(pocketGenerator);
else
this.handler = new ItemStackHandler(SLOTS);
this.pocketGeneratorItemStack = pocketGenerator;
if (handler != null)
addSlotRange(handler, 0, 80, 35, 1, 18);

addPlayerSlots(playerInventory, 8, 84);
}

@Override
public boolean stillValid(Player playerIn) {
return playerIn.getMainHandItem().equals(pocketGeneratorItemStack);
}

@Override
public ItemStack quickMoveStack(Player playerIn, int index) {
ItemStack itemstack = ItemStack.EMPTY;
Slot slot = this.slots.get(index);
if (slot.hasItem()) {
ItemStack currentStack = slot.getItem();
if (index < SLOTS) { //Slot to Player Inventory
if (!this.moveItemStackTo(currentStack, SLOTS, Inventory.INVENTORY_SIZE + SLOTS, true)) {
return ItemStack.EMPTY;
}
}
if (index >= SLOTS) { //Player Inventory to Slots
if (!this.moveItemStackTo(currentStack, 0, SLOTS, false)) {
return ItemStack.EMPTY;
}
}

if (currentStack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}

if (currentStack.getCount() == itemstack.getCount()) {
return ItemStack.EMPTY;
}

slot.onTake(playerIn, currentStack);
}
return itemstack;
}

@Override
public void removed(Player playerIn) {
super.removed(playerIn);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.direwolf20.justdirethings.common.containers.handlers;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.neoforged.neoforge.common.CommonHooks;
import net.neoforged.neoforge.items.ItemStackHandler;

import javax.annotation.Nonnull;

public class PocketGeneratorHandler extends ItemStackHandler {
public ItemStack stack;

public PocketGeneratorHandler(int size, ItemStack itemStack) {
super(size);
this.stack = itemStack;
}

@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
}

@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return CommonHooks.getBurnTime(stack, RecipeType.SMELTING) > 0; //Todo Test Lava bucket
}
}
Loading

0 comments on commit 97ab83a

Please sign in to comment.