Skip to content

Commit

Permalink
adds role commands and system
Browse files Browse the repository at this point in the history
  • Loading branch information
Noojuno committed Jul 13, 2020
1 parent d4c4b25 commit 6a24c03
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ forge*changelog.txt

# Libs directory
/libs
mcmodsrepo
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ To report a bug, please make an issue on this repository or message `Noojuno#546
- Download More Player Models for 1.12.2 from [here](https://www.curseforge.com/minecraft/mc-mods/more-player-models) and rename the jar to `moreplayermodels.jar`. Put the jar in a folder called `libs` in the projects root directory.
- Follow Minecraft Forges instructions on building a mod.

## Commands
- `/setrole <player> <index> <role>`
- `/addrole <player> <role>`
- `/removerole <player> <role}|ndex>`

## To-do:
- [ ] Character role system
- A role should have a name and a list of commands to run when selected (so permissions, etc can be given)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.0'
version = '004'
group = 'co.runed.multicharacter' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'multicharacter'

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/co/runed/multicharacter/MultiCharacterMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import co.runed.multicharacter.addons.minecraft.MinecraftAddon;
import co.runed.multicharacter.api.MultiCharacterAPI;
import co.runed.multicharacter.character.CharacterManager;
import co.runed.multicharacter.commands.CommandAddRole;
import co.runed.multicharacter.commands.CommandRemoveRole;
import co.runed.multicharacter.commands.CommandSetRole;
import co.runed.multicharacter.proxy.CommonProxy;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import org.apache.logging.log4j.Logger;

@Mod(modid = MultiCharacterMod.MODID, name = MultiCharacterMod.NAME, version = MultiCharacterMod.VERSION, dependencies = MultiCharacterMod.DEPENDENCIES, useMetadata = true)
Expand Down Expand Up @@ -56,6 +60,14 @@ public void postInit(FMLPostInitializationEvent event)
proxy.postInit();
}

@EventHandler
public void serverStart(FMLServerStartingEvent event)
{
event.registerServerCommand(new CommandSetRole());
event.registerServerCommand(new CommandRemoveRole());
event.registerServerCommand(new CommandAddRole());
}

public static MultiCharacterMod getInstance()
{
return instance;
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/co/runed/multicharacter/character/Character.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package co.runed.multicharacter.character;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.INBTSerializable;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Character implements INBTSerializable<NBTTagCompound>
{
private UUID uniqueId = UUID.randomUUID();
private String name;
private List<String> roles = new ArrayList<>();
private NBTTagCompound nbt = new NBTTagCompound();

public Character()
Expand Down Expand Up @@ -41,6 +47,75 @@ public void setName(String name)
this.name = name;
}

public void addRole(String role)
{
this.roles.add(role);
}

public void setRole(int index, String role)
{
if(index <= this.roles.size()) {
this.roles.set(index, role);
return;
}

for (int i = 0; i < index + 1; i++)
{
if (index >= this.roles.size() && i < index)
{
this.roles.add("");
}

if (i == index)
{
this.roles.set(i, role);
return;
}
}
}

public boolean removeRole(int index)
{
if (index < this.roles.size())
{
this.roles.remove(index);
return true;
}

return false;
}

public boolean removeRole(String role)
{
for (int i = 0; i < this.roles.size(); i++)
{
String r = this.roles.get(i);

if (r.equals(role))
{
this.roles.remove(i);
return true;
}
}

return false;
}

public boolean hasRole(String role)
{
return this.roles.contains(role);
}

public void setRoles(List<String> roles)
{
this.roles = roles;
}

public List<String> getRoles()
{
return roles;
}

public NBTTagCompound getNbt()
{
return this.nbt;
Expand All @@ -59,6 +134,15 @@ public NBTTagCompound serializeNBT()
tag.setString("name", this.getName());
tag.setTag("nbt", this.nbt);

NBTTagList roleList = new NBTTagList();

for (String role : this.roles)
{
roleList.appendTag(new NBTTagString(role));
}

tag.setTag("roles", roleList);

return tag;
}

Expand All @@ -68,6 +152,18 @@ public void deserializeNBT(NBTTagCompound nbt)
this.setUniqueId(nbt.getUniqueId("uuid"));
this.setName(nbt.getString("name"));

if (nbt.hasKey("roles"))
{
NBTTagList roleList = nbt.getTagList("roles", Constants.NBT.TAG_STRING);

for (int i = 0; i < roleList.tagCount(); i++)
{
String role = roleList.getStringTagAt(i);

this.addRole(role);
}
}

this.nbt = nbt.getCompoundTag("nbt");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,44 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiListExtended;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.ArrayList;
import java.util.List;

@SideOnly(Side.CLIENT)
public class GuiCharacterListEntry implements GuiListExtended.IGuiListEntry
{
public final CharacterListGuiScreen parent;
public final Character character;
public final Minecraft mc;
public GuiButton guiButton;
public String roles;

protected GuiCharacterListEntry(CharacterListGuiScreen parentScreen, Character character)
{
this.parent = parentScreen;
this.character = character;
this.mc = Minecraft.getMinecraft();
this.roles = "No roles set";

if(character.getRoles().size() > 0)
{
String roleStr = "";

for (String role : character.getRoles())
{
if (!role.isEmpty())
{
roleStr += role + ", " + TextFormatting.RESET;
}
}

if(roleStr.length() >= 4) {
this.roles = roleStr.substring(0, roleStr.length() - 4);
}
}
}

@Override
Expand All @@ -32,9 +54,7 @@ public void updatePosition(int slotIndex, int x, int y, float partialTicks)
public void drawEntry(int slotIndex, int x, int y, int listWidth, int slotHeight, int mouseX, int mouseY, boolean isSelected, float partialTicks)
{
this.mc.fontRenderer.drawString(this.character.getName(), x + 3, y + 1, 0xFFFFFF);
this.mc.fontRenderer.drawString("Role 1, Role 2, Role 3", x + 3, y + this.mc.fontRenderer.FONT_HEIGHT + 1, 0x808080);

//this.mc.fontRenderer.drawString(list.get(i), x + 3, y + 12 + this.mc.fontRenderer.FONT_HEIGHT * i, 0x808080);
this.mc.fontRenderer.drawString(this.roles, x + 3, y + this.mc.fontRenderer.FONT_HEIGHT + 1, 0x808080);
}

@Override
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/co/runed/multicharacter/commands/CommandAddRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package co.runed.multicharacter.commands;

import co.runed.multicharacter.MultiCharacterMod;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;

public class CommandAddRole extends CommandBase
{
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] params) throws CommandException
{
try {
String playerName = params[0];
String role = params[1];

EntityPlayer player = server.getPlayerList().getPlayerByUsername(playerName);

MultiCharacterMod.getCharacterManager().getActiveCharacter(player).addRole(role);

sender.sendMessage(new TextComponentString("Role '" + role + "' has been added to " + playerName));
}
catch (Exception e) {
sender.sendMessage(new TextComponentString("Error running command."));
}
}

@Override
public String getName() {
return "addrole";
}

@Override
public String getUsage(ICommandSender sender) {
return "command.addrole.usage";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package co.runed.multicharacter.commands;

import co.runed.multicharacter.MultiCharacterMod;
import co.runed.multicharacter.character.Character;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;

// /removerole <player> <index/role>
public class CommandRemoveRole extends CommandBase
{
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] params) throws CommandException
{
try {
String playerName = params[0];
String role = params[1];

EntityPlayer player = server.getPlayerList().getPlayerByUsername(playerName);
Character character = MultiCharacterMod.getCharacterManager().getActiveCharacter(player);

boolean success;
if(role.matches("-?\\d+")) {
success = character.removeRole(parseInt(role) - 1);
} else {
success = character.removeRole(role);
}

if(success) {
sender.sendMessage(new TextComponentString(playerName + "'s role has been removed"));
return;
}

sender.sendMessage(new TextComponentString("Invalid role."));
}
catch (Exception e) {
sender.sendMessage(new TextComponentString("Error running command."));
}
}

@Override
public String getName() {
return "removerole";
}

@Override
public String getUsage(ICommandSender sender) {
return "command.removerole.usage";
}
}
41 changes: 41 additions & 0 deletions src/main/java/co/runed/multicharacter/commands/CommandSetRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package co.runed.multicharacter.commands;

import co.runed.multicharacter.MultiCharacterMod;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;

public class CommandSetRole extends CommandBase
{
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] params) throws CommandException
{
try {
String playerName = params[0];
int index = parseInt(params[1]);
String role = params[2];

EntityPlayer player = server.getPlayerList().getPlayerByUsername(playerName);

MultiCharacterMod.getCharacterManager().getActiveCharacter(player).setRole(index - 1, role);

sender.sendMessage(new TextComponentString(playerName + "'s role has been set to '" + role + "'"));
}
catch (Exception e) {
sender.sendMessage(new TextComponentString("Error running command."));
}
}

@Override
public String getName() {
return "setrole";
}

@Override
public String getUsage(ICommandSender sender) {
return "command.setrole.usage";
}
}
6 changes: 5 additions & 1 deletion src/main/resources/assets/multicharacter/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ character.button.select=Select Character
character.button.create=Create New Character

key.categories.multicharacter=Multi Character
key.character.change=Change Character
key.character.change=Change Character

command.setrole.usage=/setrole <player> <index> <role>
command.addrole.usage=/addrole <player> <role>
command.removerole.usage=/removerole <player> <index|role>

0 comments on commit 6a24c03

Please sign in to comment.