mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-23 21:49:58 +00:00
Wrap UUID
This commit is contained in:
1 parent
a7c88fff59
commit
c2e15c5a87
11 files changed
+360
-64
No files matched your search
@@ -2,10 +2,15 @@ package crazypants.enderio.api.teleport;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import com.enderio.core.common.util.BlockCoord;
|
||||
|
||||
import crazypants.util.UserIdent;
|
||||
|
||||
public interface ITravelAccessable {
|
||||
|
||||
public enum AccessMode {
|
||||
@@ -40,8 +45,12 @@ public interface ITravelAccessable {
|
||||
|
||||
void setLabel(String label);
|
||||
|
||||
@Deprecated
|
||||
UUID getPlacedBy();
|
||||
|
||||
@Nonnull
|
||||
UserIdent getOwner();
|
||||
|
||||
void setPlacedBy(EntityPlayer player);
|
||||
|
||||
public void clearAuthorisedUsers();
|
||||
|
||||
@@ -76,7 +76,7 @@ public class BlockEnderIO extends BlockEio implements IResourceTooltipProvider {
|
||||
if(ta.canUiBeAccessed(entityPlayer)) {
|
||||
entityPlayer.openGui(EnderIO.instance, GuiHandler.GUI_ID_TRAVEL_ACCESSABLE, world, x, y, z);
|
||||
} else {
|
||||
BlockTravelAnchor.sendPrivateChatMessage(entityPlayer, ta.getPlacedBy());
|
||||
BlockTravelAnchor.sendPrivateChatMessage(entityPlayer, ta.getOwner());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ import java.util.UUID;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import com.enderio.core.common.util.PlayerUtil;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
import crazypants.util.UserIdent;
|
||||
|
||||
|
||||
public class Channel {
|
||||
@@ -14,26 +17,41 @@ public class Channel {
|
||||
return null;
|
||||
}
|
||||
String name = root.getString("name");
|
||||
UUID user = null;
|
||||
UserIdent user;
|
||||
if(root.hasKey("user")) {
|
||||
user = PlayerUtil.getPlayerUIDUnstable(root.getString("user"));
|
||||
String legacyUser = root.getString("user");
|
||||
user = UserIdent.create(legacyUser);
|
||||
} else {
|
||||
user = UserIdent.readfromNbt(root, "user");
|
||||
}
|
||||
ChannelType type = ChannelType.values()[root.getShort("type")];
|
||||
return new Channel(name, user, type);
|
||||
}
|
||||
|
||||
private final String name;
|
||||
final UUID user;
|
||||
private final UserIdent user;
|
||||
final ChannelType type;
|
||||
|
||||
public Channel(String name, UUID user, ChannelType type) {
|
||||
public Channel(String name, GameProfile profile, ChannelType type) {
|
||||
this.name = trim(name);
|
||||
this.user = UserIdent.create(profile);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Channel(String name, UserIdent user, ChannelType type) {
|
||||
this.name = trim(name);
|
||||
this.user = user;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Channel(String name, ChannelType type) {
|
||||
this.name = trim(name);
|
||||
this.user = UserIdent.nobody;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public boolean isPublic() {
|
||||
return user == null;
|
||||
return user == UserIdent.nobody;
|
||||
}
|
||||
|
||||
public void writeToNBT(NBTTagCompound root) {
|
||||
@@ -41,10 +59,8 @@ public class Channel {
|
||||
return;
|
||||
}
|
||||
root.setString("name", name);
|
||||
user.saveToNbt(root, "user");
|
||||
root.setShort("type", (short)type.ordinal());
|
||||
if(user != null ) {
|
||||
root.setString("user", user.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private String trim(String str) {
|
||||
@@ -100,7 +116,7 @@ public class Channel {
|
||||
return type;
|
||||
}
|
||||
|
||||
public UUID getUser() {
|
||||
public UserIdent getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package crazypants.enderio.machine.transceiver;
|
||||
|
||||
import com.enderio.core.common.util.PlayerUtil;
|
||||
|
||||
import crazypants.enderio.EnderIO;
|
||||
import crazypants.util.UserIdent;
|
||||
|
||||
|
||||
public class ClientChannelRegister extends ChannelRegister {
|
||||
@@ -18,7 +17,7 @@ public class ClientChannelRegister extends ChannelRegister {
|
||||
if (channel == null) {
|
||||
return;
|
||||
}
|
||||
if (channel.getUser() != null && !channel.getUser().equals(PlayerUtil.getPlayerUUID(EnderIO.proxy.getClientPlayer().getGameProfile().getName()))) {
|
||||
if (!channel.isPublic() && !channel.getUser().equals(EnderIO.proxy.getClientPlayer().getGameProfile())) {
|
||||
return;
|
||||
}
|
||||
super.addChannel(channel);
|
||||
|
||||
@@ -35,6 +35,7 @@ import com.google.gson.stream.JsonWriter;
|
||||
import crazypants.enderio.Log;
|
||||
import crazypants.enderio.config.Config;
|
||||
import crazypants.enderio.machine.SlotDefinition;
|
||||
import crazypants.util.UserIdent;
|
||||
|
||||
public class ServerChannelRegister extends ChannelRegister {
|
||||
|
||||
@@ -58,21 +59,41 @@ public class ServerChannelRegister extends ChannelRegister {
|
||||
JsonReader reader = new JsonReader(new FileReader(getDataFile()));
|
||||
reader.beginArray();
|
||||
while (reader.hasNext()) {
|
||||
String name = null;
|
||||
int ordinal = -1;
|
||||
UserIdent ident = UserIdent.nobody;
|
||||
String uuid = null;
|
||||
String playername = null;
|
||||
String legacyUser = null;
|
||||
reader.beginObject();
|
||||
reader.nextName();
|
||||
String name = reader.nextString();
|
||||
String key = reader.nextName();
|
||||
String user = null;
|
||||
if("user".equals(key)) {
|
||||
user = reader.nextString();
|
||||
reader.nextName();
|
||||
while (reader.hasNext()) {
|
||||
String key = reader.nextName();
|
||||
if ("name".equals(key)) {
|
||||
name = reader.nextString();
|
||||
} else if ("user".equals(key)) {
|
||||
legacyUser = reader.nextString();
|
||||
} else if ("uuid".equals(key)) {
|
||||
uuid = reader.nextString();
|
||||
} else if ("playername".equals(key)) {
|
||||
playername = reader.nextString();
|
||||
} else if ("type".equals(key)) {
|
||||
ordinal = reader.nextInt();
|
||||
} else {
|
||||
Log.warn("ServerChannelRegister: Unknown key '" + key + "' in dimensionalTransceiver.json");
|
||||
}
|
||||
}
|
||||
int oridinal = reader.nextInt();
|
||||
|
||||
Channel chan = new Channel(name, PlayerUtil.getPlayerUIDUnstable(user), ChannelType.values()[oridinal]);
|
||||
instance.addChannel(chan);
|
||||
|
||||
reader.endObject();
|
||||
if (name == null || ordinal == -1) {
|
||||
Log.warn("ServerChannelRegister: Incomplete channel in dimensionalTransceiver.json");
|
||||
} else {
|
||||
if (uuid != null || playername != null) {
|
||||
ident = UserIdent.create(uuid, playername);
|
||||
} else if (legacyUser != null) {
|
||||
ident = UserIdent.create(legacyUser);
|
||||
}
|
||||
Channel chan = new Channel(name, ident, ChannelType.values()[ordinal]);
|
||||
instance.addChannel(chan);
|
||||
}
|
||||
}
|
||||
reader.endArray();
|
||||
reader.close();
|
||||
@@ -145,8 +166,9 @@ public class ServerChannelRegister extends ChannelRegister {
|
||||
for (Channel chan : channels.values()) {
|
||||
writer.beginObject();
|
||||
writer.name("name").value(chan.getName());
|
||||
if (chan.getUser() != null) {
|
||||
writer.name("user").value(chan.getUser().toString());
|
||||
if (chan.getUser() != null && chan.getUser() != UserIdent.nobody) {
|
||||
writer.name("uuid").value(chan.getUser().getUUIDString());
|
||||
writer.name("playername").value(chan.getUser().getPlayerName());
|
||||
}
|
||||
writer.name("type").value(chan.getType().ordinal());
|
||||
writer.endObject();
|
||||
|
||||
@@ -2,6 +2,8 @@ package crazypants.enderio.machine.transceiver.gui;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.gui.GuiTextField;
|
||||
@@ -12,7 +14,6 @@ import com.enderio.core.client.gui.button.IconButton;
|
||||
import com.enderio.core.client.gui.button.ToggleButton;
|
||||
import com.enderio.core.client.gui.widget.GuiScrollableList;
|
||||
import com.enderio.core.client.render.ColorUtil;
|
||||
import com.enderio.core.common.util.PlayerUtil;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
|
||||
@@ -25,6 +26,7 @@ import crazypants.enderio.machine.transceiver.PacketAddRemoveChannel;
|
||||
import crazypants.enderio.machine.transceiver.PacketSendRecieveChannel;
|
||||
import crazypants.enderio.machine.transceiver.TileTransceiver;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.util.UserIdent;
|
||||
|
||||
public class ChannelTab implements ITabPanel {
|
||||
|
||||
@@ -82,8 +84,9 @@ public class ChannelTab implements ITabPanel {
|
||||
|
||||
Predicate<Channel> predicate = new Predicate<Channel>() {
|
||||
@Override
|
||||
public boolean apply(Channel input) {
|
||||
return input.isPublic() || input.getUser().equals(PlayerUtil.getPlayerUUID(EnderIO.proxy.getClientPlayer().getGameProfile().getName()));
|
||||
public boolean apply(@Nullable Channel input) {
|
||||
return input != null
|
||||
&& (input.isPublic() || input.getUser().equals(EnderIO.proxy.getClientPlayer().getGameProfile()) || input.getUser() == UserIdent.nobody);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -261,9 +264,9 @@ public class ChannelTab implements ITabPanel {
|
||||
}
|
||||
Channel c;
|
||||
if(privateButton.isSelected()) {
|
||||
c = new Channel(newChannelTF.getText(), Minecraft.getMinecraft().thePlayer.getGameProfile().getId(), type);
|
||||
c = new Channel(newChannelTF.getText(), Minecraft.getMinecraft().thePlayer.getGameProfile(), type);
|
||||
} else {
|
||||
c = new Channel(newChannelTF.getText(), null, type);
|
||||
c = new Channel(newChannelTF.getText(), type);
|
||||
}
|
||||
ClientChannelRegister.instance.addChannel(c);
|
||||
PacketHandler.INSTANCE.sendToServer(new PacketAddRemoveChannel(c, true));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package crazypants.enderio.teleport.anchor;
|
||||
|
||||
import java.util.UUID;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.ITileEntityProvider;
|
||||
@@ -48,6 +48,7 @@ import crazypants.enderio.teleport.packet.PacketOpenAuthGui;
|
||||
import crazypants.enderio.teleport.packet.PacketPassword;
|
||||
import crazypants.enderio.teleport.packet.PacketTravelEvent;
|
||||
import crazypants.util.IFacade;
|
||||
import crazypants.util.UserIdent;
|
||||
|
||||
public class BlockTravelAnchor extends BlockEio implements IGuiHandler, ITileEntityProvider, IResourceTooltipProvider, IFacade {
|
||||
|
||||
@@ -135,17 +136,16 @@ public class BlockTravelAnchor extends BlockEio implements IGuiHandler, ITileEnt
|
||||
if (ta.canUiBeAccessed(entityPlayer)) {
|
||||
entityPlayer.openGui(EnderIO.instance, GuiHandler.GUI_ID_TRAVEL_ACCESSABLE, world, x, y, z);
|
||||
} else {
|
||||
sendPrivateChatMessage(entityPlayer, ta.getPlacedBy());
|
||||
sendPrivateChatMessage(entityPlayer, ta.getOwner());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void sendPrivateChatMessage(EntityPlayer player, UUID owner) {
|
||||
public static void sendPrivateChatMessage(EntityPlayer player, UserIdent owner) {
|
||||
if (!player.isSneaking()) {
|
||||
ChatUtil.sendNoSpam(player,
|
||||
EnderIO.lang.localize("gui.travelAccessable.privateBlock1") + " " + EnumChatFormatting.RED + UsernameCache.getLastKnownUsername(owner)
|
||||
+ EnumChatFormatting.WHITE + " " + EnderIO.lang.localize("gui.travelAccessable.privateBlock2"));
|
||||
ChatUtil.sendNoSpam(player, EnderIO.lang.localize("gui.travelAccessable.privateBlock1") + " " + EnumChatFormatting.RED
|
||||
+ owner.getPlayerName() + EnumChatFormatting.WHITE + " " + EnderIO.lang.localize("gui.travelAccessable.privateBlock2"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ public class BlockTravelAnchor extends BlockEio implements IGuiHandler, ITileEnt
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processDrop(World world, int x, int y, int z, TileEntityEnder te, ItemStack drop) {
|
||||
protected void processDrop(World world, int x, int y, int z, @Nullable TileEntityEnder te, ItemStack drop) {
|
||||
TileTravelAnchor anchor = (TileTravelAnchor) te;
|
||||
|
||||
if (anchor == null) {
|
||||
|
||||
@@ -4,6 +4,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -20,6 +22,7 @@ import crazypants.enderio.TileEntityEio;
|
||||
import crazypants.enderio.api.teleport.ITravelAccessable;
|
||||
import crazypants.enderio.api.teleport.TravelSource;
|
||||
import crazypants.enderio.machine.painter.IPaintableTileEntity;
|
||||
import crazypants.util.UserIdent;
|
||||
|
||||
public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable, IPaintableTileEntity {
|
||||
|
||||
@@ -37,9 +40,9 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
|
||||
private String label;
|
||||
|
||||
private UUID placedBy;
|
||||
private @Nonnull UserIdent owner = UserIdent.nobody;
|
||||
|
||||
private List<UUID> authorisedUsers = new ArrayList<UUID>();
|
||||
private List<UserIdent> authorisedUsers = new ArrayList<UserIdent>();
|
||||
|
||||
@Override
|
||||
public boolean canBlockBeAccessed(EntityPlayer playerName) {
|
||||
@@ -47,8 +50,7 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
return true;
|
||||
}
|
||||
// Covers protected and private access modes
|
||||
return (placedBy != null && placedBy.equals(PlayerUtil.getPlayerUUID(playerName.getGameProfile().getName())))
|
||||
|| authorisedUsers.contains(PlayerUtil.getPlayerUUID(playerName.getGameProfile().getName()));
|
||||
return owner.equals(playerName.getGameProfile()) || authorisedUsers.contains(playerName.getGameProfile());
|
||||
|
||||
}
|
||||
|
||||
@@ -85,7 +87,7 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
@Override
|
||||
public boolean authoriseUser(EntityPlayer username, ItemStack[] password) {
|
||||
if(checkPassword(password)) {
|
||||
authorisedUsers.add(PlayerUtil.getPlayerUUID(username.getGameProfile().getName()));
|
||||
authorisedUsers.add(UserIdent.create(username.getGameProfile()));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -93,7 +95,7 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
|
||||
@Override
|
||||
public boolean canUiBeAccessed(EntityPlayer playerName) {
|
||||
return placedBy != null && placedBy.equals(PlayerUtil.getPlayerUUID(playerName.getGameProfile().getName()));
|
||||
return owner.equals(playerName.getGameProfile());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -101,7 +103,7 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
if(accessMode != AccessMode.PRIVATE) {
|
||||
return true;
|
||||
}
|
||||
return placedBy != null && placedBy.equals(PlayerUtil.getPlayerUUID(playerName.getGameProfile().getName()));
|
||||
return owner.equals(playerName.getGameProfile());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -124,10 +126,12 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItemLabel() {
|
||||
return itemLabel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setItemLabel(ItemStack lableIcon) {
|
||||
this.itemLabel = lableIcon;
|
||||
}
|
||||
@@ -143,16 +147,22 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public UUID getPlacedBy() {
|
||||
return placedBy;
|
||||
return owner.getUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nonnull UserIdent getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlacedBy(EntityPlayer player) {
|
||||
if(player == null || player.getGameProfile() == null) {
|
||||
this.placedBy = null;
|
||||
if (player != null) {
|
||||
this.owner = UserIdent.create(player.getGameProfile());
|
||||
} else {
|
||||
placedBy = PlayerUtil.getPlayerUUID(player.getGameProfile().getName());
|
||||
this.owner = UserIdent.nobody;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +210,11 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
//keep behavior the same for blocks placed prior to this update
|
||||
accessMode = AccessMode.PUBLIC;
|
||||
}
|
||||
placedBy = PlayerUtil.getPlayerUIDUnstable(root.getString("placedBy"));
|
||||
if (root.hasKey("placedBy")) {
|
||||
owner = UserIdent.create(root.getString("placedBy"));
|
||||
} else {
|
||||
owner = UserIdent.readfromNbt(root, "owner");
|
||||
}
|
||||
for (int i = 0; i < password.length; i++) {
|
||||
if(root.hasKey("password" + i)) {
|
||||
NBTTagCompound stackRoot = (NBTTagCompound) root.getTag("password" + i);
|
||||
@@ -210,6 +224,7 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
}
|
||||
}
|
||||
authorisedUsers.clear();
|
||||
if (root.hasKey("authorisedUsers")) {
|
||||
String userStr = root.getString("authorisedUsers");
|
||||
if(userStr != null && userStr.length() > 0) {
|
||||
String[] users = userStr.split(",");
|
||||
@@ -217,11 +232,18 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
if(user != null) {
|
||||
user = user.trim();
|
||||
if(user.length() > 0) {
|
||||
authorisedUsers.add(PlayerUtil.getPlayerUIDUnstable(user));
|
||||
authorisedUsers.add(UserIdent.create(user));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int userIdx = 0;
|
||||
while (UserIdent.existsInNbt(root, "authorisedUser" + userIdx)) {
|
||||
UserIdent.readfromNbt(root, "authorisedUser" + userIdx);
|
||||
userIdx++;
|
||||
}
|
||||
}
|
||||
if(root.hasKey("itemLabel")) {
|
||||
NBTTagCompound stackRoot = (NBTTagCompound) root.getTag("itemLabel");
|
||||
itemLabel = ItemStack.loadItemStackFromNBT(stackRoot);
|
||||
@@ -242,9 +264,7 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
@Override
|
||||
protected void writeCustomNBT(NBTTagCompound root) {
|
||||
root.setShort("accessMode", (short) accessMode.ordinal());
|
||||
if(placedBy != null ) {
|
||||
root.setString("placedBy", placedBy.toString());
|
||||
}
|
||||
owner.saveToNbt(root, "owner");
|
||||
for (int i = 0; i < password.length; i++) {
|
||||
ItemStack stack = password[i];
|
||||
if(stack != null) {
|
||||
@@ -253,16 +273,13 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
|
||||
root.setTag("password" + i, stackRoot);
|
||||
}
|
||||
}
|
||||
StringBuffer userStr = new StringBuffer();
|
||||
for (UUID user : authorisedUsers) {
|
||||
int userIdx = 0;
|
||||
for (UserIdent user : authorisedUsers) {
|
||||
if(user != null) {
|
||||
userStr.append(user.toString());
|
||||
userStr.append(",");
|
||||
user.saveToNbt(root, "authorisedUser" + userIdx);
|
||||
userIdx++;
|
||||
}
|
||||
}
|
||||
if(authorisedUsers.size() > 0) {
|
||||
root.setString("authorisedUsers", userStr.toString());
|
||||
}
|
||||
if(itemLabel != null) {
|
||||
NBTTagCompound labelRoot = new NBTTagCompound();
|
||||
itemLabel.writeToNBT(labelRoot);
|
||||
|
||||
@@ -132,7 +132,7 @@ public class BlockTelePad extends BlockTravelAnchor {
|
||||
if(tp.canBlockBeAccessed(entityPlayer)) {
|
||||
entityPlayer.openGui(EnderIO.instance, GuiHandler.GUI_ID_TELEPAD, world, x, y, z);
|
||||
} else {
|
||||
sendPrivateChatMessage(entityPlayer, tp.getPlacedBy());
|
||||
sendPrivateChatMessage(entityPlayer, tp.getOwner());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -123,7 +123,7 @@ public class ItemCoordSelector extends Item implements IResourceTooltipProvider
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
BlockTravelAnchor.sendPrivateChatMessage(player, tp.getPlacedBy());
|
||||
BlockTravelAnchor.sendPrivateChatMessage(player, tp.getOwner());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
package crazypants.util;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.UsernameCache;
|
||||
|
||||
import com.enderio.core.common.util.PlayerUtil;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
import crazypants.enderio.Log;
|
||||
|
||||
public class UserIdent {
|
||||
private static final String NONE_MARKER = "none";
|
||||
private final @Nullable UUID uuid;
|
||||
private final @Nonnull UUID uuid_offline;
|
||||
private final @Nonnull String playerName;
|
||||
|
||||
public @Nonnull String getPlayerName() {
|
||||
if (uuid != null) {
|
||||
String lastKnownName = UsernameCache.getLastKnownUsername(uuid);
|
||||
if (lastKnownName != null) {
|
||||
return lastKnownName;
|
||||
}
|
||||
}
|
||||
return playerName;
|
||||
}
|
||||
|
||||
public UUID getUUID() {
|
||||
return uuid != null ? uuid : uuid_offline;
|
||||
}
|
||||
|
||||
@SuppressWarnings("null")
|
||||
// it's final(!), eclipse...
|
||||
public String getUUIDString() {
|
||||
return uuid != null ? uuid.toString() : NONE_MARKER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a UserIdent from a UUID object and a name. Use this when reading
|
||||
* stored data, it will check for username changes, implement them and write a
|
||||
* log message.
|
||||
*/
|
||||
public static @Nonnull UserIdent create(@Nullable UUID uuid, @Nullable String playerName) {
|
||||
if (uuid != null) {
|
||||
if (nobody.equals(uuid)) {
|
||||
return nobody;
|
||||
}
|
||||
if (playerName != null) {
|
||||
String lastKnownName = UsernameCache.getLastKnownUsername(uuid);
|
||||
if (lastKnownName != null && !lastKnownName.equals(playerName)) {
|
||||
Log.warn("The user with the UUID " + uuid + " changed name from '" + playerName + "' to '" + lastKnownName + "'");
|
||||
return new UserIdent(uuid, lastKnownName);
|
||||
}
|
||||
}
|
||||
return new UserIdent(uuid, playerName);
|
||||
} else if (playerName != null) {
|
||||
return new UserIdent(null, playerName);
|
||||
} else {
|
||||
return nobody;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a UserIdent from a UUID string and a name. Use this when reading
|
||||
* stored data, it will check for username changes, implement them and write a
|
||||
* log message.
|
||||
*/
|
||||
public static @Nonnull UserIdent create(@Nonnull String suuid, @Nullable String playerName) {
|
||||
if (NONE_MARKER.equals(suuid)) {
|
||||
return new UserIdent(null, playerName);
|
||||
}
|
||||
try {
|
||||
UUID uuid = UUID.fromString(suuid);
|
||||
if (nobody.equals(uuid)) {
|
||||
return nobody;
|
||||
}
|
||||
return create(uuid, playerName);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return nobody;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a UserIdent from a legacy string. The string can either be a UUID or
|
||||
* a player name. Use this when reading legacy data or user configured values.
|
||||
*/
|
||||
public static @Nonnull UserIdent create(@Nullable String legacyData) {
|
||||
UUID uuid = PlayerUtil.getPlayerUIDUnstable(legacyData);
|
||||
if (uuid != null) {
|
||||
return new UserIdent(uuid, legacyData);
|
||||
} else if (legacyData != null) {
|
||||
return new UserIdent(null, legacyData);
|
||||
} else {
|
||||
return nobody;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a UserIdent from a GameProfile. Use this when creating a UserIdent
|
||||
* for a currently active player.
|
||||
*/
|
||||
public static @Nonnull UserIdent create(@Nullable GameProfile gameProfile) {
|
||||
if (gameProfile != null && (gameProfile.getId() != null || gameProfile.getName() != null)) {
|
||||
if (gameProfile.getId() != null && gameProfile.getName() != null
|
||||
&& gameProfile.getId().equals(offlineUUID(gameProfile.getName()))) {
|
||||
return new UserIdent(null, gameProfile.getName());
|
||||
} else {
|
||||
return new UserIdent(gameProfile.getId(), gameProfile.getName());
|
||||
}
|
||||
} else {
|
||||
return nobody;
|
||||
}
|
||||
}
|
||||
|
||||
private static @Nonnull UUID offlineUUID(String playerName) {
|
||||
UUID result = UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(Charsets.UTF_8));
|
||||
return result != null ? result : new UUID(-1, -1);
|
||||
}
|
||||
|
||||
private UserIdent(UUID uuid, String playerName) {
|
||||
this.uuid = uuid;
|
||||
this.uuid_offline = offlineUUID(playerName);
|
||||
this.playerName = playerName != null ? playerName : "[" + uuid + "]";
|
||||
}
|
||||
|
||||
@SuppressWarnings("null")
|
||||
// it's final(!), eclipse...
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + playerName.hashCode();
|
||||
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Please note that a UserIdent will successfully equal against GameProfiles
|
||||
* and UUIDs.
|
||||
*/
|
||||
@SuppressWarnings("null")
|
||||
// it's final(!), eclipse...
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof UserIdent) {
|
||||
UserIdent other = (UserIdent) obj;
|
||||
if (this.uuid != null && other.uuid != null) {
|
||||
return this.uuid.equals(other.uuid);
|
||||
}
|
||||
return this.uuid_offline.equals(other.uuid_offline);
|
||||
} else if (obj instanceof GameProfile) {
|
||||
GameProfile other = (GameProfile) obj;
|
||||
UUID other_uuid = other.getId();
|
||||
if (this.uuid != null && other_uuid != null) {
|
||||
return this.uuid.equals(other_uuid);
|
||||
}
|
||||
UUID uuid_offline_other = offlineUUID(other.getName());
|
||||
return uuid_offline_other.equals(this.uuid) || this.uuid_offline.equals(uuid_offline_other);
|
||||
} else if (obj instanceof UUID) {
|
||||
return obj.equals(uuid) || obj.equals(uuid_offline);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("null")
|
||||
// it's final(!), eclipse...
|
||||
public void saveToNbt(NBTTagCompound nbt, String prefix) {
|
||||
if (uuid != null) {
|
||||
nbt.setString(prefix + ".uuid", uuid.toString());
|
||||
}
|
||||
nbt.setString(prefix + ".login", playerName);
|
||||
}
|
||||
|
||||
public static boolean existsInNbt(NBTTagCompound nbt, String prefix) {
|
||||
return nbt.hasKey(prefix + ".uuid") || nbt.hasKey(prefix + ".login");
|
||||
}
|
||||
|
||||
public static @Nonnull UserIdent readfromNbt(NBTTagCompound nbt, String prefix) {
|
||||
String suuid = nbt.getString(prefix + ".uuid");
|
||||
String login = nbt.getString(prefix + ".login");
|
||||
if (Nobody.NOBODY_MARKER.equals(suuid)) {
|
||||
return nobody;
|
||||
}
|
||||
try {
|
||||
UUID uuid = UUID.fromString(suuid);
|
||||
return create(uuid, login);
|
||||
} catch (IllegalArgumentException e) {
|
||||
if (login != null && !login.isEmpty()) {
|
||||
return new UserIdent(null, login);
|
||||
} else {
|
||||
return nobody;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [uuid=" + (uuid != null ? uuid : "(unknown)") + ", name=" + playerName + "]";
|
||||
}
|
||||
|
||||
public static final @Nonnull Nobody nobody = new Nobody();
|
||||
|
||||
private static class Nobody extends UserIdent {
|
||||
private static final String NOBODY_MARKER = "nobody";
|
||||
|
||||
private Nobody() {
|
||||
super(null, "[unknown player]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return this == obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveToNbt(NBTTagCompound nbt, String prefix) {
|
||||
nbt.setString(prefix + ".uuid", NOBODY_MARKER);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user