Infusion crafting

This commit is contained in:
Aleksey 2020-11-09 16:45:21 +03:00
parent beab6ce10a
commit 16039bcb47
8 changed files with 48 additions and 19 deletions

View file

@ -106,14 +106,12 @@ public class BlockPedestal extends BlockBaseNotFull implements BlockEntityProvid
if (pedestal.isEmpty()) { if (pedestal.isEmpty()) {
ItemStack itemStack = player.getStackInHand(hand); ItemStack itemStack = player.getStackInHand(hand);
if (itemStack.isEmpty()) return ActionResult.CONSUME; if (itemStack.isEmpty()) return ActionResult.CONSUME;
world.setBlockState(pos, state.with(HAS_ITEM, true)); pedestal.setStack(world, state, itemStack.split(1));
pedestal.setStack(0, itemStack.split(1));
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} else { } else {
ItemStack itemStack = pedestal.getStack(0); ItemStack itemStack = pedestal.getStack(0);
if (player.giveItemStack(itemStack)) { if (player.giveItemStack(itemStack)) {
world.setBlockState(pos, state.with(HAS_ITEM, false)); pedestal.removeStack(world, state);
pedestal.removeStack(0);
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }
return ActionResult.FAIL; return ActionResult.FAIL;

View file

@ -41,18 +41,19 @@ public class InfusionPedestalEntity extends PedestalBlockEntity {
@Override @Override
public void fromTag(BlockState state, CompoundTag tag) { public void fromTag(BlockState state, CompoundTag tag) {
super.fromTag(state, tag);
if (tag.contains("ritual")) { if (tag.contains("ritual")) {
this.linkedRitual = new InfusionRitual(world, pos); this.linkedRitual = new InfusionRitual(world, pos);
this.linkedRitual.fromTag(tag.getCompound("ritual")); this.linkedRitual.fromTag(tag.getCompound("ritual"));
} }
super.fromTag(state, tag);
} }
@Override @Override
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
if (hasRitual()) { if (hasRitual()) {
tag.put("ritual", linkedRitual.toTag(new CompoundTag())); tag.put("ritual", linkedRitual.toTag(new CompoundTag()));
} }
return super.toTag(tag); return tag;
} }
} }

View file

@ -8,6 +8,8 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.util.Tickable; import net.minecraft.util.Tickable;
import net.minecraft.world.World;
import ru.betterend.blocks.basis.BlockPedestal;
import ru.betterend.registry.EndBlockEntities; import ru.betterend.registry.EndBlockEntities;
public class PedestalBlockEntity extends BlockEntity implements Inventory, Tickable { public class PedestalBlockEntity extends BlockEntity implements Inventory, Tickable {
@ -58,11 +60,21 @@ public class PedestalBlockEntity extends BlockEntity implements Inventory, Ticka
return this.activeItem = ItemStack.EMPTY; return this.activeItem = ItemStack.EMPTY;
} }
public void removeStack(World world, BlockState state) {
world.setBlockState(pos, state.with(BlockPedestal.HAS_ITEM, false));
this.removeStack(0);
}
@Override @Override
public void setStack(int slot, ItemStack stack) { public void setStack(int slot, ItemStack stack) {
this.activeItem = stack; this.activeItem = stack;
} }
public void setStack(World world, BlockState state, ItemStack stack) {
world.setBlockState(pos, state.with(BlockPedestal.HAS_ITEM, true));
this.setStack(0, stack);
}
@Override @Override
public boolean canPlayerUse(PlayerEntity player) { public boolean canPlayerUse(PlayerEntity player) {
return true; return true;
@ -89,8 +101,9 @@ public class PedestalBlockEntity extends BlockEntity implements Inventory, Ticka
@Override @Override
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
tag.put("active_item", activeItem.toTag(new CompoundTag())); tag.put("active_item", activeItem.toTag(new CompoundTag()));
return super.toTag(tag); return tag;
} }
@Override @Override

View file

@ -3,6 +3,6 @@ package ru.betterend.interfaces;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
public interface CompoundSerializer<T> { public interface CompoundSerializer<T> {
public abstract CompoundTag toTag(CompoundTag tag); public abstract CompoundTag beToTag(CompoundTag tag);
public abstract T fromTag(CompoundTag tag); public abstract T beFromTag(CompoundTag tag);
} }

View file

@ -21,7 +21,7 @@ public abstract class IngredientMixin implements CompoundSerializer<Ingredient>
protected abstract void cacheMatchingStacks(); protected abstract void cacheMatchingStacks();
@Override @Override
public CompoundTag toTag(CompoundTag tag) { public CompoundTag beToTag(CompoundTag tag) {
this.cacheMatchingStacks(); this.cacheMatchingStacks();
int i = 0; int i = 0;
tag.putInt("length", matchingStacks.length); tag.putInt("length", matchingStacks.length);
@ -34,7 +34,7 @@ public abstract class IngredientMixin implements CompoundSerializer<Ingredient>
} }
@Override @Override
public Ingredient fromTag(CompoundTag tag) { public Ingredient beFromTag(CompoundTag tag) {
int length = tag.getInt("length"); int length = tag.getInt("length");
ItemStack[] stacks = new ItemStack[length]; ItemStack[] stacks = new ItemStack[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {

View file

@ -1,5 +1,6 @@
package ru.betterend.recipe; package ru.betterend.recipe;
import net.minecraft.item.Items;
import ru.betterend.recipe.builders.InfusionRecipe; import ru.betterend.recipe.builders.InfusionRecipe;
import ru.betterend.registry.EndBlocks; import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndItems; import ru.betterend.registry.EndItems;
@ -14,5 +15,18 @@ public class InfusionRecipes {
.addCatalyst(4, EndItems.CRYSTAL_SHARDS) .addCatalyst(4, EndItems.CRYSTAL_SHARDS)
.addCatalyst(6, EndItems.CRYSTAL_SHARDS) .addCatalyst(6, EndItems.CRYSTAL_SHARDS)
.build(); .build();
InfusionRecipe.Builder.create("eternal_crystal")
.setInput(EndItems.CRYSTAL_SHARDS)
.setOutput(EndItems.ETERNAL_CRYSTAL)
.addCatalyst(0, Items.END_CRYSTAL)
.addCatalyst(2, Items.END_CRYSTAL)
.addCatalyst(4, Items.END_CRYSTAL)
.addCatalyst(6, Items.END_CRYSTAL)
.addCatalyst(1, EndItems.ENDER_DUST)
.addCatalyst(3, EndItems.ENDER_DUST)
.addCatalyst(5, EndItems.ENDER_DUST)
.addCatalyst(7, EndItems.ENDER_DUST)
.build();
} }
} }

View file

@ -217,29 +217,28 @@ public class InfusionRecipe implements Recipe<InfusionRitual> {
public InfusionRecipe fromTag(CompoundTag tag) { public InfusionRecipe fromTag(CompoundTag tag) {
Identifier id = new Identifier(tag.getString("id")); Identifier id = new Identifier(tag.getString("id"));
InfusionRecipe recipe = new InfusionRecipe(id); InfusionRecipe recipe = new InfusionRecipe(id);
CompoundSerializer<Ingredient> inputSerializer = this.toSerializer(recipe.input); CompoundSerializer<Ingredient> ingredientSerializer = this.toSerializer(Ingredient.EMPTY);
recipe.input = inputSerializer.fromTag(tag.getCompound("input")); recipe.input = ingredientSerializer.beFromTag(tag.getCompound("input"));
recipe.output = ItemStack.fromTag(tag.getCompound("output")); recipe.output = ItemStack.fromTag(tag.getCompound("output"));
recipe.time = tag.getInt("time"); recipe.time = tag.getInt("time");
CompoundTag catalysts = tag.getCompound("catalysts"); CompoundTag catalysts = tag.getCompound("catalysts");
for(int i = 0; i < recipe.catalysts.length; i++) { for(int i = 0; i < recipe.catalysts.length; i++) {
String key = Integer.toString(i); String key = Integer.toString(i);
CompoundSerializer<Ingredient> cataSerializer = this.toSerializer(recipe.catalysts[i]); recipe.catalysts[i] = ingredientSerializer.beFromTag(catalysts.getCompound(key));
recipe.catalysts[i] = cataSerializer.fromTag(catalysts.getCompound(key));
} }
return recipe; return recipe;
} }
public CompoundTag toTag(InfusionRecipe recipe, CompoundTag tag) { public CompoundTag toTag(InfusionRecipe recipe, CompoundTag tag) {
CompoundSerializer<?> inputSerializer = this.toSerializer(recipe.input); CompoundSerializer<?> inputSerializer = this.toSerializer(recipe.input);
tag.put("input", inputSerializer.toTag(new CompoundTag())); tag.put("input", inputSerializer.beToTag(new CompoundTag()));
tag.put("output", recipe.output.toTag(new CompoundTag())); tag.put("output", recipe.output.toTag(new CompoundTag()));
tag.putInt("time", recipe.time); tag.putInt("time", recipe.time);
CompoundTag catalysts = new CompoundTag(); CompoundTag catalysts = new CompoundTag();
for(int i = 0; i < recipe.catalysts.length; i++) { for(int i = 0; i < recipe.catalysts.length; i++) {
String key = Integer.toString(i); String key = Integer.toString(i);
CompoundSerializer<?> cataSerializer = this.toSerializer(recipe.catalysts[i]); CompoundSerializer<?> cataSerializer = this.toSerializer(recipe.catalysts[i]);
catalysts.put(key, cataSerializer.toTag(new CompoundTag())); catalysts.put(key, cataSerializer.beToTag(new CompoundTag()));
} }
tag.put("catalysts", catalysts); tag.put("catalysts", catalysts);
return tag; return tag;

View file

@ -2,6 +2,7 @@ package ru.betterend.rituals;
import java.awt.Point; import java.awt.Point;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
@ -60,6 +61,7 @@ public class InfusionRitual implements Inventory {
this.activeRecipe = this.world.getRecipeManager().getFirstMatch(InfusionRecipe.TYPE, this, world).orElse(null); this.activeRecipe = this.world.getRecipeManager().getFirstMatch(InfusionRecipe.TYPE, this, world).orElse(null);
if (activeRecipe != null) { if (activeRecipe != null) {
this.time = this.activeRecipe.getInfusionTime(); this.time = this.activeRecipe.getInfusionTime();
this.progress = 0;
return true; return true;
} }
return false; return false;
@ -69,9 +71,11 @@ public class InfusionRitual implements Inventory {
if (!isValid() || !hasRecipe()) return; if (!isValid() || !hasRecipe()) return;
this.progress++; this.progress++;
if (progress == time) { if (progress == time) {
this.input.setStack(0, activeRecipe.craft(this)); BlockState inputState = world.getBlockState(input.getPos());
this.input.removeStack(world, inputState);
this.input.setStack(world, inputState, activeRecipe.craft(this));
for (PedestalBlockEntity catalyst : catalysts) { for (PedestalBlockEntity catalyst : catalysts) {
catalyst.clear(); catalyst.removeStack(world, world.getBlockState(catalyst.getPos()));
} }
this.activeRecipe = null; this.activeRecipe = null;
this.progress = 0; this.progress = 0;