Initial work on experience from smelting for #1817
This commit is contained in:
parent
6ceb6e69cf
commit
09b59d6c87
3 changed files with 147 additions and 74 deletions
|
@ -24,11 +24,17 @@
|
||||||
|
|
||||||
package techreborn.blockentity.machine.iron;
|
package techreborn.blockentity.machine.iron;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
|
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
|
||||||
|
import net.minecraft.entity.ExperienceOrbEntity;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.recipe.RecipeType;
|
import net.minecraft.recipe.RecipeType;
|
||||||
|
import net.minecraft.recipe.SmeltingRecipe;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import reborncore.api.blockentity.InventoryProvider;
|
import reborncore.api.blockentity.InventoryProvider;
|
||||||
import reborncore.client.containerBuilder.IContainerProvider;
|
import reborncore.client.containerBuilder.IContainerProvider;
|
||||||
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
||||||
|
@ -54,11 +60,28 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity
|
||||||
int outputSlot = 1;
|
int outputSlot = 1;
|
||||||
int fuelSlot = 2;
|
int fuelSlot = 2;
|
||||||
boolean active = false;
|
boolean active = false;
|
||||||
|
private float experience;
|
||||||
|
|
||||||
public IronFurnaceBlockEntity() {
|
public IronFurnaceBlockEntity() {
|
||||||
super(TRBlockEntities.IRON_FURNACE);
|
super(TRBlockEntities.IRON_FURNACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IInventoryAccess<IronFurnaceBlockEntity> 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) {
|
public int gaugeProgressScaled(int scale) {
|
||||||
return progress * scale / fuelScale;
|
return progress * scale / fuelScale;
|
||||||
}
|
}
|
||||||
|
@ -73,6 +96,98 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity
|
||||||
return burnTime * scale / totalBurnTime;
|
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<SmeltingRecipe> 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
|
@Override
|
||||||
public void tick() {
|
public void tick() {
|
||||||
super.tick();
|
super.tick();
|
||||||
|
@ -119,85 +234,18 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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<IronFurnaceBlockEntity> getInventory() {
|
|
||||||
return this.inventory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IInventoryAccess<IronFurnaceBlockEntity> 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
|
@Override
|
||||||
public boolean canBeUpgraded() {
|
public boolean canBeUpgraded() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InventoryProvider
|
||||||
|
@Override
|
||||||
|
public RebornInventory<IronFurnaceBlockEntity> getInventory() {
|
||||||
|
return this.inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IContainerProvider
|
||||||
public int getBurnTime() {
|
public int getBurnTime() {
|
||||||
return this.burnTime;
|
return this.burnTime;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,9 @@ import net.minecraft.entity.player.PlayerEntity;
|
||||||
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
||||||
import reborncore.client.gui.builder.GuiBase;
|
import reborncore.client.gui.builder.GuiBase;
|
||||||
import reborncore.client.gui.guibuilder.GuiBuilder;
|
import reborncore.client.gui.guibuilder.GuiBuilder;
|
||||||
|
import reborncore.common.network.NetworkManager;
|
||||||
import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity;
|
import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity;
|
||||||
|
import techreborn.packets.ServerboundPackets;
|
||||||
|
|
||||||
public class GuiIronFurnace extends GuiBase<BuiltContainer> {
|
public class GuiIronFurnace extends GuiBase<BuiltContainer> {
|
||||||
|
|
||||||
|
@ -39,6 +41,10 @@ public class GuiIronFurnace extends GuiBase<BuiltContainer> {
|
||||||
this.blockEntity = furnace;
|
this.blockEntity = furnace;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onClick(int amount){
|
||||||
|
NetworkManager.sendToServer(ServerboundPackets.createPacketExperience(blockEntity));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawBackground(float lastFrameDuration, int mouseX, int mouseY) {
|
protected void drawBackground(float lastFrameDuration, int mouseX, int mouseY) {
|
||||||
super.drawBackground(lastFrameDuration, mouseX, mouseY);
|
super.drawBackground(lastFrameDuration, mouseX, mouseY);
|
||||||
|
|
|
@ -38,6 +38,7 @@ import reborncore.common.network.ExtendedPacketBuffer;
|
||||||
import reborncore.common.network.NetworkManager;
|
import reborncore.common.network.NetworkManager;
|
||||||
import techreborn.TechReborn;
|
import techreborn.TechReborn;
|
||||||
import techreborn.blockentity.fusionReactor.FusionControlComputerBlockEntity;
|
import techreborn.blockentity.fusionReactor.FusionControlComputerBlockEntity;
|
||||||
|
import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity;
|
||||||
import techreborn.blockentity.machine.tier1.AutoCraftingTableBlockEntity;
|
import techreborn.blockentity.machine.tier1.AutoCraftingTableBlockEntity;
|
||||||
import techreborn.blockentity.machine.tier1.RollingMachineBlockEntity;
|
import techreborn.blockentity.machine.tier1.RollingMachineBlockEntity;
|
||||||
import techreborn.blockentity.machine.tier3.ChunkLoaderBlockEntity;
|
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 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 REFUND = new Identifier(TechReborn.MOD_ID, "refund");
|
||||||
public static final Identifier CHUNKLOADER = new Identifier(TechReborn.MOD_ID, "chunkloader");
|
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() {
|
public static void init() {
|
||||||
registerPacketHandler(AESU, (extendedPacketBuffer, context) -> {
|
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<ExtendedPacketBuffer, PacketContext> consumer){
|
private static void registerPacketHandler(Identifier identifier, BiConsumer<ExtendedPacketBuffer, PacketContext> consumer){
|
||||||
|
@ -187,4 +200,10 @@ public class ServerboundPackets {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Packet<ServerPlayPacketListener> createPacketExperience(IronFurnaceBlockEntity blockEntity) {
|
||||||
|
return NetworkManager.createServerBoundPacket(EXPERIENCE, extendedPacketBuffer -> {
|
||||||
|
extendedPacketBuffer.writeBlockPos(blockEntity.getPos());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue