Skip to content

Commit

Permalink
Make teleport pipes name-change-resistant.
Browse files Browse the repository at this point in the history
  • Loading branch information
multiplemonomials committed Mar 8, 2015
1 parent b066005 commit f09e40b
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {

apply plugin: 'forge'

version = "4.1.1"
version = "4.2.0"
group= "com.buildcraft.additionalpipes" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "additionalpipes"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public int getIconIndex() {
@Override
public String getUniqueTag()
{
return "additionalpipes: trigger.pipeclosed";
return "additionalpipes:trigger.pipeclosed";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ContainerTeleportPipe(EntityPlayer player, PipeTeleport<?> pipe) {
locations[3 * i + 2] = connectedPipe.container.zCoord;
}

MessageTelePipeData message = new MessageTelePipeData(pipe.container.xCoord, pipe.container.yCoord, pipe.container.zCoord, locations, pipe.owner);
MessageTelePipeData message = new MessageTelePipeData(pipe.container.xCoord, pipe.container.yCoord, pipe.container.zCoord, locations, pipe.ownerUUID, pipe.ownerName);
PacketHandler.INSTANCE.sendTo(message, (EntityPlayerMP) player);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.minecraft.inventory.IInventory;
import buildcraft.BuildCraftCore;
import buildcraft.additionalpipes.network.PacketHandler;
import buildcraft.additionalpipes.network.message.MessageTelePipe;
import buildcraft.additionalpipes.network.message.MessageTelePipeUpdate;
import buildcraft.additionalpipes.pipes.PipeTeleport;
import buildcraft.additionalpipes.textures.Textures;
import buildcraft.core.CoreIconProvider;
Expand Down Expand Up @@ -43,7 +43,7 @@ public void draw(int x, int y) {

fontRendererObj.drawStringWithShadow("Teleport Pipe", x + 22, y + 8, headerColour);
fontRendererObj.drawStringWithShadow("Owner:", x + 22, y + 20, subheaderColour);
fontRendererObj.drawString(pipe.owner, x + 22, y + 32, textColour);
fontRendererObj.drawString(pipe.ownerName, x + 22, y + 32, textColour);
fontRendererObj.drawStringWithShadow("Outputs: ", x + 22, y + 44, subheaderColour);
fontRendererObj.drawString(String.valueOf(container.connectedPipes), x + 66, y + 45, textColour);
int[] net = pipe.network;
Expand All @@ -60,7 +60,7 @@ public void draw(int x, int y) {

@Override
public String getTooltip() {
return "Owner: " + pipe.owner;
return "Owner: " + pipe.ownerName;
}
}

Expand Down Expand Up @@ -157,7 +157,7 @@ protected void actionPerformed(GuiButton guibutton) {
freq = 0;
}

MessageTelePipe packet = new MessageTelePipe(pipe.container.xCoord, pipe.container.yCoord, pipe.container.zCoord, freq, isPublic, state);
MessageTelePipeUpdate packet = new MessageTelePipeUpdate(pipe.container.xCoord, pipe.container.yCoord, pipe.container.zCoord, freq, isPublic, state);
PacketHandler.INSTANCE.sendToServer(packet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import buildcraft.additionalpipes.network.message.MessageChunkloadData;
import buildcraft.additionalpipes.network.message.MessageChunkloadRequest;
import buildcraft.additionalpipes.network.message.MessageDistPipe;
import buildcraft.additionalpipes.network.message.MessageTelePipe;
import buildcraft.additionalpipes.network.message.MessageTelePipeUpdate;
import buildcraft.additionalpipes.network.message.MessageTelePipeData;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
Expand All @@ -18,7 +18,7 @@ public class PacketHandler
public static void init()
{
INSTANCE.registerMessage(MessageDistPipe.class, MessageDistPipe.class, 0, Side.SERVER);
INSTANCE.registerMessage(MessageTelePipe.class, MessageTelePipe.class, 1, Side.SERVER);
INSTANCE.registerMessage(MessageTelePipeUpdate.class, MessageTelePipeUpdate.class, 1, Side.SERVER);
INSTANCE.registerMessage(MessageAdvWoodPipe.class, MessageAdvWoodPipe.class, 2, Side.SERVER);
INSTANCE.registerMessage(MessageTelePipeData.class, MessageTelePipeData.class, 3, Side.CLIENT);
INSTANCE.registerMessage(MessageChunkloadRequest.class, MessageChunkloadRequest.class, 4, Side.SERVER);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package buildcraft.additionalpipes.network.message;

import io.netty.buffer.ByteBuf;

import java.util.UUID;

import net.minecraft.tileentity.TileEntity;
import buildcraft.additionalpipes.pipes.PipeTeleport;
import buildcraft.transport.TileGenericPipe;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
Expand All @@ -13,19 +17,21 @@ public class MessageTelePipeData implements IMessage, IMessageHandler<MessageTel
{
public int x, y, z;
public int[] locations;
public String owner;
public String ownerUUID;
public String ownerName;

public MessageTelePipeData()
{
}

public MessageTelePipeData(int x, int y, int z, int[] locations, String owner)
public MessageTelePipeData(int x, int y, int z, int[] locations, UUID ownerUUID, String ownerName)
{
this.x = x;
this.y = y;
this.z = z;
this.locations = locations;
this.owner = owner;
this.ownerUUID = ownerUUID.toString();
this.ownerName = ownerName;
}

@Override
Expand All @@ -43,8 +49,8 @@ public void fromBytes(ByteBuf buf)
locations[counter] = buf.readInt();
}

int ownerLength = buf.readInt();
this.owner = new String(buf.readBytes(ownerLength).array());
ownerUUID = ByteBufUtils.readUTF8String(buf);
ownerName = ByteBufUtils.readUTF8String(buf);
}

@Override
Expand All @@ -58,8 +64,8 @@ public void toBytes(ByteBuf buf)
{
buf.writeInt(location);
}
buf.writeInt(owner.length());
buf.writeBytes(owner.getBytes());
ByteBufUtils.writeUTF8String(buf, ownerUUID);
ByteBufUtils.writeUTF8String(buf, ownerName);
}

@Override
Expand All @@ -68,7 +74,8 @@ public IMessage onMessage(MessageTelePipeData message, MessageContext ctx)
TileEntity te = FMLClientHandler.instance().getClient().theWorld.getTileEntity(message.x, message.y, message.z);

PipeTeleport<?> pipe = (PipeTeleport<?>) ((TileGenericPipe) te).pipe;
pipe.owner = message.owner;
pipe.ownerUUID = UUID.fromString(message.ownerUUID);
pipe.ownerName = message.ownerName;
pipe.network = message.locations;

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
* Message that sets the properties of a Teleport Pipe from the GUI
*
*/
public class MessageTelePipe implements IMessage, IMessageHandler<MessageTelePipe, IMessage>
public class MessageTelePipeUpdate implements IMessage, IMessageHandler<MessageTelePipeUpdate, IMessage>
{
public int x, y, z;
int _freq;
boolean _isPublic;
byte _state;
int _newData;

public MessageTelePipe()
public MessageTelePipeUpdate()
{
}

public MessageTelePipe(int x, int y,int z, int freq, boolean isPublic, byte index)
public MessageTelePipeUpdate(int x, int y,int z, int freq, boolean isPublic, byte index)
{
this.x = x;
this.y = y;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void toBytes(ByteBuf buf)
}

@Override
public IMessage onMessage(MessageTelePipe message, MessageContext ctx)
public IMessage onMessage(MessageTelePipeUpdate message, MessageContext ctx)
{
TileEntity te = ctx.getServerHandler().playerEntity.worldObj.getTileEntity(message.x, message.y, message.z);
if(te instanceof TileGenericPipe) {
Expand Down
36 changes: 30 additions & 6 deletions src/main/java/buildcraft/additionalpipes/pipes/PipeTeleport.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package buildcraft.additionalpipes.pipes;

import java.util.Random;
import java.util.UUID;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
Expand All @@ -10,6 +11,7 @@
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.additionalpipes.AdditionalPipes;
import buildcraft.additionalpipes.gui.GuiHandler;
import buildcraft.additionalpipes.utils.PlayerUtils;
import buildcraft.api.core.Position;
import buildcraft.transport.Pipe;
import buildcraft.transport.PipeTransport;
Expand All @@ -22,7 +24,9 @@ public abstract class PipeTeleport<pipeType extends PipeTransport> extends APPip
// 00 = none, 01 = send, 10 = receive, 11 = both
public byte state = 1;

public String owner = "";
public UUID ownerUUID;
public String ownerName;

public int[] network = new int[0];
public boolean isPublic = false;

Expand Down Expand Up @@ -52,8 +56,20 @@ public void onChunkUnload() {
public boolean blockActivated(EntityPlayer player) {
if(!AdditionalPipes.proxy.isServer(player.worldObj))
return true;
if(owner == null || "".equalsIgnoreCase(owner)) {
owner = player.getCommandSenderName();
if(ownerUUID == null)
{
// getUUIDFromProfile()
ownerUUID = PlayerUtils.getUUID(player);
ownerName = player.getCommandSenderName();
}

//test for player name change
if(PlayerUtils.getUUID(player).equals(ownerUUID))
{
if(!player.getCommandSenderName().equals(ownerName))
{
ownerName = player.getCommandSenderName();
}
}

if(AdditionalPipes.instance.filterRightclicks)
Expand Down Expand Up @@ -104,7 +120,11 @@ public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
nbttagcompound.setInteger("freq", frequency);
nbttagcompound.setByte("state", state);
nbttagcompound.setString("owner", owner);
if(ownerUUID != null)
{
nbttagcompound.setString("ownerUUID", ownerUUID.toString());
nbttagcompound.setString("ownerName", ownerName);
}
nbttagcompound.setBoolean("isPublic", isPublic);
}

Expand All @@ -113,12 +133,16 @@ public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
frequency = nbttagcompound.getInteger("freq");
state = nbttagcompound.getByte("state");
owner = nbttagcompound.getString("owner");
if(nbttagcompound.hasKey("ownerUUID"))
{
ownerUUID = UUID.fromString(nbttagcompound.getString("ownerUUID"));
ownerName = nbttagcompound.getString("ownerName");
}
isPublic = nbttagcompound.getBoolean("isPublic");
}

public static boolean canPlayerModifyPipe(EntityPlayer player, PipeTeleport<?> pipe) {
if(pipe.isPublic || pipe.owner.equals(player.getCommandSenderName()) || player.capabilities.isCreativeMode)
if(pipe.isPublic || pipe.ownerUUID.equals(PlayerUtils.getUUID(player)) || player.capabilities.isCreativeMode)
return true;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public List<PipeTeleport<?>> getConnectedPipes(PipeTeleport<?> pipe, boolean for
// pipe is open or forceReceive &&
// both public or same owner
if((pipe.container.xCoord != other.container.xCoord || pipe.container.yCoord != other.container.yCoord || pipe.container.zCoord != other.container.zCoord) && other.getFrequency() == pipe.getFrequency()
&& ((other.state & 0x2) > 0 || forceReceive) && (pipe.isPublic ? other.isPublic : other.owner.equalsIgnoreCase(pipe.owner))) {
&& ((other.state & 0x2) > 0 || forceReceive) && (pipe.isPublic ? other.isPublic : other.ownerUUID.equals(pipe.ownerUUID)))
{
connected.add(other);
}
}
Expand All @@ -75,7 +76,7 @@ public List<PipeTeleport<?>> getAllPipesInNetwork(PipeTeleport<?> pipe) {
continue;
}

if(other.getFrequency() == pipe.getFrequency() && (pipe.isPublic ? other.isPublic : other.owner.equalsIgnoreCase(pipe.owner))) {
if(other.getFrequency() == pipe.getFrequency() && (pipe.isPublic ? other.isPublic : other.ownerUUID.equals(pipe.ownerUUID))) {
pipes.add(other);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/buildcraft/additionalpipes/utils/PlayerUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package buildcraft.additionalpipes.utils;

import java.util.UUID;

import net.minecraft.entity.player.EntityPlayer;

public class PlayerUtils
{
/**
* Get a player's UUID.
* Calling this is easier than the more complicated default syntax.
* @param player
*/
public static UUID getUUID(EntityPlayer player)
{
return EntityPlayer.func_146094_a(player.getGameProfile());
}
}

0 comments on commit f09e40b

Please sign in to comment.