From e991a8a23ad5c7ef65c3522f69facc2a38596cdd Mon Sep 17 00:00:00 2001 From: Aleksey Date: Sat, 16 Jan 2021 22:48:05 +0300 Subject: [PATCH] End Stone Smelter slots fix --- .../entities/EndStoneSmelterBlockEntity.java | 107 +++++++----------- 1 file changed, 40 insertions(+), 67 deletions(-) diff --git a/src/main/java/ru/betterend/blocks/entities/EndStoneSmelterBlockEntity.java b/src/main/java/ru/betterend/blocks/entities/EndStoneSmelterBlockEntity.java index ae764e76..54b3fcc1 100644 --- a/src/main/java/ru/betterend/blocks/entities/EndStoneSmelterBlockEntity.java +++ b/src/main/java/ru/betterend/blocks/entities/EndStoneSmelterBlockEntity.java @@ -1,15 +1,9 @@ package ru.betterend.blocks.entities; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - import com.google.common.collect.Lists; import com.google.common.collect.Maps; - import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; -import it.unimi.dsi.fastutil.objects.ObjectIterator; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.entity.LockableContainerBlockEntity; @@ -23,15 +17,9 @@ import net.minecraft.item.ItemConvertible; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.CompoundTag; -import net.minecraft.recipe.BlastingRecipe; -import net.minecraft.recipe.Recipe; -import net.minecraft.recipe.RecipeFinder; -import net.minecraft.recipe.RecipeInputProvider; -import net.minecraft.recipe.RecipeType; -import net.minecraft.recipe.RecipeUnlocker; +import net.minecraft.recipe.*; import net.minecraft.screen.PropertyDelegate; import net.minecraft.screen.ScreenHandler; -import net.minecraft.tag.Tag; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; @@ -47,11 +35,15 @@ import ru.betterend.client.gui.EndStoneSmelterScreenHandler; import ru.betterend.recipe.builders.AlloyingRecipe; import ru.betterend.registry.EndBlockEntities; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider, Tickable { private static final int[] TOP_SLOTS = new int[] { 0, 1 }; private static final int[] BOTTOM_SLOTS = new int[] { 2, 3 }; - private static final int[] SIDE_SLOTS = new int[] { 3 }; + private static final int[] SIDE_SLOTS = new int[] { 1, 2 }; private static final Map AVAILABLE_FUELS = Maps.newHashMap(); private final Object2IntOpenHashMap recipesUsed; @@ -66,7 +58,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp public EndStoneSmelterBlockEntity() { super(EndBlockEntities.END_STONE_SMELTER); this.inventory = DefaultedList.ofSize(4, ItemStack.EMPTY); - this.recipesUsed = new Object2IntOpenHashMap(); + this.recipesUsed = new Object2IntOpenHashMap<>(); this.propertyDelegate = new PropertyDelegate() { public int get(int index) { switch(index) { @@ -167,6 +159,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp } protected int getSmeltTime() { + assert this.world != null; int smeltTime = this.world.getRecipeManager().getFirstMatch(AlloyingRecipe.TYPE, this, world) .map(AlloyingRecipe::getSmeltTime).orElse(0); if (smeltTime == 0) { @@ -178,10 +171,9 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp } public void dropExperience(PlayerEntity player) { + assert world != null; List> list = Lists.newArrayList(); - ObjectIterator> usedRecipes = this.recipesUsed.object2IntEntrySet().iterator(); - while(usedRecipes.hasNext()) { - Entry entry = usedRecipes.next(); + for (Entry entry : this.recipesUsed.object2IntEntrySet()) { world.getRecipeManager().get(entry.getKey()).ifPresent((recipe) -> { list.add(recipe); if (recipe instanceof AlloyingRecipe) { @@ -213,11 +205,11 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp @Override public boolean canPlayerUse(PlayerEntity player) { + assert this.world != null; if (this.world.getBlockEntity(this.pos) != this) { return false; - } else { - return player.squaredDistanceTo(this.pos.getX() + 0.5D, this.pos.getY() + 0.5D, this.pos.getZ() + 0.5D) <= 64.0D; } + return player.squaredDistanceTo(this.pos.getX() + 0.5D, this.pos.getY() + 0.5D, this.pos.getZ() + 0.5D) <= 64.0D; } @Override @@ -243,10 +235,11 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp } boolean burning = this.isBurning(); + assert this.world != null; if (!this.world.isClient) { ItemStack fuel = this.inventory.get(2); if (!burning && (fuel.isEmpty() || inventory.get(0).isEmpty() && inventory.get(1).isEmpty())) { - if (!burning && smeltTime > 0) { + if (smeltTime > 0) { this.smeltTime = MathHelper.clamp(smeltTime - 2, 0, smeltTimeTotal); } } else { @@ -294,7 +287,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp protected boolean canAcceptRecipeOutput(Recipe recipe) { if (recipe == null) return false; - boolean validInput = false; + boolean validInput; if (recipe instanceof AlloyingRecipe) { validInput = !inventory.get(0).isEmpty() && !inventory.get(1).isEmpty(); @@ -306,22 +299,21 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp ItemStack result = recipe.getOutput(); if (result.isEmpty()) { return false; - } else { - ItemStack output = this.inventory.get(3); - int outCount = output.getCount(); - int total = outCount + result.getCount(); - if (output.isEmpty()) { - return true; - } else if (!output.isItemEqualIgnoreDamage(result)) { - return false; - } else if (outCount < this.getMaxCountPerStack() && outCount < output.getMaxCount()) { - return this.getMaxCountPerStack() >= total; - } else { - return output.getCount() < result.getMaxCount(); - } } + ItemStack output = this.inventory.get(3); + int outCount = output.getCount(); + int total = outCount + result.getCount(); + if (output.isEmpty()) { + return true; + } + if (!output.isItemEqualIgnoreDamage(result)) { + return false; + } + if (outCount < this.getMaxCountPerStack() && outCount < output.getMaxCount()) { + return this.getMaxCountPerStack() >= total; + } + return output.getCount() < result.getMaxCount(); } - return false; } @@ -336,6 +328,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp output.increment(result.getCount()); } + assert this.world != null; if (!this.world.isClient) { this.setLastRecipe(recipe); } @@ -354,9 +347,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp @Override public void provideRecipeInputs(RecipeFinder finder) { - Iterator inventory = this.inventory.iterator(); - while(inventory.hasNext()) { - ItemStack itemStack = inventory.next(); + for (ItemStack itemStack : this.inventory) { finder.addItem(itemStack); } } @@ -379,9 +370,8 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp public int[] getAvailableSlots(Direction side) { if (side == Direction.DOWN) { return BOTTOM_SLOTS; - } else { - return side == Direction.UP ? TOP_SLOTS : SIDE_SLOTS; } + return side == Direction.UP ? TOP_SLOTS : SIDE_SLOTS; } @Override @@ -392,9 +382,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp @Override public boolean canExtract(int slot, ItemStack stack, Direction dir) { if (dir == Direction.DOWN && slot == 2) { - if (stack.getItem() != Items.BUCKET) { - return false; - } + return stack.getItem() == Items.BUCKET; } return true; } @@ -402,10 +390,9 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp protected int getFuelTime(ItemStack fuel) { if (fuel.isEmpty()) { return 0; - } else { - Item item = fuel.getItem(); - return AVAILABLE_FUELS.getOrDefault(item, 0); } + Item item = fuel.getItem(); + return AVAILABLE_FUELS.getOrDefault(item, 0); } @Override @@ -418,9 +405,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp this.smeltTime = tag.getShort("SmeltTime"); this.smeltTimeTotal = tag.getShort("SmeltTimeTotal"); CompoundTag compoundTag = tag.getCompound("RecipesUsed"); - Iterator recipes = compoundTag.getKeys().iterator(); - while(recipes.hasNext()) { - String id = recipes.next(); + for (String id : compoundTag.getKeys()) { this.recipesUsed.put(new Identifier(id), compoundTag.getInt(id)); } } @@ -434,9 +419,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp tag.putShort("SmeltTimeTotal", (short) smeltTimeTotal); Inventories.toTag(tag, inventory); CompoundTag usedRecipes = new CompoundTag(); - this.recipesUsed.forEach((identifier, integer) -> { - usedRecipes.putInt(identifier.toString(), integer); - }); + this.recipesUsed.forEach((identifier, integer) -> usedRecipes.putInt(identifier.toString(), integer)); tag.put("RecipesUsed", usedRecipes); return tag; @@ -445,12 +428,11 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp public boolean isValid(int slot, ItemStack stack) { if (slot == 3) { return false; - } else if (slot != 0 || slot != 1) { + } else if (slot != 2) { return true; - } else { - ItemStack itemStack = this.inventory.get(2); - return canUseAsFuel(stack) || stack.getItem() == Items.BUCKET && itemStack.getItem() != Items.BUCKET; } + ItemStack itemStack = this.inventory.get(2); + return canUseAsFuel(stack) || stack.getItem() == Items.BUCKET && itemStack.getItem() != Items.BUCKET; } public static boolean canUseAsFuel(ItemStack stack) { @@ -458,16 +440,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp } public static void registerFuel(ItemConvertible fuel, int time) { - if (AVAILABLE_FUELS.containsKey(fuel)) { - AVAILABLE_FUELS.replace(fuel.asItem(), time); - } else { - AVAILABLE_FUELS.put(fuel.asItem(), time); - } - } - - public static void registerFuel(Tag tag, int time) { - Iterator tagItems = tag.values().iterator(); - tagItems.forEachRemaining(item -> registerFuel(item, time)); + AVAILABLE_FUELS.put(fuel.asItem(), time); } public static Map availableFuels() {