save the current ritual

This commit is contained in:
Ellpeck 2018-10-16 18:07:03 +02:00
parent d290eb3eff
commit 9645d09063
2 changed files with 62 additions and 23 deletions

View file

@ -5,18 +5,21 @@ import de.ellpeck.naturesaura.blocks.ModBlocks;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticleStream;
import de.ellpeck.naturesaura.packet.PacketParticles;
import de.ellpeck.naturesaura.recipes.TreeRitualRecipe;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.BlockLog;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.Map;
public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
@ -42,27 +45,29 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
public ItemStack stack = ItemStack.EMPTY;
private BlockPos ritualPos;
private Map<TileEntityWoodStand, ItemStack> involvedStands;
private TreeRitualRecipe recipe;
private Map<BlockPos, ItemStack> involvedStands;
private ItemStack output;
private int totalTime;
private int timer;
public void setRitual(BlockPos pos, TreeRitualRecipe recipe, Map<TileEntityWoodStand, ItemStack> involvedStands) {
public void setRitual(BlockPos pos, ItemStack output, int totalTime, Map<BlockPos, ItemStack> involvedStands) {
this.ritualPos = pos;
this.recipe = recipe;
this.output = output;
this.totalTime = totalTime;
this.involvedStands = involvedStands;
}
@Override
public void update() {
if (!this.world.isRemote) {
if (this.ritualPos != null && this.involvedStands != null && this.recipe != null) {
if (this.ritualPos != null && this.involvedStands != null && this.output != null && this.totalTime > 0) {
if (this.isRitualOkay(this.world)) {
this.timer++;
if (this.timer % 3 == 0) {
for (TileEntityWoodStand stand : this.involvedStands.keySet()) {
if (this.timer % 3 == 0 && this.timer < this.totalTime / 2) {
for (BlockPos pos : this.involvedStands.keySet()) {
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticleStream(
(float) stand.pos.getX() + 0.5F, (float) stand.pos.getY() + 1.25F, (float) stand.pos.getZ() + 0.5F,
(float) pos.getX() + 0.5F, (float) pos.getY() + 1.25F, (float) pos.getZ() + 0.5F,
this.ritualPos.getX() + 0.5F, this.ritualPos.getY() + 2.5F, this.ritualPos.getZ() + 0.5F,
this.world.rand.nextFloat() * 0.02F + 0.02F, 0xFF00FF, this.world.rand.nextFloat() * 1F + 1F
));
@ -73,7 +78,7 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
new PacketParticles(this.ritualPos.getX(), this.ritualPos.getY(), this.ritualPos.getZ(), 0));
}
if (this.timer >= this.recipe.time) {
if (this.timer >= this.totalTime) {
this.recurseTreeDestruction(this.ritualPos, this.ritualPos);
for (BlockPos offset : GOLD_POWDER_POSITIONS) {
this.world.setBlockToAir(this.ritualPos.add(offset));
@ -81,7 +86,7 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
EntityItem item = new EntityItem(this.world,
this.ritualPos.getX() + 0.5, this.ritualPos.getY() + 4.5, this.ritualPos.getZ() + 0.5,
this.recipe.result.copy());
this.output.copy());
this.world.spawnEntity(item);
PacketHandler.sendToAllAround(this.world, this.pos, 32,
@ -89,21 +94,23 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
this.ritualPos = null;
this.involvedStands = null;
this.recipe = null;
this.output = null;
this.totalTime = 0;
this.timer = 0;
} else if (this.timer >= this.recipe.time / 2 && !this.involvedStands.isEmpty()) {
for (TileEntityWoodStand stand : this.involvedStands.keySet()) {
} else if (this.timer == this.totalTime / 2) {
for (BlockPos pos : this.involvedStands.keySet()) {
TileEntityWoodStand stand = (TileEntityWoodStand) this.world.getTileEntity(pos);
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(stand.pos.getX(), stand.pos.getY(), stand.pos.getZ(), 1));
stand.stack = ItemStack.EMPTY;
stand.sendToClients();
}
this.involvedStands.clear();
}
} else {
this.ritualPos = null;
this.involvedStands = null;
this.recipe = null;
this.output = null;
this.totalTime = 0;
this.timer = 0;
}
}
@ -128,9 +135,9 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
}
private boolean isRitualOkay(World world) {
for (Map.Entry<TileEntityWoodStand, ItemStack> entry : this.involvedStands.entrySet()) {
TileEntityWoodStand stand = entry.getKey();
if (stand.isInvalid() || !stand.stack.isItemEqual(entry.getValue())) {
for (Map.Entry<BlockPos, ItemStack> entry : this.involvedStands.entrySet()) {
TileEntity tile = world.getTileEntity(entry.getKey());
if (!(tile instanceof TileEntityWoodStand) || (this.timer < this.totalTime / 2 && !((TileEntityWoodStand) tile).stack.isItemEqual(entry.getValue()))) {
return false;
}
}
@ -141,12 +148,44 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
public void writeNBT(NBTTagCompound compound, boolean syncing) {
super.writeNBT(compound, syncing);
compound.setTag("item", this.stack.writeToNBT(new NBTTagCompound()));
//TODO Save info about the current ritual somehow
if (this.ritualPos != null && this.involvedStands != null && this.output != null && this.totalTime > 0) {
compound.setLong("ritual_pos", this.ritualPos.toLong());
compound.setInteger("timer", this.timer);
compound.setInteger("total_time", this.totalTime);
compound.setTag("output", this.output.writeToNBT(new NBTTagCompound()));
NBTTagList list = new NBTTagList();
for (Map.Entry<BlockPos, ItemStack> entry : this.involvedStands.entrySet()) {
NBTTagCompound tag = new NBTTagCompound();
tag.setLong("pos", entry.getKey().toLong());
tag.setTag("item", entry.getValue().writeToNBT(new NBTTagCompound()));
list.appendTag(tag);
}
compound.setTag("stands", list);
}
}
@Override
public void readNBT(NBTTagCompound compound, boolean syncing) {
super.readNBT(compound, syncing);
this.stack = new ItemStack(compound.getCompoundTag("item"));
if (compound.hasKey("ritual_pos") && compound.hasKey("stands") && compound.hasKey("output") && compound.hasKey("total_time")) {
this.ritualPos = BlockPos.fromLong(compound.getLong("ritual_pos"));
this.timer = compound.getInteger("timer");
this.totalTime = compound.getInteger("total_time");
this.output = new ItemStack(compound.getCompoundTag("output"));
this.involvedStands = new HashMap<>();
NBTTagList list = compound.getTagList("stands", 10);
for (NBTBase base : list) {
NBTTagCompound tag = (NBTTagCompound) base;
this.involvedStands.put(
BlockPos.fromLong(tag.getLong("pos")),
new ItemStack(tag.getCompoundTag("item"))
);
}
}
}
}

View file

@ -41,14 +41,14 @@ public class TerrainGenEvents {
if (!saplingStack.isEmpty()) {
for (TreeRitualRecipe recipe : TreeRitualRecipe.RECIPES) {
if (recipe.matchesItems(saplingStack, usableItems)) {
Map<TileEntityWoodStand, ItemStack> actuallyInvolved = new HashMap<>();
Map<BlockPos, ItemStack> actuallyInvolved = new HashMap<>();
List<ItemStack> stillRequired = new ArrayList<>(Arrays.asList(recipe.items));
TileEntityWoodStand toPick = null;
for (TileEntityWoodStand stand : stands) {
int index = Helper.getItemIndex(stillRequired, stand.stack);
if (index >= 0) {
actuallyInvolved.put(stand, stand.stack);
actuallyInvolved.put(stand.getPos(), stand.stack);
stillRequired.remove(index);
if (toPick == null) {
@ -58,7 +58,7 @@ public class TerrainGenEvents {
}
if (stillRequired.isEmpty()) {
toPick.setRitual(pos, recipe, actuallyInvolved);
toPick.setRitual(pos, recipe.result, recipe.time, actuallyInvolved);
break;
}
}