Continue mapping migration

This commit is contained in:
Aleksey 2021-04-12 21:38:22 +03:00
parent 99ade39404
commit f03fd03bd0
499 changed files with 12567 additions and 12723 deletions

View file

@ -10,99 +10,98 @@ import com.google.common.collect.Maps;
import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.fabricmc.fabric.api.registry.FuelRegistry;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.world.level.block.entity.LockableContainerBlockEntity;
import net.minecraft.world.entity.ExperienceOrbEntity;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.WorldlyContainer;
import net.minecraft.world.entity.ExperienceOrb;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.SidedInventory;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.inventory.RecipeHolder;
import net.minecraft.world.inventory.StackedContentsCompatible;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.crafting.BlastingRecipe;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeFinder;
import net.minecraft.world.item.crafting.RecipeInputProvider;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.RecipeUnlocker;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Tickable;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.entity.TickableBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import ru.betterend.BetterEnd;
import ru.betterend.blocks.EndStoneSmelter;
import ru.betterend.client.gui.EndStoneSmelterScreenHandler;
import ru.betterend.recipe.builders.AlloyingRecipe;
import ru.betterend.registry.EndBlockEntities;
public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
implements SidedInventory, RecipeUnlocker, RecipeInputProvider, Tickable {
public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity implements WorldlyContainer, RecipeHolder, StackedContentsCompatible, TickableBlockEntity {
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[] { 1, 2 };
private static final Map<Item, Integer> AVAILABLE_FUELS = Maps.newHashMap();
private final Object2IntOpenHashMap<ResourceLocation> recipesUsed;
protected DefaultedList<ItemStack> inventory;
protected final PropertyDelegate propertyDelegate;
protected NonNullList<ItemStack> inventory;
protected final ContainerData propertyDelegate;
private Recipe<?> lastRecipe;
private int smeltTimeTotal;
private int smeltTime;
private int burnTime;
private int fuelTime;
public EndStoneSmelterBlockEntity() {
super(EndBlockEntities.END_STONE_SMELTER);
this.inventory = DefaultedList.ofSize(4, ItemStack.EMPTY);
this.inventory = NonNullList.withSize(4, ItemStack.EMPTY);
this.recipesUsed = new Object2IntOpenHashMap<>();
this.propertyDelegate = new PropertyDelegate() {
public int get(int index) {
switch (index) {
case 0:
return EndStoneSmelterBlockEntity.this.burnTime;
case 1:
return EndStoneSmelterBlockEntity.this.fuelTime;
case 2:
return EndStoneSmelterBlockEntity.this.smeltTime;
case 3:
return EndStoneSmelterBlockEntity.this.smeltTimeTotal;
default:
return 0;
}
}
this.propertyDelegate = new ContainerData() {
public int get(int index) {
switch(index) {
case 0:
return EndStoneSmelterBlockEntity.this.burnTime;
case 1:
return EndStoneSmelterBlockEntity.this.fuelTime;
case 2:
return EndStoneSmelterBlockEntity.this.smeltTime;
case 3:
return EndStoneSmelterBlockEntity.this.smeltTimeTotal;
default:
return 0;
}
}
public void set(int index, int value) {
switch (index) {
case 0:
EndStoneSmelterBlockEntity.this.burnTime = value;
break;
case 1:
EndStoneSmelterBlockEntity.this.fuelTime = value;
break;
case 2:
EndStoneSmelterBlockEntity.this.smeltTime = value;
break;
case 3:
EndStoneSmelterBlockEntity.this.smeltTimeTotal = value;
}
}
public void set(int index, int value) {
switch(index) {
case 0:
EndStoneSmelterBlockEntity.this.burnTime = value;
break;
case 1:
EndStoneSmelterBlockEntity.this.fuelTime = value;
break;
case 2:
EndStoneSmelterBlockEntity.this.smeltTime = value;
break;
case 3:
EndStoneSmelterBlockEntity.this.smeltTimeTotal = value;
}
}
public int size() {
return 4;
}
};
public int getCount() {
return 4;
}
};
}
private boolean isBurning() {
@ -110,7 +109,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
}
@Override
public int size() {
public int getContainerSize() {
return this.inventory.size();
}
@ -129,103 +128,101 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
}
@Override
public ItemStack getStack(int slot) {
public ItemStack getItem(int slot) {
return this.inventory.get(slot);
}
@Override
public ItemStack removeStack(int slot, int amount) {
return Inventories.splitStack(this.inventory, slot, amount);
public ItemStack removeItem(int slot, int amount) {
return ContainerHelper.removeItem(this.inventory, slot, amount);
}
@Override
public ItemStack removeStack(int slot) {
return Inventories.removeStack(this.inventory, slot);
public ItemStack removeItemNoUpdate(int slot) {
return ContainerHelper.takeItem(this.inventory, slot);
}
@Override
public void setStack(int slot, ItemStack stack) {
public void setItem(int slot, ItemStack stack) {
ItemStack itemStack = this.inventory.get(slot);
boolean stackValid = !stack.isEmpty() && stack.isItemEqualIgnoreDamage(itemStack)
&& ItemStack.areTagsEqual(stack, itemStack);
boolean stackValid = !stack.isEmpty() && stack.sameItem(itemStack) && ItemStack.tagMatches(stack, itemStack);
this.inventory.set(slot, stack);
if (stack.getCount() > getMaxCountPerStack()) {
stack.setCount(getMaxCountPerStack());
if (stack.getCount() > getMaxStackSize()) {
stack.setCount(getMaxStackSize());
}
if ((slot == 0 || slot == 1) && !stackValid) {
this.smeltTimeTotal = this.getSmeltTime();
this.smeltTime = 0;
this.markDirty();
this.setChanged();
}
}
protected int getSmeltTime() {
assert this.world != null;
int smeltTime = this.world.getRecipeManager().getFirstMatch(AlloyingRecipe.TYPE, this, world)
assert this.level != null;
int smeltTime = this.level.getRecipeManager().getRecipeFor(AlloyingRecipe.TYPE, this, level)
.map(AlloyingRecipe::getSmeltTime).orElse(0);
if (smeltTime == 0) {
smeltTime = this.world.getRecipeManager().getFirstMatch(RecipeType.BLASTING, this, world)
.map(BlastingRecipe::getCookTime).orElse(200);
smeltTime = this.level.getRecipeManager().getRecipeFor(RecipeType.BLASTING, this, level)
.map(BlastingRecipe::getCookingTime).orElse(200);
smeltTime /= 1.5;
}
return smeltTime;
}
public void dropExperience(Player player) {
assert world != null;
assert level != null;
List<Recipe<?>> list = Lists.newArrayList();
for (Entry<ResourceLocation> entry : this.recipesUsed.object2IntEntrySet()) {
world.getRecipeManager().get(entry.getKey()).ifPresent((recipe) -> {
level.getRecipeManager().byKey(entry.getKey()).ifPresent((recipe) -> {
list.add(recipe);
if (recipe instanceof AlloyingRecipe) {
AlloyingRecipe alloying = (AlloyingRecipe) recipe;
this.dropExperience(player.world, player.getPos(), entry.getIntValue(), alloying.getExperience());
this.dropExperience(player.level, player.position(), entry.getIntValue(), alloying.getExperience());
} else {
BlastingRecipe blasting = (BlastingRecipe) recipe;
this.dropExperience(player.world, player.getPos(), entry.getIntValue(), blasting.getExperience());
this.dropExperience(player.level, player.position(), entry.getIntValue(), blasting.getExperience());
}
});
}
player.unlockRecipes(list);
player.awardRecipes(list);
this.recipesUsed.clear();
}
private void dropExperience(Level world, Vec3d vec3d, int i, float f) {
private void dropExperience(Level world, Vec3 vec3d, int i, float f) {
int j = Mth.floor(i * f);
float g = Mth.fractionalPart(i * f);
float g = Mth.frac(i * f);
if (g != 0.0F && Math.random() < g) {
j++;
}
while (j > 0) {
int k = ExperienceOrbEntity.roundToOrbSize(j);
while(j > 0) {
int k = ExperienceOrb.getExperienceValue(j);
j -= k;
world.spawnEntity(new ExperienceOrbEntity(world, vec3d.x, vec3d.y, vec3d.z, k));
world.addFreshEntity(new ExperienceOrb(world, vec3d.x, vec3d.y, vec3d.z, k));
}
}
@Override
public boolean canPlayerUse(Player player) {
assert this.world != null;
if (this.world.getBlockEntity(this.pos) != this) {
public boolean stillValid(Player player) {
assert this.level != null;
if (this.level.getBlockEntity(this.worldPosition) != this) {
return false;
}
return player.squaredDistanceTo(this.pos.getX() + 0.5D, this.pos.getY() + 0.5D,
this.pos.getZ() + 0.5D) <= 64.0D;
return player.distanceToSqr(this.worldPosition.getX() + 0.5D, this.worldPosition.getY() + 0.5D, this.worldPosition.getZ() + 0.5D) <= 64.0D;
}
@Override
public void clear() {
public void clearContent() {
this.inventory.clear();
}
@Override
protected Text getContainerName() {
return new TranslatableText(String.format("block.%s.%s", BetterEnd.MOD_ID, EndStoneSmelter.ID));
protected Component getDefaultName() {
return new TranslatableComponent(String.format("block.%s.%s", BetterEnd.MOD_ID, EndStoneSmelter.ID));
}
@Override
protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) {
protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) {
return new EndStoneSmelterScreenHandler(syncId, playerInventory, this, propertyDelegate);
}
@ -237,18 +234,17 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
}
boolean burning = this.isBurning();
assert this.world != null;
if (!this.world.isClientSide) {
assert this.level != null;
if (!this.level.isClientSide) {
ItemStack fuel = this.inventory.get(2);
if (!burning && (fuel.isEmpty() || inventory.get(0).isEmpty() && inventory.get(1).isEmpty())) {
if (smeltTime > 0) {
this.smeltTime = Mth.clamp(smeltTime - 2, 0, smeltTimeTotal);
}
} else {
Recipe<?> recipe = this.world.getRecipeManager().getFirstMatch(AlloyingRecipe.TYPE, this, world)
.orElse(null);
Recipe<?> recipe = this.level.getRecipeManager().getRecipeFor(AlloyingRecipe.TYPE, this, level).orElse(null);
if (recipe == null) {
recipe = this.world.getRecipeManager().getFirstMatch(RecipeType.BLASTING, this, world).orElse(null);
recipe = this.level.getRecipeManager().getRecipeFor(RecipeType.BLASTING, this, level).orElse(null);
}
boolean accepted = this.canAcceptRecipeOutput(recipe);
if (!burning && accepted) {
@ -258,13 +254,13 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
if (burning) {
if (!fuel.isEmpty()) {
Item item = fuel.getItem();
fuel.decrement(1);
fuel.shrink(1);
if (fuel.isEmpty()) {
Item remainFuel = item.getRecipeRemainder();
Item remainFuel = item.getCraftingRemainingItem();
this.inventory.set(2, remainFuel == null ? ItemStack.EMPTY : new ItemStack(remainFuel));
}
}
this.markDirty();
this.setChanged();
}
}
@ -274,7 +270,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
this.smeltTime = 0;
this.smeltTimeTotal = this.getSmeltTime();
this.craftRecipe(recipe);
this.markDirty();
this.setChanged();
}
} else {
this.smeltTime = 0;
@ -282,23 +278,24 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
}
if (initialBurning != burning) {
this.world.setBlockAndUpdate(pos, world.getBlockState(pos).with(EndStoneSmelter.LIT, burning), 3);
this.markDirty();
this.level.setBlock(worldPosition, level.getBlockState(worldPosition).setValue(EndStoneSmelter.LIT, burning), 3);
this.setChanged();
}
}
}
protected boolean canAcceptRecipeOutput(Recipe<?> recipe) {
if (recipe == null)
return false;
if (recipe == null) return false;
boolean validInput;
if (recipe instanceof AlloyingRecipe) {
validInput = !inventory.get(0).isEmpty() && !inventory.get(1).isEmpty();
validInput = !inventory.get(0).isEmpty() &&
!inventory.get(1).isEmpty();
} else {
validInput = !inventory.get(0).isEmpty() || !inventory.get(1).isEmpty();
validInput = !inventory.get(0).isEmpty() ||
!inventory.get(1).isEmpty();
}
if (validInput) {
ItemStack result = recipe.getOutput();
ItemStack result = recipe.getResultItem();
if (result.isEmpty()) {
return false;
}
@ -308,55 +305,54 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
if (output.isEmpty()) {
return true;
}
if (!output.isItemEqualIgnoreDamage(result)) {
if (!output.sameItem(result)) {
return false;
}
if (outCount < this.getMaxCountPerStack() && outCount < output.getMaxCount()) {
return this.getMaxCountPerStack() >= total;
if (outCount < this.getMaxStackSize() && outCount < output.getMaxStackSize()) {
return this.getMaxStackSize() >= total;
}
return output.getCount() < result.getMaxCount();
return output.getCount() < result.getMaxStackSize();
}
return false;
}
private void craftRecipe(Recipe<?> recipe) {
if (recipe == null || !canAcceptRecipeOutput(recipe))
return;
ItemStack result = recipe.getOutput();
if (recipe == null || !canAcceptRecipeOutput(recipe)) return;
ItemStack result = recipe.getResultItem();
ItemStack output = this.inventory.get(3);
if (output.isEmpty()) {
this.inventory.set(3, result.copy());
} else if (output.getItem() == result.getItem()) {
output.increment(result.getCount());
output.grow(result.getCount());
}
assert this.world != null;
if (!this.world.isClientSide) {
this.setLastRecipe(recipe);
assert this.level != null;
if (!this.level.isClientSide) {
this.setRecipeUsed(recipe);
}
if (recipe instanceof AlloyingRecipe) {
this.inventory.get(0).decrement(1);
this.inventory.get(1).decrement(1);
this.inventory.get(0).shrink(1);
this.inventory.get(1).shrink(1);
} else {
if (!this.inventory.get(0).isEmpty()) {
this.inventory.get(0).decrement(1);
this.inventory.get(0).shrink(1);
} else {
this.inventory.get(1).decrement(1);
this.inventory.get(1).shrink(1);
}
}
}
@Override
public void provideRecipeInputs(RecipeFinder finder) {
public void fillStackedContents(StackedContents finder) {
for (ItemStack itemStack : this.inventory) {
finder.addItem(itemStack);
finder.accountStack(itemStack);
}
}
@Override
public void setLastRecipe(Recipe<?> recipe) {
public void setRecipeUsed(Recipe<?> recipe) {
if (recipe != null) {
ResourceLocation recipeId = recipe.getId();
this.recipesUsed.addTo(recipeId, 1);
@ -365,12 +361,12 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
}
@Override
public Recipe<?> getLastRecipe() {
public Recipe<?> getRecipeUsed() {
return this.lastRecipe;
}
@Override
public int[] getAvailableSlots(Direction side) {
public int[] getSlotsForFace(Direction side) {
if (side == Direction.DOWN) {
return BOTTOM_SLOTS;
}
@ -378,12 +374,12 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
}
@Override
public boolean canInsert(int slot, ItemStack stack, Direction dir) {
return this.isValid(slot, stack);
public boolean canPlaceItemThroughFace(int slot, ItemStack stack, Direction dir) {
return this.canPlaceItem(slot, stack);
}
@Override
public boolean canExtract(int slot, ItemStack stack, Direction dir) {
public boolean canTakeItemThroughFace(int slot, ItemStack stack, Direction dir) {
if (dir == Direction.DOWN && slot == 2) {
return stack.getItem() == Items.BUCKET;
}
@ -397,38 +393,38 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
Item item = fuel.getItem();
return AVAILABLE_FUELS.getOrDefault(item, getFabricFuel(fuel));
}
@Override
public void fromTag(BlockState state, CompoundTag tag) {
super.fromTag(state, tag);
this.inventory = DefaultedList.ofSize(size(), ItemStack.EMPTY);
Inventories.fromTag(tag, inventory);
public void load(BlockState state, CompoundTag tag) {
super.load(state, tag);
this.inventory = NonNullList.withSize(getContainerSize(), ItemStack.EMPTY);
ContainerHelper.loadAllItems(tag, inventory);
this.burnTime = tag.getShort("BurnTime");
this.fuelTime = tag.getShort("FuelTime");
this.smeltTime = tag.getShort("SmeltTime");
this.smeltTimeTotal = tag.getShort("SmeltTimeTotal");
CompoundTag compoundTag = tag.getCompound("RecipesUsed");
for (String id : compoundTag.getKeys()) {
for (String id : compoundTag.getAllKeys()) {
this.recipesUsed.put(new ResourceLocation(id), compoundTag.getInt(id));
}
}
@Override
public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
public CompoundTag save(CompoundTag tag) {
super.save(tag);
tag.putShort("BurnTime", (short) burnTime);
tag.putShort("FuelTime", (short) fuelTime);
tag.putShort("SmeltTime", (short) smeltTime);
tag.putShort("SmeltTimeTotal", (short) smeltTimeTotal);
Inventories.toTag(tag, inventory);
ContainerHelper.saveAllItems(tag, inventory);
CompoundTag usedRecipes = new CompoundTag();
this.recipesUsed.forEach((identifier, integer) -> usedRecipes.putInt(identifier.toString(), integer));
tag.put("RecipesUsed", usedRecipes);
return tag;
}
public boolean isValid(int slot, ItemStack stack) {
public boolean canPlaceItem(int slot, ItemStack stack) {
if (slot == 3) {
return false;
} else if (slot != 2) {
@ -449,14 +445,14 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity
public static Map<Item, Integer> availableFuels() {
return AVAILABLE_FUELS;
}
private static int getFabricFuel(ItemStack stack) {
Integer ticks = FuelRegistry.INSTANCE.get(stack.getItem());
return ticks == null ? 0 : ticks;
}
static {
AbstractFurnaceBlockEntity.createFuelTimeMap().forEach((item, time) -> {
AbstractFurnaceBlockEntity.getFuel().forEach((item, time) -> {
if (time >= 2000) {
registerFuel(item, time);
}