Fix smelter exp, refactor

This commit is contained in:
Aleksey 2021-04-26 09:43:22 +03:00
parent 265cc1a666
commit dba8876bde
2 changed files with 123 additions and 124 deletions

View file

@ -105,17 +105,17 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
} }
private boolean isBurning() { private boolean isBurning() {
return this.burnTime > 0; return burnTime > 0;
} }
@Override @Override
public int getContainerSize() { public int getContainerSize() {
return this.inventory.size(); return inventory.size();
} }
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
Iterator<ItemStack> iterator = this.inventory.iterator(); Iterator<ItemStack> iterator = inventory.iterator();
ItemStack itemStack; ItemStack itemStack;
do { do {
if (!iterator.hasNext()) { if (!iterator.hasNext()) {
@ -129,40 +129,40 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
@Override @Override
public ItemStack getItem(int slot) { public ItemStack getItem(int slot) {
return this.inventory.get(slot); return inventory.get(slot);
} }
@Override @Override
public ItemStack removeItem(int slot, int amount) { public ItemStack removeItem(int slot, int amount) {
return ContainerHelper.removeItem(this.inventory, slot, amount); return ContainerHelper.removeItem(inventory, slot, amount);
} }
@Override @Override
public ItemStack removeItemNoUpdate(int slot) { public ItemStack removeItemNoUpdate(int slot) {
return ContainerHelper.takeItem(this.inventory, slot); return ContainerHelper.takeItem(inventory, slot);
} }
@Override @Override
public void setItem(int slot, ItemStack stack) { public void setItem(int slot, ItemStack stack) {
ItemStack itemStack = this.inventory.get(slot); ItemStack itemStack = inventory.get(slot);
boolean stackValid = !stack.isEmpty() && stack.sameItem(itemStack) && ItemStack.tagMatches(stack, itemStack); boolean stackValid = !stack.isEmpty() && stack.sameItem(itemStack) && ItemStack.tagMatches(stack, itemStack);
this.inventory.set(slot, stack); inventory.set(slot, stack);
if (stack.getCount() > getMaxStackSize()) { if (stack.getCount() > getMaxStackSize()) {
stack.setCount(getMaxStackSize()); stack.setCount(getMaxStackSize());
} }
if ((slot == 0 || slot == 1) && !stackValid) { if ((slot == 0 || slot == 1) && !stackValid) {
this.smeltTimeTotal = this.getSmeltTime(); smeltTimeTotal = getSmeltTime();
this.smeltTime = 0; smeltTime = 0;
this.setChanged(); setChanged();
} }
} }
protected int getSmeltTime() { protected int getSmeltTime() {
assert this.level != null; if (level == null) return 200;
int smeltTime = this.level.getRecipeManager().getRecipeFor(AlloyingRecipe.TYPE, this, level) int smeltTime = level.getRecipeManager().getRecipeFor(AlloyingRecipe.TYPE, this, level)
.map(AlloyingRecipe::getSmeltTime).orElse(0); .map(AlloyingRecipe::getSmeltTime).orElse(0);
if (smeltTime == 0) { if (smeltTime == 0) {
smeltTime = this.level.getRecipeManager().getRecipeFor(RecipeType.BLASTING, this, level) smeltTime = level.getRecipeManager().getRecipeFor(RecipeType.BLASTING, this, level)
.map(BlastingRecipe::getCookingTime).orElse(200); .map(BlastingRecipe::getCookingTime).orElse(200);
smeltTime /= 1.5; smeltTime /= 1.5;
} }
@ -170,50 +170,49 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
} }
public void dropExperience(Player player) { public void dropExperience(Player player) {
assert level != null; if (level == null) return;
List<Recipe<?>> list = Lists.newArrayList(); List<Recipe<?>> list = Lists.newArrayList();
for (Entry<ResourceLocation> entry : this.recipesUsed.object2IntEntrySet()) { for (Entry<ResourceLocation> entry : recipesUsed.object2IntEntrySet()) {
level.getRecipeManager().byKey(entry.getKey()).ifPresent((recipe) -> { level.getRecipeManager().byKey(entry.getKey()).ifPresent((recipe) -> {
list.add(recipe); list.add(recipe);
if (recipe instanceof AlloyingRecipe) { if (recipe instanceof AlloyingRecipe) {
AlloyingRecipe alloying = (AlloyingRecipe) recipe; AlloyingRecipe alloying = (AlloyingRecipe) recipe;
this.dropExperience(player.level, player.position(), entry.getIntValue(), alloying.getExperience()); dropExperience(player.level, player.position(), entry.getIntValue(), alloying.getExperience());
} else { } else {
BlastingRecipe blasting = (BlastingRecipe) recipe; BlastingRecipe blasting = (BlastingRecipe) recipe;
this.dropExperience(player.level, player.position(), entry.getIntValue(), blasting.getExperience()); dropExperience(player.level, player.position(), entry.getIntValue(), blasting.getExperience());
} }
}); });
} }
player.awardRecipes(list); player.awardRecipes(list);
this.recipesUsed.clear(); recipesUsed.clear();
} }
private void dropExperience(Level world, Vec3 vec3d, int i, float f) { private void dropExperience(Level world, Vec3 vec3d, int count, float amount) {
int j = Mth.floor(i * f); int expTotal = Mth.floor(count * amount);
float g = Mth.frac(i * f); float g = Mth.frac(count * amount);
if (g != 0.0F && Math.random() < g) { if (g != 0.0F && Math.random() < g) {
j++; expTotal++;
} }
while(j > 0) { while(expTotal > 0) {
int k = ExperienceOrb.getExperienceValue(j); int expVal = ExperienceOrb.getExperienceValue(expTotal);
j -= k; expTotal -= expVal;
world.addFreshEntity(new ExperienceOrb(world, vec3d.x, vec3d.y, vec3d.z, k)); world.addFreshEntity(new ExperienceOrb(world, vec3d.x, vec3d.y, vec3d.z, expVal));
} }
} }
@Override @Override
public boolean stillValid(Player player) { public boolean stillValid(Player player) {
assert this.level != null; if (level != null && level.getBlockEntity(worldPosition) != this) {
if (this.level.getBlockEntity(this.worldPosition) != this) {
return false; return false;
} }
return player.distanceToSqr(this.worldPosition.getX() + 0.5D, this.worldPosition.getY() + 0.5D, this.worldPosition.getZ() + 0.5D) <= 64.0D; return player.distanceToSqr(worldPosition.getX() + 0.5D, worldPosition.getY() + 0.5D, worldPosition.getZ() + 0.5D) <= 64.0D;
} }
@Override @Override
public void clearContent() { public void clearContent() {
this.inventory.clear(); inventory.clear();
} }
@Override @Override
@ -228,58 +227,59 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
@Override @Override
public void tick() { public void tick() {
boolean initialBurning = this.isBurning(); if (level == null) return;
boolean initialBurning = isBurning();
if (initialBurning) { if (initialBurning) {
this.burnTime--; burnTime--;
} }
boolean burning = this.isBurning(); boolean burning = initialBurning;
assert this.level != null; if (!level.isClientSide) {
if (!this.level.isClientSide) { ItemStack fuel = inventory.get(2);
ItemStack fuel = this.inventory.get(2);
if (!burning && (fuel.isEmpty() || inventory.get(0).isEmpty() && inventory.get(1).isEmpty())) { if (!burning && (fuel.isEmpty() || inventory.get(0).isEmpty() && inventory.get(1).isEmpty())) {
if (smeltTime > 0) { if (smeltTime > 0) {
this.smeltTime = Mth.clamp(smeltTime - 2, 0, smeltTimeTotal); smeltTime = Mth.clamp(smeltTime - 2, 0, smeltTimeTotal);
} }
} else { } else {
Recipe<?> recipe = this.level.getRecipeManager().getRecipeFor(AlloyingRecipe.TYPE, this, level).orElse(null); Recipe<?> recipe = level.getRecipeManager().getRecipeFor(AlloyingRecipe.TYPE, this, level).orElse(null);
if (recipe == null) { if (recipe == null) {
recipe = this.level.getRecipeManager().getRecipeFor(RecipeType.BLASTING, this, level).orElse(null); recipe = level.getRecipeManager().getRecipeFor(RecipeType.BLASTING, this, level).orElse(null);
} }
boolean accepted = this.canAcceptRecipeOutput(recipe); boolean accepted = this.canAcceptRecipeOutput(recipe);
if (!burning && accepted) { if (!burning && accepted) {
this.burnTime = this.getFuelTime(fuel); burnTime = getFuelTime(fuel);
this.fuelTime = this.burnTime; fuelTime = burnTime;
burning = this.isBurning(); burning = isBurning();
if (burning) { if (burning) {
if (!fuel.isEmpty()) { if (!fuel.isEmpty()) {
Item item = fuel.getItem(); Item item = fuel.getItem();
fuel.shrink(1); fuel.shrink(1);
if (fuel.isEmpty()) { if (fuel.isEmpty()) {
Item remainFuel = item.getCraftingRemainingItem(); Item remainFuel = item.getCraftingRemainingItem();
this.inventory.set(2, remainFuel == null ? ItemStack.EMPTY : new ItemStack(remainFuel)); inventory.set(2, remainFuel == null ? ItemStack.EMPTY : new ItemStack(remainFuel));
} }
} }
this.setChanged(); setChanged();
} }
} }
if (burning && accepted) { if (burning && accepted) {
this.smeltTime++; this.smeltTime++;
if (smeltTime == smeltTimeTotal) { if (smeltTime == smeltTimeTotal) {
this.smeltTime = 0; smeltTime = 0;
this.smeltTimeTotal = this.getSmeltTime(); smeltTimeTotal = getSmeltTime();
this.craftRecipe(recipe); craftRecipe(recipe);
this.setChanged(); setChanged();
} }
} else { } else {
this.smeltTime = 0; smeltTime = 0;
} }
} }
if (initialBurning != burning) { if (initialBurning != burning) {
this.level.setBlock(worldPosition, level.getBlockState(worldPosition).setValue(EndStoneSmelter.LIT, burning), 3); level.setBlock(worldPosition, level.getBlockState(worldPosition).setValue(EndStoneSmelter.LIT, burning), 3);
this.setChanged(); setChanged();
} }
} }
} }
@ -308,8 +308,8 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
if (!output.sameItem(result)) { if (!output.sameItem(result)) {
return false; return false;
} }
if (outCount < this.getMaxStackSize() && outCount < output.getMaxStackSize()) { if (outCount < getMaxStackSize() && outCount < output.getMaxStackSize()) {
return this.getMaxStackSize() >= total; return getMaxStackSize() >= total;
} }
return output.getCount() < result.getMaxStackSize(); return output.getCount() < result.getMaxStackSize();
} }
@ -320,26 +320,26 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
if (recipe == null || !canAcceptRecipeOutput(recipe)) return; if (recipe == null || !canAcceptRecipeOutput(recipe)) return;
ItemStack result = recipe.getResultItem(); ItemStack result = recipe.getResultItem();
ItemStack output = this.inventory.get(3); ItemStack output = inventory.get(3);
if (output.isEmpty()) { if (output.isEmpty()) {
this.inventory.set(3, result.copy()); inventory.set(3, result.copy());
} else if (output.getItem() == result.getItem()) { } else if (output.getItem() == result.getItem()) {
output.grow(result.getCount()); output.grow(result.getCount());
} }
assert this.level != null; assert this.level != null;
if (!this.level.isClientSide) { if (!this.level.isClientSide) {
this.setRecipeUsed(recipe); setRecipeUsed(recipe);
} }
if (recipe instanceof AlloyingRecipe) { if (recipe instanceof AlloyingRecipe) {
this.inventory.get(0).shrink(1); inventory.get(0).shrink(1);
this.inventory.get(1).shrink(1); inventory.get(1).shrink(1);
} else { } else {
if (!this.inventory.get(0).isEmpty()) { if (!inventory.get(0).isEmpty()) {
this.inventory.get(0).shrink(1); inventory.get(0).shrink(1);
} else { } else {
this.inventory.get(1).shrink(1); inventory.get(1).shrink(1);
} }
} }
} }
@ -355,8 +355,8 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
public void setRecipeUsed(Recipe<?> recipe) { public void setRecipeUsed(Recipe<?> recipe) {
if (recipe != null) { if (recipe != null) {
ResourceLocation recipeId = recipe.getId(); ResourceLocation recipeId = recipe.getId();
this.recipesUsed.addTo(recipeId, 1); recipesUsed.addTo(recipeId, 1);
this.lastRecipe = recipe; lastRecipe = recipe;
} }
} }
@ -397,15 +397,15 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
@Override @Override
public void load(BlockState state, CompoundTag tag) { public void load(BlockState state, CompoundTag tag) {
super.load(state, tag); super.load(state, tag);
this.inventory = NonNullList.withSize(getContainerSize(), ItemStack.EMPTY); inventory = NonNullList.withSize(getContainerSize(), ItemStack.EMPTY);
ContainerHelper.loadAllItems(tag, inventory); ContainerHelper.loadAllItems(tag, inventory);
this.burnTime = tag.getShort("BurnTime"); burnTime = tag.getShort("BurnTime");
this.fuelTime = tag.getShort("FuelTime"); fuelTime = tag.getShort("FuelTime");
this.smeltTime = tag.getShort("SmeltTime"); smeltTime = tag.getShort("SmeltTime");
this.smeltTimeTotal = tag.getShort("SmeltTimeTotal"); smeltTimeTotal = tag.getShort("SmeltTimeTotal");
CompoundTag compoundTag = tag.getCompound("RecipesUsed"); CompoundTag compoundTag = tag.getCompound("RecipesUsed");
for (String id : compoundTag.getAllKeys()) { for (String id : compoundTag.getAllKeys()) {
this.recipesUsed.put(new ResourceLocation(id), compoundTag.getInt(id)); recipesUsed.put(new ResourceLocation(id), compoundTag.getInt(id));
} }
} }
@ -418,7 +418,7 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
tag.putShort("SmeltTimeTotal", (short) smeltTimeTotal); tag.putShort("SmeltTimeTotal", (short) smeltTimeTotal);
ContainerHelper.saveAllItems(tag, inventory); ContainerHelper.saveAllItems(tag, inventory);
CompoundTag usedRecipes = new CompoundTag(); CompoundTag usedRecipes = new CompoundTag();
this.recipesUsed.forEach((identifier, integer) -> usedRecipes.putInt(identifier.toString(), integer)); recipesUsed.forEach((identifier, integer) -> usedRecipes.putInt(identifier.toString(), integer));
tag.put("RecipesUsed", usedRecipes); tag.put("RecipesUsed", usedRecipes);
return tag; return tag;

View file

@ -44,19 +44,19 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
this.propertyDelegate = propertyDelegate; this.propertyDelegate = propertyDelegate;
this.world = playerInventory.player.level; this.world = playerInventory.player.level;
this.addDataSlots(propertyDelegate); addDataSlots(propertyDelegate);
this.addSlot(new Slot(inventory, 0, 45, 17)); addSlot(new Slot(inventory, 0, 45, 17));
this.addSlot(new Slot(inventory, 1, 67, 17)); addSlot(new Slot(inventory, 1, 67, 17));
this.addSlot(new SmelterFuelSlot(this, inventory, 2, 56, 53)); addSlot(new SmelterFuelSlot(this, inventory, 2, 56, 53));
this.addSlot(new SmelterOutputSlot(playerInventory.player, inventory, 3, 129, 35)); addSlot(new SmelterOutputSlot(playerInventory.player, inventory, 3, 129, 35));
for(int i = 0; i < 3; ++i) { for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 9; ++j) { for(int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
} }
} }
for(int i = 0; i < 9; ++i) { for(int i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142)); addSlot(new Slot(playerInventory, i, 8 + i * 18, 142));
} }
} }
@ -74,12 +74,12 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
@Override @Override
public void clearCraftingContent() { public void clearCraftingContent() {
this.inventory.clearContent(); inventory.clearContent();
} }
@Override @Override
public boolean recipeMatches(Recipe<? super Container> recipe) { public boolean recipeMatches(Recipe<? super Container> recipe) {
return recipe.matches(this.inventory, this.world); return recipe.matches(inventory, world);
} }
@Override @Override
@ -109,11 +109,11 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
@Override @Override
public boolean stillValid(Player player) { public boolean stillValid(Player player) {
return this.inventory.stillValid(player); return inventory.stillValid(player);
} }
protected boolean isSmeltable(ItemStack itemStack) { protected boolean isSmeltable(ItemStack itemStack) {
return this.world.getRecipeManager().getRecipeFor(AlloyingRecipe.TYPE, new SimpleContainer(itemStack), this.world).isPresent(); return world.getRecipeManager().getRecipeFor(AlloyingRecipe.TYPE, new SimpleContainer(itemStack), world).isPresent();
} }
public boolean isFuel(ItemStack itemStack) { public boolean isFuel(ItemStack itemStack) {
@ -122,70 +122,69 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
@Override @Override
public ItemStack quickMoveStack(Player player, int index) { public ItemStack quickMoveStack(Player player, int index) {
ItemStack itemStack = ItemStack.EMPTY; Slot slot = slots.get(index);
Slot slot = this.slots.get(index); if (slot == null || !slot.hasItem()) return ItemStack.EMPTY;
if (slot != null && slot.hasItem()) {
ItemStack itemStack2 = slot.getItem(); ItemStack slotStack = slot.getItem();
itemStack = itemStack2.copy(); ItemStack itemStack = slotStack.copy();
if (index == 3) { if (index == 3) {
if (moveItemStackTo(itemStack2, 4, 40, true)) { if (!moveItemStackTo(slotStack, 4, 40, true)) {
return ItemStack.EMPTY;
}
slot.onQuickCraft(itemStack2, itemStack);
} else if (index != 2 && index != 1 && index != 0) {
if (isSmeltable(itemStack2)) {
if (!moveItemStackTo(itemStack2, 0, 2, false)) {
return ItemStack.EMPTY;
}
} else if (isFuel(itemStack2)) {
if (!this.moveItemStackTo(itemStack2, 2, 3, false)) {
return ItemStack.EMPTY;
}
} else if (index < 31) {
if (!moveItemStackTo(itemStack2, 31, 40, false)) {
return ItemStack.EMPTY;
}
} else if (index < 40 && !moveItemStackTo(itemStack2, 4, 31, false)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(itemStack2, 4, 40, false)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
slot.onQuickCraft(slotStack, itemStack);
if (itemStack2.isEmpty()) { } else if (index != 2 && index != 1 && index != 0) {
slot.set(ItemStack.EMPTY); if (isSmeltable(slotStack)) {
} else { if (!moveItemStackTo(slotStack, 0, 2, false)) {
slot.setChanged(); return ItemStack.EMPTY;
} }
} else if (isFuel(slotStack)) {
if (itemStack2.getCount() == itemStack.getCount()) { if (!moveItemStackTo(slotStack, 2, 3, false)) {
return ItemStack.EMPTY;
}
} else if (index < 31) {
if (!moveItemStackTo(slotStack, 31, 40, false)) {
return ItemStack.EMPTY;
}
} else if (index < 40 && !moveItemStackTo(slotStack, 4, 31, false)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
} else if (!moveItemStackTo(slotStack, 4, 40, false)) {
slot.onTake(player, itemStack2); return ItemStack.EMPTY;
} }
if (slotStack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
if (slotStack.getCount() == itemStack.getCount()) {
return ItemStack.EMPTY;
}
slot.onTake(player, slotStack);
return itemStack; return itemStack;
} }
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public int getSmeltProgress() { public int getSmeltProgress() {
int time = this.propertyDelegate.get(2); int time = propertyDelegate.get(2);
int timeTotal = this.propertyDelegate.get(3); int timeTotal = propertyDelegate.get(3);
return timeTotal != 0 && time != 0 ? time * 24 / timeTotal : 0; return timeTotal != 0 && time != 0 ? time * 24 / timeTotal : 0;
} }
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public int getFuelProgress() { public int getFuelProgress() {
int fuelTime = this.propertyDelegate.get(1); int fuelTime = propertyDelegate.get(1);
if (fuelTime == 0) { if (fuelTime == 0) {
fuelTime = 200; fuelTime = 200;
} }
return this.propertyDelegate.get(0) * 13 / fuelTime; return propertyDelegate.get(0) * 13 / fuelTime;
} }
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public boolean isBurning() { public boolean isBurning() {
return this.propertyDelegate.get(0) > 0; return propertyDelegate.get(0) > 0;
} }
} }