Move SoulNetwork to UUID system
*Should* work correctly. As for all major changes, needs to be thoroughly tested.
This commit is contained in:
parent
8edb8a45e6
commit
24058d7350
14 changed files with 112 additions and 49 deletions
|
@ -8,7 +8,7 @@ public class Constants {
|
|||
|
||||
public static class NBT {
|
||||
|
||||
public static final String OWNER_NAME = "ownerName";
|
||||
public static final String OWNER_UUID = "ownerUUID";
|
||||
public static final String USES = "uses";
|
||||
public static final String UNUSABLE = "unusable";
|
||||
public static final String SACRIFICE = "sacrifice";
|
||||
|
|
|
@ -75,6 +75,8 @@ public class SoulNetwork extends WorldSavedData {
|
|||
if (event.getResult() != Event.Result.DENY)
|
||||
data.setCurrentEssence(newEss);
|
||||
|
||||
markDirty();
|
||||
|
||||
return newEss - currEss;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@ import net.minecraft.entity.player.EntityPlayer;
|
|||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class BindableHelper {
|
||||
|
||||
/**
|
||||
|
@ -20,7 +22,7 @@ public class BindableHelper {
|
|||
* @return - Whether binding was successful
|
||||
*/
|
||||
public static boolean checkAndSetItemOwner(ItemStack stack, EntityPlayer player) {
|
||||
return !PlayerHelper.isFakePlayer(player) && checkAndSetItemOwner(stack, player.getGameProfile().getName());
|
||||
return !PlayerHelper.isFakePlayer(player) && checkAndSetItemOwner(stack, PlayerHelper.getUUIDFromPlayer(player));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,26 +33,33 @@ public class BindableHelper {
|
|||
* Fires {@link ItemBindEvent}.
|
||||
*
|
||||
* @param stack - The ItemStack to bind
|
||||
* @param ownerName - The username to bind the ItemStack to
|
||||
* @param uuid - The username to bind the ItemStack to
|
||||
*
|
||||
* @return - Whether the binding was successful
|
||||
*/
|
||||
public static boolean checkAndSetItemOwner(ItemStack stack, String ownerName) {
|
||||
public static boolean checkAndSetItemOwner(ItemStack stack, String uuid) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
|
||||
if (!(stack.getItem() instanceof IBindable))
|
||||
return false;
|
||||
|
||||
if (Strings.isNullOrEmpty(stack.getTagCompound().getString(Constants.NBT.OWNER_NAME))) {
|
||||
MinecraftForge.EVENT_BUS.post(new ItemBindEvent(PlayerHelper.getPlayerFromUsername(ownerName), ownerName, stack));
|
||||
((IBindable) stack.getItem()).onBind(PlayerHelper.getPlayerFromUsername(ownerName), stack);
|
||||
stack.getTagCompound().setString(Constants.NBT.OWNER_NAME, ownerName);
|
||||
if (Strings.isNullOrEmpty(stack.getTagCompound().getString(Constants.NBT.OWNER_UUID))) {
|
||||
MinecraftForge.EVENT_BUS.post(new ItemBindEvent(PlayerHelper.getPlayerFromUUID(uuid), uuid, stack));
|
||||
((IBindable) stack.getItem()).onBind(PlayerHelper.getPlayerFromUUID(uuid), stack);
|
||||
stack.getTagCompound().setString(Constants.NBT.OWNER_UUID, uuid);
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BindableHelper#checkAndSetItemOwner(ItemStack, String)
|
||||
*/
|
||||
public static boolean checkAndSetItemOwner(ItemStack stack, UUID uuid) {
|
||||
return checkAndSetItemOwner(stack, uuid.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Owner of the item without checking if it is already bound.
|
||||
* Also bypasses {@link ItemBindEvent}.
|
||||
|
@ -61,7 +70,7 @@ public class BindableHelper {
|
|||
public static void setItemOwner(ItemStack stack, String ownerName) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
|
||||
stack.getTagCompound().setString(Constants.NBT.OWNER_NAME, ownerName);
|
||||
stack.getTagCompound().setString(Constants.NBT.OWNER_UUID, ownerName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,6 +83,19 @@ public class BindableHelper {
|
|||
public static String getOwnerName(ItemStack stack) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
|
||||
return stack.getTagCompound().getString(Constants.NBT.OWNER_NAME);
|
||||
return PlayerHelper.getUsernameFromStack(stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to safely obtain the UUID of the ItemStack's owner
|
||||
*
|
||||
* @param stack - The ItemStack to check the owner of
|
||||
*
|
||||
* @return - The UUID of the ItemStack's owner
|
||||
*/
|
||||
public static String getOwnerUUID(ItemStack stack) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
|
||||
return stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ import net.minecraft.world.World;
|
|||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class NetworkHelper {
|
||||
|
||||
// Get
|
||||
|
@ -20,22 +22,36 @@ public class NetworkHelper {
|
|||
/**
|
||||
* Gets the SoulNetwork for the player.
|
||||
*
|
||||
* @param ownerName - The name of the SoulNetwork owner
|
||||
* @param world - The world
|
||||
* @param name - The username of the SoulNetwork owner
|
||||
* @param world - The world
|
||||
*
|
||||
* @return - The SoulNetwork for the given name.
|
||||
*/
|
||||
public static SoulNetwork getSoulNetwork(String ownerName, World world) {
|
||||
SoulNetwork network = (SoulNetwork) world.getMapStorage().loadData(SoulNetwork.class, ownerName);
|
||||
public static SoulNetwork getSoulNetwork(String name, World world) {
|
||||
SoulNetwork network = (SoulNetwork) world.getMapStorage().loadData(SoulNetwork.class, name);
|
||||
|
||||
if (network == null) {
|
||||
network = new SoulNetwork(ownerName);
|
||||
world.getMapStorage().setData(ownerName, network);
|
||||
network = new SoulNetwork(name);
|
||||
world.getMapStorage().setData(name, network);
|
||||
}
|
||||
|
||||
return network;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see NetworkHelper#getSoulNetwork(String, World)
|
||||
*/
|
||||
public static SoulNetwork getSoulNetwork(UUID uuid, World world) {
|
||||
return getSoulNetwork(PlayerHelper.getUsernameFromUUID(uuid), world);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see NetworkHelper#getSoulNetwork(String, World)
|
||||
*/
|
||||
public static SoulNetwork getSoulNetwork(EntityPlayer player, World world) {
|
||||
return getSoulNetwork(PlayerHelper.getUUIDFromPlayer(player), world);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current orb tier of the SoulNetwork.
|
||||
*
|
||||
|
@ -79,7 +95,7 @@ public class NetworkHelper {
|
|||
*/
|
||||
public static boolean syphonFromContainer(ItemStack stack, World world, int toSyphon) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_NAME);
|
||||
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
|
||||
|
||||
if (Strings.isNullOrEmpty(ownerName))
|
||||
return false;
|
||||
|
@ -122,7 +138,7 @@ public class NetworkHelper {
|
|||
return false;
|
||||
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_NAME);
|
||||
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
|
||||
|
||||
if (!Strings.isNullOrEmpty(ownerName)) {
|
||||
SoulNetworkEvent.ItemDrainNetworkEvent event = new SoulNetworkEvent.ItemDrainNetworkEvent(player, ownerName, stack, syphon);
|
||||
|
@ -149,7 +165,7 @@ public class NetworkHelper {
|
|||
@Deprecated
|
||||
public static boolean syphonFromNetworkWhileInContainer(ItemStack stack, int syphon) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_NAME);
|
||||
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
|
||||
|
||||
if (Strings.isNullOrEmpty(ownerName))
|
||||
return false;
|
||||
|
@ -165,7 +181,7 @@ public class NetworkHelper {
|
|||
@Deprecated
|
||||
public static int syphonFromNetwork(ItemStack stack, int syphon) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_NAME);
|
||||
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
|
||||
if (!Strings.isNullOrEmpty(ownerName))
|
||||
return syphonFromNetwork(ownerName, syphon);
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import net.minecraft.item.ItemStack;
|
|||
import net.minecraft.potion.Potion;
|
||||
import net.minecraft.potion.PotionEffect;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraftforge.common.UsernameCache;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
import java.util.UUID;
|
||||
|
@ -17,7 +18,7 @@ public class PlayerHelper {
|
|||
private static final Pattern FAKE_PLAYER_PATTERN = Pattern.compile("^(?:\\[.*\\])|(?:ComputerCraft)$");
|
||||
|
||||
public static String getUsernameFromPlayer(EntityPlayer player) {
|
||||
return player.getGameProfile().getName();
|
||||
return UsernameCache.getLastKnownUsername(getUUIDFromPlayer(player));
|
||||
}
|
||||
|
||||
public static EntityPlayer getPlayerFromUsername(String username) {
|
||||
|
@ -27,10 +28,32 @@ public class PlayerHelper {
|
|||
return MinecraftServer.getServer().getConfigurationManager().getPlayerByUsername(username);
|
||||
}
|
||||
|
||||
public static EntityPlayer getPlayerFromUUID(String uuid) {
|
||||
return getPlayerFromUsername(getUsernameFromUUID(uuid));
|
||||
}
|
||||
|
||||
public static EntityPlayer getPlayerFromUUID(UUID uuid) {
|
||||
return getPlayerFromUsername(getUsernameFromUUID(uuid));
|
||||
}
|
||||
|
||||
public static UUID getUUIDFromPlayer(EntityPlayer player) {
|
||||
return player.getGameProfile().getId();
|
||||
}
|
||||
|
||||
public static String getUsernameFromUUID(String uuid) {
|
||||
return UsernameCache.getLastKnownUsername(UUID.fromString(uuid));
|
||||
}
|
||||
|
||||
public static String getUsernameFromUUID(UUID uuid) {
|
||||
return UsernameCache.getLastKnownUsername(uuid);
|
||||
}
|
||||
|
||||
public static String getUsernameFromStack(ItemStack stack) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
|
||||
return PlayerHelper.getUsernameFromUUID(stack.getTagCompound().getString(Constants.NBT.OWNER_UUID));
|
||||
}
|
||||
|
||||
public static boolean isFakePlayer(EntityPlayer player) {
|
||||
return player instanceof FakePlayer || FAKE_PLAYER_PATTERN.matcher(getUsernameFromPlayer(player)).matches();
|
||||
}
|
||||
|
@ -38,8 +61,8 @@ public class PlayerHelper {
|
|||
public static void causeNauseaToPlayer(ItemStack stack) {
|
||||
stack = NBTHelper.checkNBT(stack);
|
||||
|
||||
if (!Strings.isNullOrEmpty(stack.getTagCompound().getString(Constants.NBT.OWNER_NAME)))
|
||||
causeNauseaToPlayer(stack.getTagCompound().getString(Constants.NBT.OWNER_NAME));
|
||||
if (!Strings.isNullOrEmpty(stack.getTagCompound().getString(Constants.NBT.OWNER_UUID)))
|
||||
causeNauseaToPlayer(stack.getTagCompound().getString(Constants.NBT.OWNER_UUID));
|
||||
}
|
||||
|
||||
public static void causeNauseaToPlayer(String ownerName) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue