diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfile.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfile.java index 70607f4f2..ed3133eaf 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandProfile.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfile.java @@ -23,6 +23,7 @@ public class CommandProfile extends CommandBase { public CommandProfile(){ registerCommand(help); registerCommand(new CommandProfileChange()); + registerCommand(new CommandProfileCreate()); registerCommand(new CommandProfileRemove()); registerCommand(new CommandProfileAdmin()); registerCommand(new CommandProfileList()); @@ -116,7 +117,7 @@ public class CommandProfile extends CommandBase { @Override public int getRequiredPermissionLevel(){ - return 0; + return 4; } public static String getCommandPermission(String command){ diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfileAdmin.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfileAdmin.java index 255a48fac..c2c710a08 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandProfileAdmin.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfileAdmin.java @@ -105,6 +105,7 @@ public class CommandProfileAdmin extends CommandProfileBase { } } + // Existing change subcommand update: @SubCommand( desc = "Change a specified player's active slot.", usage = " " @@ -144,6 +145,41 @@ public class CommandProfileAdmin extends CommandProfileBase { } } + // New admin create subcommand: + @SubCommand( + desc = "Create a new slot for a specified player's profile.", + usage = "" + ) + public void create(ICommandSender sender, String[] args) throws CommandException { + if(args.length < 1) { + sendError(sender, "Usage: /profile admin create "); + return; + } + String targetPlayer = args[0]; + Profile profile = ProfileController.getProfile(targetPlayer); + if(profile == null) { + sendError(sender, "Profile not found for player: " + targetPlayer); + return; + } + ProfileOperation result = ProfileController.createSlotInternal(profile); + switch(result) { + case NEW_SLOT_CREATED: + sendResult(sender, "New slot created successfully for %s.", targetPlayer); + break; + case MAX_SLOTS: + sendError(sender, "Player %s has reached maximum allowed slots.", targetPlayer); + break; + case LOCKED: + sendError(sender, "Profile for %s is locked. Please try again later.", targetPlayer); + break; + case VERIFICATION_FAILED: + sendError(sender, "Profile verification failed for %s.", targetPlayer); + break; + default: + sendError(sender, "Error creating new slot for %s.", targetPlayer); + } + } + @SubCommand( desc = "List all slots (IDs and names) for a specified player's profile.", usage = "" diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandProfileCreate.java b/src/main/java/kamkeel/npcs/command/profile/CommandProfileCreate.java new file mode 100644 index 000000000..b8610f33c --- /dev/null +++ b/src/main/java/kamkeel/npcs/command/profile/CommandProfileCreate.java @@ -0,0 +1,64 @@ +package kamkeel.npcs.command.profile; + +import kamkeel.npcs.controllers.ProfileController; +import kamkeel.npcs.controllers.data.Profile; +import kamkeel.npcs.controllers.data.ProfileOperation; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import static kamkeel.npcs.util.ColorUtil.sendError; +import static kamkeel.npcs.util.ColorUtil.sendResult; + +public class CommandProfileCreate extends CommandProfileBase { + + @Override + public String getCommandName() { + return "create"; + } + + @Override + public String getDescription() { + return "Create a new profile slot."; + } + + @Override + public String getUsage() { + return ""; + } + + @Override + public boolean runSubCommands() { + return false; + } + + @Override + public void processCommand(ICommandSender sender, String[] args) throws CommandException { + if(!(sender instanceof EntityPlayer)) { + sendError(sender, "This command can only be used by a player."); + return; + } + EntityPlayer player = (EntityPlayer)sender; + Profile profile = ProfileController.getProfile(player); + if(profile == null) { + sendError(sender, "Profile not found."); + return; + } + ProfileOperation result = ProfileController.createSlotInternal(profile); + switch(result) { + case NEW_SLOT_CREATED: + sendResult(sender, "New profile slot created successfully."); + break; + case MAX_SLOTS: + sendError(sender, "You have reached the maximum allowed slots."); + break; + case LOCKED: + sendError(sender, "Profile is locked. Please try again later."); + break; + case VERIFICATION_FAILED: + sendError(sender, "Profile verification failed. Creation not permitted."); + break; + default: + sendError(sender, "Error occurred while creating a new profile slot."); + } + } +} diff --git a/src/main/java/kamkeel/npcs/controllers/ProfileController.java b/src/main/java/kamkeel/npcs/controllers/ProfileController.java index 71c2d9f5e..be8ce4912 100644 --- a/src/main/java/kamkeel/npcs/controllers/ProfileController.java +++ b/src/main/java/kamkeel/npcs/controllers/ProfileController.java @@ -1,12 +1,14 @@ package kamkeel.npcs.controllers; import kamkeel.npcs.controllers.data.*; +import kamkeel.npcs.network.packets.data.ChatAlertPacket; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.server.MinecraftServer; import noppes.npcs.CustomNpcs; +import noppes.npcs.CustomNpcsPermissions; import noppes.npcs.LogWriter; import noppes.npcs.config.ConfigMain; import noppes.npcs.controllers.data.PlayerData; @@ -357,52 +359,78 @@ public class ProfileController { return ProfileOperation.SUCCESS; } - // Change the active slot in a Profile. - // If the new slot doesn't exist, it is created and NEW_SLOT_CREATED is returned. - // Saves current slot data before switching. - private static ProfileOperation changeSlotInternal(Profile profile, int newSlotId) { - if(profile.locked) { - LogWriter.error("Profile is locked; cannot change slot."); + public static ProfileOperation createSlotInternal(Profile profile) { + if (profile.locked) { + LogWriter.error("Profile is locked; cannot create slot."); return ProfileOperation.LOCKED; } - // Disallow switching to the same active slot. - if(profile.currentID == newSlotId) { - LogWriter.error("Slot " + newSlotId + " is already active."); - return ProfileOperation.ALREADY_ACTIVE; + int newSlotId = 0; + while (profile.slots.containsKey(newSlotId)) { + newSlotId++; + } + + // Check if the player is allowed to create a new slot. + if (!allowSlotPermission(profile.player)) { + LogWriter.error("Player has reached maximum allowed slots."); + return ProfileOperation.MAX_SLOTS; } // Run verification checks from all IProfileData in order of priority. - if(profile.player != null) { + if (profile.player != null) { List dataList = new ArrayList<>(profileTypes.values()); dataList.sort(Comparator.comparingInt(IProfileData::getSwitchPriority)); - for(IProfileData pd : dataList) { - if(!pd.verifySwitch(profile.player)) { + for (IProfileData pd : dataList) { + if (!pd.verifySwitch(profile.player)) { LogWriter.error("Verification check failed for profile type: " + pd.getTagName()); return ProfileOperation.VERIFICATION_FAILED; } } } - if(profile.player != null) { + // Create and add the new slot. + Slot newSlot = new Slot(newSlotId, "Slot " + newSlotId); + newSlot.setCompound(new NBTTagCompound()); + profile.slots.put(newSlotId, newSlot); + return ProfileOperation.NEW_SLOT_CREATED; + } + + // Change the active slot in a profile without automatically creating a new one. + // If the target slot does not exist, the operation fails. + private static ProfileOperation changeSlotInternal(Profile profile, int newSlotId) { + if (profile.locked) { + LogWriter.error("Profile is locked; cannot change slot."); + return ProfileOperation.LOCKED; + } + if (profile.currentID == newSlotId) { + LogWriter.error("Slot " + newSlotId + " is already active."); + return ProfileOperation.ALREADY_ACTIVE; + } + if (!profile.slots.containsKey(newSlotId)) { + LogWriter.error("Slot " + newSlotId + " does not exist."); + return ProfileOperation.ERROR; + } + // Run verification checks on all IProfileData in priority order. + if (profile.player != null) { + List dataList = new ArrayList<>(profileTypes.values()); + dataList.sort(Comparator.comparingInt(IProfileData::getSwitchPriority)); + for (IProfileData pd : dataList) { + if (!pd.verifySwitch(profile.player)) { + LogWriter.error("Verification check failed for profile type: " + pd.getTagName()); + return ProfileOperation.VERIFICATION_FAILED; + } + } + } + if (profile.player != null) { saveSlotData(profile.player); } - boolean createdNewSlot = false; - // If the new slot doesn't exist, create it. - if(!profile.slots.containsKey(newSlotId)) { - Slot newSlot = new Slot(newSlotId, "Slot " + newSlotId); - newSlot.setCompound(new NBTTagCompound()); - profile.slots.put(newSlotId, newSlot); - createdNewSlot = true; - } profile.currentID = newSlotId; - if(profile.player != null) { + if (profile.player != null) { loadSlotData(profile.player); } - // Update PlayerData so that PlayerData.get(player).profileSlot matches Profile.currentID. - if(profile.player != null) { + if (profile.player != null) { PlayerData pdata = PlayerData.get(profile.player); pdata.profileSlot = newSlotId; pdata.save(); } - return createdNewSlot ? ProfileOperation.NEW_SLOT_CREATED : ProfileOperation.SUCCESS; + return ProfileOperation.SUCCESS; } // ---------- Public Operations (Overloaded for EntityPlayer, UUID, or username) ---------- @@ -607,6 +635,51 @@ public class ProfileController { } + public static boolean allowSlotPermission(EntityPlayer player) { + Profile profile = ProfileController.getProfile(player); + int currentSlots = profile.slots.size(); + + // Unlimited slots if the player has the wildcard permission. + if (CustomNpcsPermissions.hasCustomPermission(player, "customnpcs.profile.max.*")) { + return true; + } + + int highestAllowed = 0; + for (int i = 1; i <= 50; i++) { + String perm = "customnpcs.profile.max." + i; + if (CustomNpcsPermissions.hasCustomPermission(player, perm)) { + highestAllowed = i; + } + } + + if (highestAllowed == 0) { + highestAllowed = 1; + } + + return currentSlots < highestAllowed; + } + + public void sendChatAlert(ProfileOperation operation, EntityPlayerMP player){ + switch (operation){ + case SUCCESS: + ChatAlertPacket.sendChatAlert(player, "quest.newquest", ": ", ""); + break; + case ERROR: + break; + case LOCKED: + break; + case PLAYER_NOT_FOUND: + break; + case NEW_SLOT_CREATED: + break; + case ALREADY_ACTIVE: + break; + case VERIFICATION_FAILED: + break; + case MAX_SLOTS: + break; + } + } public static EntityPlayerMP getPlayer(String username){ return MinecraftServer.getServer().getConfigurationManager().func_152612_a(username); diff --git a/src/main/java/kamkeel/npcs/controllers/data/ProfileOperation.java b/src/main/java/kamkeel/npcs/controllers/data/ProfileOperation.java index 3e6e81b90..59dc7e2ef 100644 --- a/src/main/java/kamkeel/npcs/controllers/data/ProfileOperation.java +++ b/src/main/java/kamkeel/npcs/controllers/data/ProfileOperation.java @@ -7,6 +7,7 @@ public enum ProfileOperation { PLAYER_NOT_FOUND, NEW_SLOT_CREATED, ALREADY_ACTIVE, - VERIFICATION_FAILED + VERIFICATION_FAILED, + MAX_SLOTS } diff --git a/src/main/java/kamkeel/npcs/network/PacketHandler.java b/src/main/java/kamkeel/npcs/network/PacketHandler.java index de5799d2b..91b24a22b 100644 --- a/src/main/java/kamkeel/npcs/network/PacketHandler.java +++ b/src/main/java/kamkeel/npcs/network/PacketHandler.java @@ -56,8 +56,7 @@ import kamkeel.npcs.network.packets.request.pather.MovingPathSavePacket; import kamkeel.npcs.network.packets.request.playerdata.PlayerDataGetPacket; import kamkeel.npcs.network.packets.request.playerdata.PlayerDataMapRegenPacket; import kamkeel.npcs.network.packets.request.playerdata.PlayerDataRemovePacket; -import kamkeel.npcs.network.packets.request.profile.ProfileGetInfoPacket; -import kamkeel.npcs.network.packets.request.profile.ProfileGetPacket; +import kamkeel.npcs.network.packets.request.profile.*; import kamkeel.npcs.network.packets.request.quest.*; import kamkeel.npcs.network.packets.request.recipe.RecipeGetPacket; import kamkeel.npcs.network.packets.request.recipe.RecipeRemovePacket; @@ -328,6 +327,10 @@ public class PacketHandler { // Profile Packets REQUEST_PACKET.registerPacket(new ProfileGetPacket()); REQUEST_PACKET.registerPacket(new ProfileGetInfoPacket()); + REQUEST_PACKET.registerPacket(new ProfileCreatePacket()); + REQUEST_PACKET.registerPacket(new ProfileRenamePacket()); + REQUEST_PACKET.registerPacket(new ProfileRenamePacket()); + REQUEST_PACKET.registerPacket(new ProfileChangePacket()); // Other Packets REQUEST_PACKET.registerPacket(new IsGuiOpenInform()); diff --git a/src/main/java/kamkeel/npcs/network/enums/EnumRequestPacket.java b/src/main/java/kamkeel/npcs/network/enums/EnumRequestPacket.java index 49e0b57b5..febd0386b 100644 --- a/src/main/java/kamkeel/npcs/network/enums/EnumRequestPacket.java +++ b/src/main/java/kamkeel/npcs/network/enums/EnumRequestPacket.java @@ -185,5 +185,6 @@ public enum EnumRequestPacket { EffectSave, ColorSet, - ProfileGet, ProfileGetAll, ProfileGetInfo, ColorBrush + ColorBrush, + ProfileGet, ProfileGetAll, ProfileGetInfo, ProfileCreate, ProfileRemove, ProfileRename, ProfileChange, } diff --git a/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileChangePacket.java b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileChangePacket.java new file mode 100644 index 000000000..4b6579bd5 --- /dev/null +++ b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileChangePacket.java @@ -0,0 +1,62 @@ +package kamkeel.npcs.network.packets.request.profile; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import io.netty.buffer.ByteBuf; +import kamkeel.npcs.controllers.ProfileController; +import kamkeel.npcs.controllers.data.ProfileOperation; +import kamkeel.npcs.network.AbstractPacket; +import kamkeel.npcs.network.PacketChannel; +import kamkeel.npcs.network.PacketHandler; +import kamkeel.npcs.network.enums.EnumRequestPacket; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import noppes.npcs.CustomNpcsPermissions; + +import java.io.IOException; + +public final class ProfileChangePacket extends AbstractPacket { + public static String packetName = "Request|ProfileChange"; + + private int slotID; + + public ProfileChangePacket() {} + + public ProfileChangePacket(int slotID) { + this.slotID = slotID; + } + + @Override + public Enum getType() { + return EnumRequestPacket.ProfileChange; + } + + @Override + public PacketChannel getChannel() { + return PacketHandler.REQUEST_PACKET; + } + + @Override + public CustomNpcsPermissions.Permission getPermission() { + return CustomNpcsPermissions.PROFILE_CHANGE; + } + + @SideOnly(Side.CLIENT) + @Override + public void sendData(ByteBuf out) throws IOException { + out.writeInt(this.slotID); + } + + @Override + public void receiveData(ByteBuf in, EntityPlayer player) throws IOException { + if (!(player instanceof EntityPlayerMP)) + return; + + int slot = in.readInt(); + ProfileOperation operation = ProfileController.changeSlot(player, slot); + // TODO: Check Operation and Send Message + + ProfileGetPacket.sendProfileNBT(player); + ProfileGetInfoPacket.sendProfileInfo(player); + } +} diff --git a/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileCreatePacket.java b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileCreatePacket.java new file mode 100644 index 000000000..102bb026d --- /dev/null +++ b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileCreatePacket.java @@ -0,0 +1,57 @@ +package kamkeel.npcs.network.packets.request.profile; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import io.netty.buffer.ByteBuf; +import kamkeel.npcs.controllers.ProfileController; +import kamkeel.npcs.controllers.data.Profile; +import kamkeel.npcs.controllers.data.ProfileOperation; +import kamkeel.npcs.network.AbstractPacket; +import kamkeel.npcs.network.PacketChannel; +import kamkeel.npcs.network.PacketHandler; +import kamkeel.npcs.network.enums.EnumRequestPacket; +import kamkeel.npcs.network.packets.data.large.GuiDataPacket; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.CustomNpcsPermissions; + +import java.io.IOException; + +public final class ProfileCreatePacket extends AbstractPacket { + public static String packetName = "Request|ProfileCreate"; + + public ProfileCreatePacket() {} + + @Override + public Enum getType() { + return EnumRequestPacket.ProfileCreate; + } + + @Override + public PacketChannel getChannel() { + return PacketHandler.REQUEST_PACKET; + } + + @Override + public CustomNpcsPermissions.Permission getPermission() { + return CustomNpcsPermissions.PROFILE_CREATE; + } + + @SideOnly(Side.CLIENT) + @Override + public void sendData(ByteBuf out) throws IOException {} + + @Override + public void receiveData(ByteBuf in, EntityPlayer player) throws IOException { + if (!(player instanceof EntityPlayerMP)) + return; + + Profile profile = ProfileController.getProfile(player); + ProfileOperation operation = ProfileController.createSlotInternal(profile); + // TODO: Check Operation and Send Message + + ProfileGetPacket.sendProfileNBT(player); + ProfileGetInfoPacket.sendProfileInfo(player); + } +} diff --git a/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileGetInfoPacket.java b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileGetInfoPacket.java index 5d853db50..50ce09dd3 100644 --- a/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileGetInfoPacket.java +++ b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileGetInfoPacket.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Map; public final class ProfileGetInfoPacket extends AbstractPacket { public static String packetName = "Request|ProfileGetInfo"; @@ -48,6 +47,10 @@ public final class ProfileGetInfoPacket extends AbstractPacket { if (!(player instanceof EntityPlayerMP)) return; + sendProfileInfo(player); + } + + public static void sendProfileInfo(EntityPlayer player){ NBTTagCompound compound = new NBTTagCompound(); compound.setBoolean("PROFILE_INFO", true); NBTTagList slotList = new NBTTagList(); @@ -69,7 +72,7 @@ public final class ProfileGetInfoPacket extends AbstractPacket { GuiDataPacket.sendGuiData((EntityPlayerMP) player, compound); } - public static HashMap> readSlotsFromNBT(NBTTagCompound compound) { + public static HashMap> readProfileInfo(NBTTagCompound compound) { HashMap> slotInfoMap = new HashMap<>(); if (!compound.getBoolean("PROFILE_INFO")) return slotInfoMap; diff --git a/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileGetPacket.java b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileGetPacket.java index feac5f37c..e86204acf 100644 --- a/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileGetPacket.java +++ b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileGetPacket.java @@ -43,6 +43,10 @@ public final class ProfileGetPacket extends AbstractPacket { if (!(player instanceof EntityPlayerMP)) return; + sendProfileNBT(player); + } + + public static void sendProfileNBT(EntityPlayer player){ Profile profile = ProfileController.getProfile(player); NBTTagCompound compound = profile.writeToNBT(); compound.setBoolean("PROFILE", true); diff --git a/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileRemovePacket.java b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileRemovePacket.java new file mode 100644 index 000000000..ed98fae19 --- /dev/null +++ b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileRemovePacket.java @@ -0,0 +1,64 @@ +package kamkeel.npcs.network.packets.request.profile; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import io.netty.buffer.ByteBuf; +import kamkeel.npcs.controllers.ProfileController; +import kamkeel.npcs.controllers.data.Profile; +import kamkeel.npcs.controllers.data.ProfileOperation; +import kamkeel.npcs.network.AbstractPacket; +import kamkeel.npcs.network.PacketChannel; +import kamkeel.npcs.network.PacketHandler; +import kamkeel.npcs.network.enums.EnumRequestPacket; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import noppes.npcs.CustomNpcsPermissions; + +import java.io.IOException; + +public final class ProfileRemovePacket extends AbstractPacket { + public static String packetName = "Request|ProfileRemove"; + + private int slotID; + + public ProfileRemovePacket() {} + + public ProfileRemovePacket(int slotID) { + this.slotID = slotID; + } + + @Override + public Enum getType() { + return EnumRequestPacket.ProfileRemove; + } + + @Override + public PacketChannel getChannel() { + return PacketHandler.REQUEST_PACKET; + } + + @Override + public CustomNpcsPermissions.Permission getPermission() { + return CustomNpcsPermissions.PROFILE_DELETE; + } + + @SideOnly(Side.CLIENT) + @Override + public void sendData(ByteBuf out) throws IOException { + out.writeInt(this.slotID); + } + + @Override + public void receiveData(ByteBuf in, EntityPlayer player) throws IOException { + if (!(player instanceof EntityPlayerMP)) + return; + + int slot = in.readInt(); + ProfileOperation operation = ProfileController.removeSlot(player, slot); + // TODO: Check Operation and Send Message + + + ProfileGetPacket.sendProfileNBT(player); + ProfileGetInfoPacket.sendProfileInfo(player); + } +} diff --git a/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileRenamePacket.java b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileRenamePacket.java new file mode 100644 index 000000000..c1496caf5 --- /dev/null +++ b/src/main/java/kamkeel/npcs/network/packets/request/profile/ProfileRenamePacket.java @@ -0,0 +1,77 @@ +package kamkeel.npcs.network.packets.request.profile; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import io.netty.buffer.ByteBuf; +import kamkeel.npcs.controllers.ProfileController; +import kamkeel.npcs.controllers.data.Profile; +import kamkeel.npcs.controllers.data.ProfileOperation; +import kamkeel.npcs.network.AbstractPacket; +import kamkeel.npcs.network.PacketChannel; +import kamkeel.npcs.network.PacketHandler; +import kamkeel.npcs.network.enums.EnumRequestPacket; +import kamkeel.npcs.util.ByteBufUtils; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import noppes.npcs.CustomNpcsPermissions; + +import java.io.IOException; + +import static kamkeel.npcs.util.ColorUtil.sendError; + +public final class ProfileRenamePacket extends AbstractPacket { + public static String packetName = "Request|ProfileRename"; + + private int slotID; + private String name; + + public ProfileRenamePacket() {} + + public ProfileRenamePacket(int slotID, String name) { + this.slotID = slotID; + this.name = name; + } + + @Override + public Enum getType() { + return EnumRequestPacket.ProfileRename; + } + + @Override + public PacketChannel getChannel() { + return PacketHandler.REQUEST_PACKET; + } + + @Override + public CustomNpcsPermissions.Permission getPermission() { + return CustomNpcsPermissions.PROFILE_RENAME; + } + + @SideOnly(Side.CLIENT) + @Override + public void sendData(ByteBuf out) throws IOException { + out.writeInt(this.slotID); + ByteBufUtils.writeString(out, this.name); + } + + @Override + public void receiveData(ByteBuf in, EntityPlayer player) throws IOException { + if (!(player instanceof EntityPlayerMP)) + return; + + int slot = in.readInt(); + String newName = ByteBufUtils.readString(in); + + Profile profile = ProfileController.getProfile(player); + if(!profile.slots.containsKey(slot)) { + // TODO: Check Operation and Send Message + return; + } + + ProfileController.getProfile(player).slots.get(slot).setName(newName); + ProfileController.save(player, ProfileController.getProfile(player)); + + ProfileGetPacket.sendProfileNBT(player); + ProfileGetInfoPacket.sendProfileInfo(player); + } +} diff --git a/src/main/java/noppes/npcs/CustomNpcsPermissions.java b/src/main/java/noppes/npcs/CustomNpcsPermissions.java index f494ddb9a..35aa84c0e 100644 --- a/src/main/java/noppes/npcs/CustomNpcsPermissions.java +++ b/src/main/java/noppes/npcs/CustomNpcsPermissions.java @@ -88,6 +88,14 @@ public class CustomNpcsPermissions{ public static final Permission SCRIPT_ITEM = new Permission("customnpcs.script.item"); public static final Permission SCRIPT_BLOCK = new Permission("customnpcs.script.block"); + // Profiles + public static final Permission PROFILE_ADMIN = new Permission("customnpcs.profile.admin"); + public static final Permission PROFILE_CREATE = new Permission("customnpcs.profile.create"); + public static final Permission PROFILE_DELETE = new Permission("customnpcs.profile.delete"); + public static final Permission PROFILE_RENAME = new Permission("customnpcs.profile.rename"); + public static final Permission PROFILE_CHANGE = new Permission("customnpcs.profile.change"); + public static final Permission PROFILE_MAX = new Permission("customnpcs.profile.max.*"); + public static CustomNpcsPermissions Instance; private Class bukkit; private Method getPlayer; diff --git a/src/main/java/noppes/npcs/client/gui/player/inventory/GuiProfiles.java b/src/main/java/noppes/npcs/client/gui/player/inventory/GuiProfiles.java index 9c9f00140..d8219546e 100644 --- a/src/main/java/noppes/npcs/client/gui/player/inventory/GuiProfiles.java +++ b/src/main/java/noppes/npcs/client/gui/player/inventory/GuiProfiles.java @@ -4,11 +4,11 @@ import kamkeel.npcs.controllers.data.InfoEntry; import kamkeel.npcs.controllers.data.Profile; import kamkeel.npcs.controllers.data.Slot; import kamkeel.npcs.network.PacketClient; -import kamkeel.npcs.network.packets.request.linked.LinkedNPCAddPacket; -import kamkeel.npcs.network.packets.request.profile.ProfileGetInfoPacket; -import kamkeel.npcs.network.packets.request.profile.ProfileGetPacket; +import kamkeel.npcs.network.packets.request.linked.LinkedItemRemovePacket; +import kamkeel.npcs.network.packets.request.profile.*; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiYesNo; import net.minecraft.client.gui.GuiYesNoCallback; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.nbt.NBTTagCompound; @@ -34,6 +34,7 @@ public class GuiProfiles extends GuiCNPCInventory implements ISubGuiListener, IC private GuiCustomScroll scroll; public HashMap data = new HashMap<>(); private String selected = null; + private String rename; private Profile profile; private Slot slot; private HashMap> slotInfoMap = new HashMap<>(); @@ -85,6 +86,26 @@ public class GuiProfiles extends GuiCNPCInventory implements ISubGuiListener, IC super.actionPerformed(guibutton); return; } + + if(guibutton.id == 4){ + GuiYesNo guiyesno = new GuiYesNo(this, StatCollector.translateToLocal("profile.create.message"), StatCollector.translateToLocal("gui.sure"), 104); + displayGuiScreen(guiyesno); + } + + if(this.slot == null) + return; + + if(guibutton.id == 1){ + GuiYesNo guiyesno = new GuiYesNo(this, slot.getName(), StatCollector.translateToLocal("profile.change.message"), 101); + displayGuiScreen(guiyesno); + } + if(guibutton.id == 2){ + setSubGui(new SubGuiEditText(slot.getName())); + } + if(guibutton.id == 3){ + GuiYesNo guiyesno = new GuiYesNo(this, slot.getName(), StatCollector.translateToLocal("gui.delete"), 103); + displayGuiScreen(guiyesno); + } } @Override @@ -261,68 +282,38 @@ public class GuiProfiles extends GuiCNPCInventory implements ISubGuiListener, IC NoppesUtil.openGUI(player, this); if (!result) return; - if (id == 1) { - if (data.containsKey(scroll.getSelected())) { - - // DELETE PROFILE - // PacketClient.sendClient(new LinkedNPCRemovePacket(scroll.getSelected())); - - initGui(); - } + if (id == 104) { + // Create Slot + PacketClient.sendClient(new ProfileCreatePacket()); } - if(id == 1){ - if (data.containsKey(scroll.getSelected())) { + if(slot == null) + return; - // RENAME PROFILE - // PacketClient.sendClient(new LinkedItemRemovePacket(data.get(scroll.getSelected()))); - - initGui(); - } + if(id == 101){ + // Change Slot + PacketClient.sendClient(new ProfileChangePacket(slot.getId())); } - if(id == 1){ - // CREATE PROFILE - // PacketClient.sendClient(new LinkedItemRemovePacket(data.get(scroll.getSelected()))); - - initGui(); + if(id == 102 && rename != null && !rename.equalsIgnoreCase(slot.getName())){ + // Rename Slot + PacketClient.sendClient(new ProfileRenamePacket(slot.getId(), rename)); } - if(id == 1){ - // CHANGE PROFILE - // PacketClient.sendClient(new LinkedItemRemovePacket(data.get(scroll.getSelected()))); - - initGui(); + if(id == 103){ + // Delete Slot + PacketClient.sendClient(new ProfileRemovePacket(slot.getId())); } } - public int getStringWidthWithoutColor(String text) { - int width = 0; - for (int i = 0; i < text.length(); i++) { - char c = text.charAt(i); - - if (c == 'ยง') { - if (i < text.length() - 1) { - i += 1; - } - } else { - // If not a color code, calculate the width - width += Minecraft.getMinecraft().fontRenderer.getCharWidth(c); - } - } - return width; - } - @Override public void subGuiClosed(SubGuiInterface subgui) { if(subgui instanceof SubGuiEditText){ if(!((SubGuiEditText)subgui).cancelled){ - PacketClient.sendClient(new LinkedNPCAddPacket(((SubGuiEditText)subgui).text)); + rename = ((SubGuiEditText)subgui).text; + GuiYesNo guiyesno = new GuiYesNo(this, rename, StatCollector.translateToLocal("profile.rename.message"), 102); + displayGuiScreen(guiyesno); } } } - public boolean isMouseOverRenderer(int x, int y) { - return x >= guiLeft + 10 && x <= guiLeft + 10 + 200 && y >= guiTop + 6 && y <= guiTop + 6 + 204; - } - @Override public void customScrollClicked(int i, int j, int k, GuiCustomScroll guiCustomScroll) { if (guiCustomScroll.id == 0) { @@ -343,19 +334,23 @@ public class GuiProfiles extends GuiCNPCInventory implements ISubGuiListener, IC public void customScrollDoubleClicked(String selection, GuiCustomScroll scroll) {} public void setGuiData(NBTTagCompound compound) { + this.slot = null; if(compound.hasKey("PROFILE")){ // Load Profile this.profile = new Profile(mc.thePlayer, compound); this.data = new HashMap<>(); - String colorCode = "\u00A7e"; + String currentSlot = "\u00A7e"; + String otherSlot = "\u00A7f"; for(Slot slot1 : profile.slots.values()){ String name = slot1.getId() + " - " + slot1.getName(); if(profile.currentID == slot1.getId()) - name = colorCode + name; + name = currentSlot + name; + else + name = otherSlot + name; this.data.put(name, slot1.getId()); } } else if(compound.hasKey("PROFILE_INFO")){ - slotInfoMap = ProfileGetInfoPacket.readSlotsFromNBT(compound); + slotInfoMap = ProfileGetInfoPacket.readProfileInfo(compound); } initGui(); } diff --git a/src/main/resources/assets/customnpcs/lang/en_US.lang b/src/main/resources/assets/customnpcs/lang/en_US.lang index 95a469c4f..2e827cb94 100644 --- a/src/main/resources/assets/customnpcs/lang/en_US.lang +++ b/src/main/resources/assets/customnpcs/lang/en_US.lang @@ -148,6 +148,9 @@ gui.sure=Are you sure? gui.available=Requirements Met gui.items=Items gui.npcs=NPCs +gui.change=Change +gui.create=Create +gui.rename=Rename party.accept=Accept party.allowParty=Allow Partying @@ -1307,3 +1310,7 @@ profile.lastLoaded=Last Loaded profile.info.quest.active=Quests Active profile.info.quest.finished=Quests Finished profile.info.dialog.read=Dialogs Read +profile.change.message=Do you want to change? +profile.create.message=Creating a new profile will change you to it. +profile.rename.message=Do you want to rename your slot? +profile.operation.error=There was an error in your request