Added client syncing for lasers

This commit is contained in:
Ellpeck 2015-10-21 00:22:50 +02:00
parent f045134434
commit 3435bcf8e4
10 changed files with 181 additions and 21 deletions

View file

@ -10,9 +10,15 @@
package ellpeck.actuallyadditions.blocks.render;
import ellpeck.actuallyadditions.misc.LaserRelayConnectionHandler;
import ellpeck.actuallyadditions.tile.TileEntityLaserRelay;
import ellpeck.actuallyadditions.util.WorldPos;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.tileentity.TileEntity;
import org.lwjgl.opengl.GL11;
import java.util.ArrayList;
public class ModelLaserRelay extends ModelBaseAA{
ModelRenderer bottom;
@ -164,6 +170,20 @@ public class ModelLaserRelay extends ModelBaseAA{
covering12.render(f);
}
@Override
public void renderExtra(float f, TileEntity tile){
TileEntityLaserRelay relay = (TileEntityLaserRelay)tile;
WorldPos thisPos = new WorldPos(relay.getWorldObj(), relay.xCoord, relay.yCoord, relay.zCoord);
ArrayList<LaserRelayConnectionHandler.ConnectionPair> network = LaserRelayConnectionHandler.getInstance().getNetworkFor(thisPos);
if(network != null){
for(LaserRelayConnectionHandler.ConnectionPair aPair : network){
if(aPair.contains(thisPos) && aPair.firstRelay.isEqual(thisPos)){
//TODO Make this work
}
}
}
}
@Override
public String getName(){
return "modelLaserRelay";

View file

@ -31,6 +31,7 @@ public class InitEvents{
Util.registerEvent(new BucketFillEvent());
Util.registerEvent(new LogoutEvent());
Util.registerEvent(new EntityConstructingEvent());
Util.registerEvent(new WorldLoadEvent());
MinecraftForge.TERRAIN_GEN_BUS.register(new WorldDecorationEvent());
}

View file

@ -0,0 +1,26 @@
/*
* This file ("WorldLoadEvent.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.event;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import ellpeck.actuallyadditions.misc.LaserRelayConnectionHandler;
import net.minecraftforge.event.world.WorldEvent;
public class WorldLoadEvent{
@SubscribeEvent
public void onLoad(WorldEvent.Load event){
if(LaserRelayConnectionHandler.getInstance() == null){
LaserRelayConnectionHandler.setInstance(new LaserRelayConnectionHandler());
}
}
}

View file

@ -35,10 +35,19 @@ public class ItemLaserWrench extends Item implements IActAddItemOrBlock{
this.setMaxStackSize(1);
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player){
if(player.isSneaking() && !world.isRemote){
ItemPhantomConnector.clearStorage(stack);
player.addChatComponentMessage(new ChatComponentText("Storage cleared!"));
}
return stack;
}
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10){
TileEntity tile = world.getTileEntity(x, y, z);
if(tile instanceof TileEntityLaserRelay){
if(tile instanceof TileEntityLaserRelay && !player.isSneaking()){
if(!world.isRemote){
if(ItemPhantomConnector.getStoredPosition(stack) == null){
ItemPhantomConnector.storeConnection(stack, x, y, z, world);
@ -54,6 +63,9 @@ public class ItemLaserWrench extends Item implements IActAddItemOrBlock{
if(LaserRelayConnectionHandler.getInstance().addConnection(savedPos, otherPos)){
player.addChatComponentMessage(new ChatComponentText("Connected!"));
ItemPhantomConnector.clearStorage(stack);
savedPos.update();
otherPos.update();
}
else{
player.addChatComponentMessage(new ChatComponentText("Couldn't connect!"));

View file

@ -14,7 +14,10 @@ import cofh.api.energy.IEnergyReceiver;
import ellpeck.actuallyadditions.tile.TileEntityLaserRelay;
import ellpeck.actuallyadditions.util.WorldPos;
import ellpeck.actuallyadditions.util.WorldUtil;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.util.ForgeDirection;
import java.util.ArrayList;
@ -31,12 +34,37 @@ public class LaserRelayConnectionHandler{
public ArrayList<ArrayList<ConnectionPair>> networks = new ArrayList<ArrayList<ConnectionPair>>();
public static LaserRelayConnectionHandler getInstance(){
if(instance == null){
instance = new LaserRelayConnectionHandler();
}
return instance;
}
public static void setInstance(LaserRelayConnectionHandler i){
instance = i;
}
public void writeNetworkToNBT(ArrayList<ConnectionPair> network, NBTTagCompound tag, String name){
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("NetworkSize", network.size());
for(int pair = 0; pair < network.size(); pair++){
network.get(pair).writeToNBT(compound, "Pair"+pair);
}
tag.setTag(name, compound);
}
public ArrayList<ConnectionPair> readNetworkFromNBT(NBTTagCompound tag, String name){
NBTTagCompound compound = tag.getCompoundTag(name);
int networkSize = compound.getInteger("NetworkSize");
ArrayList<ConnectionPair> network = new ArrayList<ConnectionPair>();
for(int pair = 0; pair < networkSize; pair++){
network.add(ConnectionPair.readFromNBT(compound, "Pair"+pair));
}
return network;
}
/**
* Gets a Network for a Relay
*/
@ -51,6 +79,21 @@ public class LaserRelayConnectionHandler{
return null;
}
/**
* Gets all Connections for a Relay
*/
public ArrayList<ConnectionPair> getConnectionsFor(WorldPos relay){
ArrayList<ConnectionPair> allPairs = new ArrayList<ConnectionPair>();
for(ArrayList<ConnectionPair> aNetwork : this.networks){
for(ConnectionPair pair : aNetwork){
if(pair.contains(relay)){
allPairs.add(pair);
}
}
}
return allPairs;
}
/**
* Adds a new connection between two relays
* (Puts it into the correct network!)
@ -170,5 +213,27 @@ public class LaserRelayConnectionHandler{
public String toString(){
return (this.firstRelay == null ? "-" : this.firstRelay.toString())+" | "+(this.secondRelay == null ? "-" : this.secondRelay.toString());
}
public void writeToNBT(NBTTagCompound compound, String name){
for(int i = 0; i < 2; i++){
WorldPos relay = i == 0 ? this.firstRelay : this.secondRelay;
compound.setInteger("world"+name+i, relay.getWorld().provider.dimensionId);
compound.setInteger("x"+name+i, relay.getX());
compound.setInteger("y"+name+i, relay.getY());
compound.setInteger("z"+name+i, relay.getZ());
}
}
public static ConnectionPair readFromNBT(NBTTagCompound compound, String name){
WorldPos[] pos = new WorldPos[2];
for(int i = 0; i < pos.length; i++){
World aWorld = DimensionManager.getWorld(compound.getInteger("world"+name+i));
int anX = compound.getInteger("x"+name+i);
int aY = compound.getInteger("y"+name+i);
int aZ = compound.getInteger("z"+name+i);
pos[i] = new WorldPos(aWorld, anX, aY, aZ);
}
return new ConnectionPair(pos[0], pos[1]);
}
}
}

View file

@ -16,6 +16,8 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import java.util.ArrayList;
public class WorldData extends WorldSavedData{
public static final String DATA_TAG = ModUtil.MOD_ID+"WorldData";
@ -28,12 +30,25 @@ public class WorldData extends WorldSavedData{
@Override
public void readFromNBT(NBTTagCompound compound){
int netAmount = compound.getInteger("LaserNetworkAmount");
LaserRelayConnectionHandler.getInstance().networks.clear();
for(int i = 0; i < netAmount; i++){
ArrayList<LaserRelayConnectionHandler.ConnectionPair> network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(compound, "LaserNetwork"+i);
if(network != null){
LaserRelayConnectionHandler.getInstance().networks.add(network);
}
}
}
@Override
public void writeToNBT(NBTTagCompound compound){
int netAmount = LaserRelayConnectionHandler.getInstance().networks.size();
compound.setInteger("LaserNetworkAmount", netAmount);
for(int i = 0; i < netAmount; i++){
LaserRelayConnectionHandler.getInstance().writeNetworkToNBT(LaserRelayConnectionHandler.getInstance().networks.get(i), compound, "LaserNetwork"+i);
}
}
public static void makeDirty(){

View file

@ -77,7 +77,9 @@ public abstract class TileEntityBase extends TileEntity{
this.writeSyncableNBT(compound, false);
}
public abstract void writeSyncableNBT(NBTTagCompound compound, boolean isForSync);
public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
}
@Override
public Packet getDescriptionPacket(){
@ -96,7 +98,9 @@ public abstract class TileEntityBase extends TileEntity{
return !(oldBlock.isAssociatedBlock(newBlock));
}
public abstract void readSyncableNBT(NBTTagCompound compound, boolean isForSync);
public void readSyncableNBT(NBTTagCompound compound, boolean isForSync){
}
protected void sendUpdate(){
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);

View file

@ -14,6 +14,9 @@ import cofh.api.energy.IEnergyReceiver;
import ellpeck.actuallyadditions.misc.LaserRelayConnectionHandler;
import ellpeck.actuallyadditions.util.WorldPos;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import java.util.ArrayList;
@ -34,13 +37,33 @@ public class TileEntityLaserRelay extends TileEntityBase implements IEnergyRecei
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
public Packet getDescriptionPacket(){
NBTTagCompound compound = new NBTTagCompound();
WorldPos thisPos = new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord);
ArrayList<LaserRelayConnectionHandler.ConnectionPair> connections = LaserRelayConnectionHandler.getInstance().getConnectionsFor(thisPos);
if(connections != null){
compound.setInteger("ConnectionAmount", connections.size());
for(int i = 0; i < connections.size(); i++){
connections.get(i).writeToNBT(compound, "Connection"+i);
}
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 3, compound);
}
return null;
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean isForSync){
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt){
NBTTagCompound compound = pkt.func_148857_g();
LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord));
int amount = compound.getInteger("ConnectionAmount");
for(int i = 0; i < amount; i++){
LaserRelayConnectionHandler.ConnectionPair pair = LaserRelayConnectionHandler.ConnectionPair.readFromNBT(compound, "Connection"+i);
LaserRelayConnectionHandler.getInstance().addConnection(pair.firstRelay, pair.secondRelay);
}
}
@Override

View file

@ -10,22 +10,10 @@
package ellpeck.actuallyadditions.tile;
import net.minecraft.nbt.NBTTagCompound;
public class TileEntityPhantomBooster extends TileEntityBase{
@Override
public boolean canUpdate(){
return false;
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean isForSync){
}
}

View file

@ -51,7 +51,13 @@ public class WorldPos{
}
public boolean isEqual(WorldPos pos){
return pos != null && this.x == pos.getX() && this.y == pos.getY() && this.z == pos.getZ() && this.world == pos.getWorld();
return pos != null && this.x == pos.getX() && this.y == pos.getY() && this.z == pos.getZ() && (this.world == pos.getWorld() || ((world.isRemote || pos.getWorld().isRemote) && this.world.provider.dimensionId == pos.getWorld().provider.dimensionId));
}
public void update(){
if(this.world != null){
this.world.markBlockForUpdate(this.x, this.y, this.z);
}
}
public int getX(){