From 09b59d6c87516899f819efefa6afa6b7854c52dd Mon Sep 17 00:00:00 2001 From: drcrazy Date: Tue, 27 Aug 2019 01:52:52 +0300 Subject: [PATCH] Initial work on experience from smelting for #1817 --- .../machine/iron/IronFurnaceBlockEntity.java | 196 +++++++++++------- .../techreborn/client/gui/GuiIronFurnace.java | 6 + .../packets/ServerboundPackets.java | 19 ++ 3 files changed, 147 insertions(+), 74 deletions(-) diff --git a/src/main/java/techreborn/blockentity/machine/iron/IronFurnaceBlockEntity.java b/src/main/java/techreborn/blockentity/machine/iron/IronFurnaceBlockEntity.java index b563ef584..8eadfb800 100644 --- a/src/main/java/techreborn/blockentity/machine/iron/IronFurnaceBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/iron/IronFurnaceBlockEntity.java @@ -24,11 +24,17 @@ package techreborn.blockentity.machine.iron; +import java.util.Optional; + import net.minecraft.block.BlockState; import net.minecraft.block.entity.AbstractFurnaceBlockEntity; +import net.minecraft.entity.ExperienceOrbEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundTag; import net.minecraft.recipe.RecipeType; +import net.minecraft.recipe.SmeltingRecipe; +import net.minecraft.server.network.ServerPlayerEntity; import reborncore.api.blockentity.InventoryProvider; import reborncore.client.containerBuilder.IContainerProvider; import reborncore.client.containerBuilder.builder.BuiltContainer; @@ -54,10 +60,27 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity int outputSlot = 1; int fuelSlot = 2; boolean active = false; + private float experience; public IronFurnaceBlockEntity() { super(TRBlockEntities.IRON_FURNACE); } + + public static IInventoryAccess getInvetoryAccess(){ + return (slotID, stack, face, direction, blockEntity) -> { + if(direction == IInventoryAccess.AccessDirection.INSERT){ + boolean isFuel = AbstractFurnaceBlockEntity.canUseAsFuel(stack); + if(isFuel){ + ItemStack fuelSlotStack = blockEntity.inventory.getInvStack(blockEntity.fuelSlot); + if(fuelSlotStack.isEmpty() || ItemUtils.isItemEqual(stack, fuelSlotStack, true, true) && fuelSlotStack.getMaxCount() != fuelSlotStack.getCount()){ + return slotID == blockEntity.fuelSlot; + } + } + return slotID != blockEntity.outputSlot; + } + return true; + }; + } public int gaugeProgressScaled(int scale) { return progress * scale / fuelScale; @@ -72,6 +95,98 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity } return burnTime * scale / totalBurnTime; } + + private void cookItems() { + if (!canSmelt()) { + return; + } + ItemStack inputStack = inventory.getInvStack(inputSlot); + ItemStack outputStack = getResultFor(inputStack); + float recipeExp = getExperienceFor(inputStack); + + if (inventory.getInvStack(outputSlot).isEmpty()) { + inventory.setInvStack(outputSlot, outputStack.copy()); + } else if (inventory.getInvStack(outputSlot).isItemEqualIgnoreDamage(outputStack)) { + inventory.getInvStack(outputSlot).increment(outputStack.getCount()); + } + if (inputStack.getCount() > 1) { + inventory.shrinkSlot(inputSlot, 1); + } else { + inventory.setInvStack(inputSlot, ItemStack.EMPTY); + } + experience += recipeExp; + } + + private boolean canSmelt() { + if (inventory.getInvStack(inputSlot).isEmpty()) { + return false; + } + ItemStack outputStack = getResultFor(inventory.getInvStack(inputSlot)); + if (outputStack.isEmpty()) + return false; + if (inventory.getInvStack(outputSlot).isEmpty()) + return true; + if (!inventory.getInvStack(outputSlot).isItemEqualIgnoreDamage(outputStack)) + return false; + int result = inventory.getInvStack(outputSlot).getCount() + outputStack.getCount(); + return result <= inventory.getStackLimit() && result <= outputStack.getMaxCount(); + } + + public boolean isBurning() { + return burnTime > 0; + } + + private ItemStack getResultFor(ItemStack stack) { + ItemStack result = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, stack); + if (!result.isEmpty()) { + return result.copy(); + } + return ItemStack.EMPTY; + } + + private float getExperienceFor(ItemStack stack) { + Optional recipe = world.getRecipeManager().getFirstMatch(RecipeType.SMELTING, this, world); + if (recipe.isPresent()) { + return recipe.get().getExperience(); + } + return 0; + } + + public void handleGuiInputFromClient(PlayerEntity playerIn) { + if (playerIn instanceof ServerPlayerEntity) { + ServerPlayerEntity player = (ServerPlayerEntity) playerIn; + int totalExperience = (int) experience; + while (totalExperience > 0) { + int expToDrop = ExperienceOrbEntity.roundToOrbSize(totalExperience); + totalExperience -= expToDrop; + player.world.spawnEntity(new ExperienceOrbEntity(player.world, player.x, player.y + 0.5D, player.z + 0.5D, expToDrop)); + } + experience = 0; + } + } + + private void updateState() { + BlockState state = world.getBlockState(pos); + if (state.getBlock() instanceof BlockMachineBase) { + BlockMachineBase blockMachineBase = (BlockMachineBase) state.getBlock(); + if (state.get(BlockMachineBase.ACTIVE) != burnTime > 0) + blockMachineBase.setActive(burnTime > 0, world, pos); + } + } + + // MachineBaseBlockEntity + @Override + public void fromTag(CompoundTag compoundTag) { + super.fromTag(compoundTag); + experience = compoundTag.getFloat("Experience"); + } + + @Override + public CompoundTag toTag(CompoundTag compoundTag) { + super.toTag(compoundTag); + compoundTag.putFloat("Experience", experience); + return compoundTag; + } @Override public void tick() { @@ -118,86 +233,19 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity markDirty(); } } - - public void cookItems() { - if (!canSmelt()) { - return; - } - ItemStack outputStack = getResultFor(inventory.getInvStack(inputSlot)); - if (inventory.getInvStack(outputSlot).isEmpty()) { - inventory.setInvStack(outputSlot, outputStack.copy()); - } else if (inventory.getInvStack(outputSlot).isItemEqualIgnoreDamage(outputStack)) { - inventory.getInvStack(outputSlot).increment(outputStack.getCount()); - } - if (inventory.getInvStack(inputSlot).getCount() > 1) { - inventory.shrinkSlot(inputSlot, 1); - } else { - inventory.setInvStack(inputSlot, ItemStack.EMPTY); - } - } - - public boolean canSmelt() { - if (inventory.getInvStack(inputSlot).isEmpty()) { - return false; - } - ItemStack outputStack = getResultFor(inventory.getInvStack(inputSlot)); - if (outputStack.isEmpty()) - return false; - if (inventory.getInvStack(outputSlot).isEmpty()) - return true; - if (!inventory.getInvStack(outputSlot).isItemEqualIgnoreDamage(outputStack)) - return false; - int result = inventory.getInvStack(outputSlot).getCount() + outputStack.getCount(); - return result <= inventory.getStackLimit() && result <= outputStack.getMaxCount(); - } - - public boolean isBurning() { - return burnTime > 0; - } - - private ItemStack getResultFor(ItemStack stack) { - ItemStack result = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, stack); - if (!result.isEmpty()) { - return result.copy(); - } - return ItemStack.EMPTY; - } - - private void updateState() { - BlockState state = world.getBlockState(pos); - if (state.getBlock() instanceof BlockMachineBase) { - BlockMachineBase blockMachineBase = (BlockMachineBase) state.getBlock(); - if (state.get(BlockMachineBase.ACTIVE) != burnTime > 0) - blockMachineBase.setActive(burnTime > 0, world, pos); - } - } - - @Override - public RebornInventory getInventory() { - return this.inventory; - } - - public static IInventoryAccess getInvetoryAccess(){ - return (slotID, stack, face, direction, blockEntity) -> { - if(direction == IInventoryAccess.AccessDirection.INSERT){ - boolean isFuel = AbstractFurnaceBlockEntity.canUseAsFuel(stack); - if(isFuel){ - ItemStack fuelSlotStack = blockEntity.inventory.getInvStack(blockEntity.fuelSlot); - if(fuelSlotStack.isEmpty() || ItemUtils.isItemEqual(stack, fuelSlotStack, true, true) && fuelSlotStack.getMaxCount() != fuelSlotStack.getCount()){ - return slotID == blockEntity.fuelSlot; - } - } - return slotID != blockEntity.outputSlot; - } - return true; - }; - } @Override public boolean canBeUpgraded() { return false; } + // InventoryProvider + @Override + public RebornInventory getInventory() { + return this.inventory; + } + + // IContainerProvider public int getBurnTime() { return this.burnTime; } diff --git a/src/main/java/techreborn/client/gui/GuiIronFurnace.java b/src/main/java/techreborn/client/gui/GuiIronFurnace.java index ea6c3c4eb..9d75a528b 100644 --- a/src/main/java/techreborn/client/gui/GuiIronFurnace.java +++ b/src/main/java/techreborn/client/gui/GuiIronFurnace.java @@ -28,7 +28,9 @@ import net.minecraft.entity.player.PlayerEntity; import reborncore.client.containerBuilder.builder.BuiltContainer; import reborncore.client.gui.builder.GuiBase; import reborncore.client.gui.guibuilder.GuiBuilder; +import reborncore.common.network.NetworkManager; import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity; +import techreborn.packets.ServerboundPackets; public class GuiIronFurnace extends GuiBase { @@ -39,6 +41,10 @@ public class GuiIronFurnace extends GuiBase { this.blockEntity = furnace; } + public void onClick(int amount){ + NetworkManager.sendToServer(ServerboundPackets.createPacketExperience(blockEntity)); + } + @Override protected void drawBackground(float lastFrameDuration, int mouseX, int mouseY) { super.drawBackground(lastFrameDuration, mouseX, mouseY); diff --git a/src/main/java/techreborn/packets/ServerboundPackets.java b/src/main/java/techreborn/packets/ServerboundPackets.java index 347dfc1a8..eeed1032a 100644 --- a/src/main/java/techreborn/packets/ServerboundPackets.java +++ b/src/main/java/techreborn/packets/ServerboundPackets.java @@ -38,6 +38,7 @@ import reborncore.common.network.ExtendedPacketBuffer; import reborncore.common.network.NetworkManager; import techreborn.TechReborn; import techreborn.blockentity.fusionReactor.FusionControlComputerBlockEntity; +import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity; import techreborn.blockentity.machine.tier1.AutoCraftingTableBlockEntity; import techreborn.blockentity.machine.tier1.RollingMachineBlockEntity; import techreborn.blockentity.machine.tier3.ChunkLoaderBlockEntity; @@ -55,6 +56,7 @@ public class ServerboundPackets { public static final Identifier FUSION_CONTROL_SIZE = new Identifier(TechReborn.MOD_ID, "fusion_control_size"); public static final Identifier REFUND = new Identifier(TechReborn.MOD_ID, "refund"); public static final Identifier CHUNKLOADER = new Identifier(TechReborn.MOD_ID, "chunkloader"); + public static final Identifier EXPERIENCE = new Identifier(TechReborn.MOD_ID, "experience"); public static void init() { registerPacketHandler(AESU, (extendedPacketBuffer, context) -> { @@ -137,6 +139,17 @@ public class ServerboundPackets { } }); }); + + registerPacketHandler(EXPERIENCE, (extendedPacketBuffer, context) -> { + BlockPos pos = extendedPacketBuffer.readBlockPos(); + + context.getTaskQueue().execute(() -> { + BlockEntity blockEntity = context.getPlayer().world.getBlockEntity(pos); + if (blockEntity instanceof IronFurnaceBlockEntity) { + ((IronFurnaceBlockEntity) blockEntity).handleGuiInputFromClient(context.getPlayer()); + } + }); + }); } private static void registerPacketHandler(Identifier identifier, BiConsumer consumer){ @@ -186,5 +199,11 @@ public class ServerboundPackets { extendedPacketBuffer.writeInt(buttonID); }); } + + public static Packet createPacketExperience(IronFurnaceBlockEntity blockEntity) { + return NetworkManager.createServerBoundPacket(EXPERIENCE, extendedPacketBuffer -> { + extendedPacketBuffer.writeBlockPos(blockEntity.getPos()); + }); + } }