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; 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.client.model.ModelRenderer;
import net.minecraft.tileentity.TileEntity;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import java.util.ArrayList;
public class ModelLaserRelay extends ModelBaseAA{ public class ModelLaserRelay extends ModelBaseAA{
ModelRenderer bottom; ModelRenderer bottom;
@ -164,6 +170,20 @@ public class ModelLaserRelay extends ModelBaseAA{
covering12.render(f); 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 @Override
public String getName(){ public String getName(){
return "modelLaserRelay"; return "modelLaserRelay";

View file

@ -31,6 +31,7 @@ public class InitEvents{
Util.registerEvent(new BucketFillEvent()); Util.registerEvent(new BucketFillEvent());
Util.registerEvent(new LogoutEvent()); Util.registerEvent(new LogoutEvent());
Util.registerEvent(new EntityConstructingEvent()); Util.registerEvent(new EntityConstructingEvent());
Util.registerEvent(new WorldLoadEvent());
MinecraftForge.TERRAIN_GEN_BUS.register(new WorldDecorationEvent()); 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); 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 @Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10){ 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); TileEntity tile = world.getTileEntity(x, y, z);
if(tile instanceof TileEntityLaserRelay){ if(tile instanceof TileEntityLaserRelay && !player.isSneaking()){
if(!world.isRemote){ if(!world.isRemote){
if(ItemPhantomConnector.getStoredPosition(stack) == null){ if(ItemPhantomConnector.getStoredPosition(stack) == null){
ItemPhantomConnector.storeConnection(stack, x, y, z, world); ItemPhantomConnector.storeConnection(stack, x, y, z, world);
@ -54,6 +63,9 @@ public class ItemLaserWrench extends Item implements IActAddItemOrBlock{
if(LaserRelayConnectionHandler.getInstance().addConnection(savedPos, otherPos)){ if(LaserRelayConnectionHandler.getInstance().addConnection(savedPos, otherPos)){
player.addChatComponentMessage(new ChatComponentText("Connected!")); player.addChatComponentMessage(new ChatComponentText("Connected!"));
ItemPhantomConnector.clearStorage(stack); ItemPhantomConnector.clearStorage(stack);
savedPos.update();
otherPos.update();
} }
else{ else{
player.addChatComponentMessage(new ChatComponentText("Couldn't connect!")); 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.tile.TileEntityLaserRelay;
import ellpeck.actuallyadditions.util.WorldPos; import ellpeck.actuallyadditions.util.WorldPos;
import ellpeck.actuallyadditions.util.WorldUtil; import ellpeck.actuallyadditions.util.WorldUtil;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,12 +34,37 @@ public class LaserRelayConnectionHandler{
public ArrayList<ArrayList<ConnectionPair>> networks = new ArrayList<ArrayList<ConnectionPair>>(); public ArrayList<ArrayList<ConnectionPair>> networks = new ArrayList<ArrayList<ConnectionPair>>();
public static LaserRelayConnectionHandler getInstance(){ public static LaserRelayConnectionHandler getInstance(){
if(instance == null){
instance = new LaserRelayConnectionHandler();
}
return instance; 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 * Gets a Network for a Relay
*/ */
@ -51,6 +79,21 @@ public class LaserRelayConnectionHandler{
return null; 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 * Adds a new connection between two relays
* (Puts it into the correct network!) * (Puts it into the correct network!)
@ -170,5 +213,27 @@ public class LaserRelayConnectionHandler{
public String toString(){ public String toString(){
return (this.firstRelay == null ? "-" : this.firstRelay.toString())+" | "+(this.secondRelay == null ? "-" : this.secondRelay.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.World;
import net.minecraft.world.WorldSavedData; import net.minecraft.world.WorldSavedData;
import java.util.ArrayList;
public class WorldData extends WorldSavedData{ public class WorldData extends WorldSavedData{
public static final String DATA_TAG = ModUtil.MOD_ID+"WorldData"; public static final String DATA_TAG = ModUtil.MOD_ID+"WorldData";
@ -28,12 +30,25 @@ public class WorldData extends WorldSavedData{
@Override @Override
public void readFromNBT(NBTTagCompound compound){ 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 @Override
public void writeToNBT(NBTTagCompound compound){ 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(){ public static void makeDirty(){

View file

@ -77,7 +77,9 @@ public abstract class TileEntityBase extends TileEntity{
this.writeSyncableNBT(compound, false); this.writeSyncableNBT(compound, false);
} }
public abstract void writeSyncableNBT(NBTTagCompound compound, boolean isForSync); public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
}
@Override @Override
public Packet getDescriptionPacket(){ public Packet getDescriptionPacket(){
@ -96,7 +98,9 @@ public abstract class TileEntityBase extends TileEntity{
return !(oldBlock.isAssociatedBlock(newBlock)); return !(oldBlock.isAssociatedBlock(newBlock));
} }
public abstract void readSyncableNBT(NBTTagCompound compound, boolean isForSync); public void readSyncableNBT(NBTTagCompound compound, boolean isForSync){
}
protected void sendUpdate(){ protected void sendUpdate(){
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord); 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.misc.LaserRelayConnectionHandler;
import ellpeck.actuallyadditions.util.WorldPos; import ellpeck.actuallyadditions.util.WorldPos;
import net.minecraft.nbt.NBTTagCompound; 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 net.minecraftforge.common.util.ForgeDirection;
import java.util.ArrayList; import java.util.ArrayList;
@ -34,13 +37,33 @@ public class TileEntityLaserRelay extends TileEntityBase implements IEnergyRecei
} }
@Override @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 @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 @Override

View file

@ -10,22 +10,10 @@
package ellpeck.actuallyadditions.tile; package ellpeck.actuallyadditions.tile;
import net.minecraft.nbt.NBTTagCompound;
public class TileEntityPhantomBooster extends TileEntityBase{ public class TileEntityPhantomBooster extends TileEntityBase{
@Override @Override
public boolean canUpdate(){ public boolean canUpdate(){
return false; 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){ 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(){ public int getX(){