Commit 1467a860 authored by mrAppleXZ's avatar mrAppleXZ 💾

The TileEntity syncing refactoring.

parent 199feec2
......@@ -34,6 +34,11 @@ public class PMMultiblock extends Multiblock
this.tier = tier;
}
public PMMultiblock()
{
setItemCreativeTab(PMCreativeTab.INSTANCE);
}
@Override
public Optional<Rotation> checkMultiblock(World w, BlockPos pos, @Nullable EntityPlayer p, @Nullable EnumHand hand)
{
......
package ru.pearx.purmag.common.recipes.crushing;
import net.minecraft.item.ItemStack;
/*
* Created by mrAppleXZ on 17.02.18 19:55.
*/
public class CrushingOutput
{
private ItemStack stack;
private float chance;
public CrushingOutput(ItemStack stack, float chance)
{
this.stack = stack;
this.chance = chance;
}
public ItemStack getStack()
{
return stack;
}
public float getChance()
{
return chance;
}
}
......@@ -3,21 +3,25 @@ package ru.pearx.purmag.common.recipes.crushing;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.NonNullList;
import ru.pearx.purmag.common.tiles.TileStoneCrusher;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
/*
* Created by mrAppleXZ on 15.02.18 13:27.
*/
public interface ICrushingRecipe
{
Ingredient getInput();
ItemStack getOutput();
default boolean isInputMatches(ItemStack stack)
{
return getInput().apply(stack);
}
default ItemStack newOutput()
interface ChanceModifier
{
return getOutput().copy();
float apply(float f);
}
boolean isInputMatches(ItemStack stack);
NonNullList<ItemStack> createOutputs(Random rand, @Nullable ChanceModifier modifier);
}
package ru.pearx.purmag.common.recipes.crushing;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.NonNullList;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Random;
/*
* Created by mrAppleXZ on 17.02.18 19:15.
*/
public interface IStandardCrushingRecipe extends ICrushingRecipe
{
Ingredient getInput();
List<CrushingOutput> getOutputs();
@Override
default boolean isInputMatches(ItemStack stack)
{
return getInput().apply(stack);
}
@Override
default NonNullList<ItemStack> createOutputs(Random rand, @Nullable ChanceModifier modifier)
{
NonNullList<ItemStack> lst = NonNullList.create();
for(CrushingOutput out : getOutputs())
{
if(rand.nextFloat() <= (modifier != null ? modifier.apply(out.getChance()) : out.getChance()))
{
lst.add(out.getStack().copy());
}
}
return lst;
}
}
......@@ -6,6 +6,7 @@ import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import ru.pearx.libmc.common.nbt.NBTTagCompoundBuilder;
import ru.pearx.libmc.common.tiles.TileSyncable;
import javax.annotation.Nonnull;
......@@ -22,7 +23,7 @@ public abstract class TileAbstractSingleItem extends TileSyncable
protected void onContentsChanged(int slot)
{
TileAbstractSingleItem.this.markDirty();
sendUpdatesToClients();
sendUpdates(new NBTTagCompoundBuilder().setTag("items", serializeNBT()).build());
}
@Nonnull
......@@ -51,19 +52,16 @@ public abstract class TileAbstractSingleItem extends TileSyncable
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
public void readCustomData(NBTTagCompound tag)
{
super.writeToNBT(compound);
compound.setTag("items", handler.serializeNBT());
return compound;
if (tag.hasKey("items"))
handler.deserializeNBT((NBTTagCompound) tag.getTag("items"));
}
@Override
public void readFromNBT(NBTTagCompound compound)
public void writeCustomData(NBTTagCompound tag)
{
super.readFromNBT(compound);
if (compound.hasKey("items"))
handler.deserializeNBT((NBTTagCompound) compound.getTag("items"));
tag.setTag("items", handler.serializeNBT());
}
public abstract boolean isItemValid(ItemStack stack);
......
......@@ -15,8 +15,9 @@ import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import ru.pearx.lib.HashingUtils;
import ru.pearx.libmc.common.PXLCapabilities;
import ru.pearx.libmc.common.animation.AnimationElement;
import ru.pearx.libmc.common.animation.AnimationStateManager;
import ru.pearx.libmc.common.caps.animation.AnimationElement;
import ru.pearx.libmc.common.caps.animation.AnimationStateManager;
import ru.pearx.libmc.common.nbt.NBTTagCompoundBuilder;
import ru.pearx.libmc.common.tiles.TileSyncable;
import ru.pearx.purmag.common.SoundRegistry;
import ru.pearx.purmag.common.inventory.ContainerCodeStorage;
......@@ -174,7 +175,7 @@ public class TileCodeStorage extends TileSyncable
setUnlocked(true);
setHash(null);
setText(null);
sendUpdatesToClients();
sendUpdates(serializeLockUpdate(new NBTTagCompound()));
return true;
}
return false;
......@@ -191,7 +192,7 @@ public class TileCodeStorage extends TileSyncable
setText(text);
setCode(code);
setUnlocked(false);
sendUpdatesToClients();
sendUpdates(serializeLockUpdate(new NBTTagCompound()));
return true;
}
}
......@@ -216,25 +217,29 @@ public class TileCodeStorage extends TileSyncable
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
public void readCustomData(NBTTagCompound tag)
{
super.writeToNBT(compound);
serializeMin(compound);
compound.setBoolean("lockable", isLockable());
return compound;
deserializeMin(tag);
if(tag.hasKey("lockable", Constants.NBT.TAG_BYTE))
setLockable(tag.getBoolean("lockable"));
}
@Override
public void readFromNBT(NBTTagCompound compound)
public void writeCustomData(NBTTagCompound tag)
{
super.readFromNBT(compound);
deserializeMin(compound);
setLockable(compound.getBoolean("lockable"));
serializeMin(tag);
tag.setBoolean("lockable", isLockable());
}
public NBTTagCompound serializeMin(NBTTagCompound compound)
{
compound.setTag("items", handler.serializeNBT());
serializeLockUpdate(compound);
return compound;
}
private NBTTagCompound serializeLockUpdate(NBTTagCompound compound)
{
if (getText() != null)
compound.setString("text", getText());
if (getHash() != null)
......@@ -245,11 +250,13 @@ public class TileCodeStorage extends TileSyncable
public void deserializeMin(NBTTagCompound compound)
{
handler.deserializeNBT(compound.getCompoundTag("items"));
if(compound.hasKey("items", Constants.NBT.TAG_COMPOUND))
handler.deserializeNBT(compound.getCompoundTag("items"));
if (compound.hasKey("text", Constants.NBT.TAG_STRING))
setText(compound.getString("text"));
if (compound.hasKey("hash", Constants.NBT.TAG_BYTE_ARRAY))
setHash(compound.getByteArray("hash"));
setUnlocked(compound.getBoolean("unlocked"));
if(compound.hasKey("unlocked", Constants.NBT.TAG_BYTE))
setUnlocked(compound.getBoolean("unlocked"));
}
}
......@@ -6,11 +6,13 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import ru.pearx.libmc.common.ItemStackUtils;
import ru.pearx.libmc.common.nbt.NBTTagCompoundBuilder;
import ru.pearx.libmc.common.tiles.TileSyncable;
import ru.pearx.purmag.PurMag;
import ru.pearx.purmag.common.magibench.MagibenchRegistry;
......@@ -43,7 +45,7 @@ public class TileMagibench extends TileSyncable
protected void onContentsChanged(int slot)
{
TileMagibench.this.markDirty();
sendUpdatesToClients();
sendUpdates(new NBTTagCompoundBuilder().setTag("items", serializeNBT()).build());
}
}
......@@ -66,7 +68,7 @@ public class TileMagibench extends TileSyncable
this.tier = tier;
handler = new Handler(t.getWidth() * t.getHeight());
if(sync)
sendUpdatesToClients();
sendUpdates(new NBTTagCompoundBuilder().setInteger("tier", tier).build());
}
@Nullable
......@@ -83,19 +85,19 @@ public class TileMagibench extends TileSyncable
}
@Override
public void readFromNBT(NBTTagCompound compound)
public void readCustomData(NBTTagCompound tag)
{
super.readFromNBT(compound);
setTier(compound.getInteger("tier"), false);
handler.deserializeNBT(compound.getCompoundTag("items"));
if(tag.hasKey("tier", Constants.NBT.TAG_INT))
setTier(tag.getInteger("tier"), false);
if(tag.hasKey("items", Constants.NBT.TAG_COMPOUND))
handler.deserializeNBT(tag.getCompoundTag("items"));
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
public void writeCustomData(NBTTagCompound tag)
{
compound.setInteger("tier", getTier());
compound.setTag("items", handler.serializeNBT());
return super.writeToNBT(compound);
tag.setInteger("tier", getTier());
tag.setTag("items", handler.serializeNBT());
}
public boolean canWork()
......
package ru.pearx.purmag.common.tiles;
import net.minecraft.nbt.NBTTagCompound;
import ru.pearx.libmc.common.nbt.NBTTagCompoundBuilder;
import ru.pearx.libmc.common.tiles.TileSyncable;
import ru.pearx.purmag.PurMag;
......@@ -21,22 +22,19 @@ public class TileSingleSip extends TileSyncable
this.type = type;
markDirty();
if (sync)
sendUpdatesToClients();
sendUpdates(new NBTTagCompoundBuilder().setString("sip_type", type).build());
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
public void readCustomData(NBTTagCompound tag)
{
super.writeToNBT(compound);
compound.setString("sip_type", getType());
return compound;
if (tag.hasKey("sip_type"))
setType(tag.getString("sip_type"), false);
}
@Override
public void readFromNBT(NBTTagCompound compound)
public void writeCustomData(NBTTagCompound tag)
{
super.readFromNBT(compound);
if (compound.hasKey("sip_type"))
setType(compound.getString("sip_type"), false);
tag.setString("sip_type", getType());
}
}
package ru.pearx.purmag.common.tiles;
import net.minecraft.util.math.BlockPos;
import ru.pearx.libmc.common.structure.multiblock.IMultiblockPart;
import ru.pearx.libmc.common.structure.multiblock.events.MultiblockActivatedEvent;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import ru.pearx.libmc.common.nbt.NBTTagCompoundBuilder;
import ru.pearx.libmc.common.tiles.TileMultiblockMaster;
import ru.pearx.purmag.common.blocks.multiblock.MultiblockRegistry;
import javax.annotation.Nullable;
/*
* Created by mrAppleXZ on 16.11.17 20:31.
*/
public class TileStoneCrusher extends TileMultiblockMaster
{
public ItemStackHandler handler = new ItemStackHandler()
{
@Override
protected void onContentsChanged(int slot)
{
TileStoneCrusher.this.markDirty();
sendUpdates(new NBTTagCompoundBuilder().setTag("items", serializeNBT()).build());
}
};
@Override
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing)
{
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
}
@Nullable
@Override
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing)
{
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(handler);
return super.getCapability(capability, facing);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
{
super.writeToNBT(compound);
compound.setTag("items", handler.serializeNBT());
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound)
{
super.readFromNBT(compound);
if (compound.hasKey("items", Constants.NBT.TAG_COMPOUND))
handler.deserializeNBT((NBTTagCompound) compound.getTag("items"));
}
}
package ru.pearx.purmag.common.tiles;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraftforge.common.util.Constants;
import ru.pearx.libmc.common.nbt.NBTTagCompoundBuilder;
import ru.pearx.libmc.common.tiles.TileSyncable;
/*
......@@ -19,23 +22,21 @@ public class TileWallIfTablet extends TileSyncable
{
this.tier = tier;
markDirty();
if (sync)
sendUpdatesToClients();
sendUpdates(new NBTTagCompoundBuilder().setInteger("tier", tier).build());
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
public void readCustomData(NBTTagCompound tag)
{
super.writeToNBT(compound);
compound.setInteger("tier", getTier());
return compound;
if (tag.hasKey("tier", Constants.NBT.TAG_INT))
setTier(tag.getInteger("tier"), false);
}
@Override
public void readFromNBT(NBTTagCompound compound)
public void writeCustomData(NBTTagCompound tag)
{
super.readFromNBT(compound);
if (compound.hasKey("tier"))
setTier(compound.getInteger("tier"), false);
tag.setInteger("tier", getTier());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment