Buff framework implementation. Administration commands added. PlayerData

structure has been added.
AlexBranch
Joshua Sigona 10 years ago
parent f36de77761
commit c4b2441889
  1. 183
      SigJobs/java/org/sig/jobs/Commands/AdminCommandHandler.java
  2. 37
      SigJobs/java/org/sig/jobs/Commands/TimeCommandHandler.java
  3. 26
      SigJobs/java/org/sig/jobs/EntityHandler.java
  4. 203
      SigJobs/java/org/sig/jobs/PlayerData.java
  5. 59
      SigJobs/java/org/sig/jobs/Players/Buff.java
  6. 68
      SigJobs/java/org/sig/jobs/main.java

@ -0,0 +1,183 @@
package org.sig.jobs.Commands;
import java.text.NumberFormat;
import java.util.UUID;
import org.sig.jobs.PlayerData;
import org.sig.jobs.main;
import org.sig.jobs.Players.Buff;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
public class AdminCommandHandler extends CommandBase {
@Override
public String getCommandName() {
// TODO Auto-generated method stub
return "admin";
}
@Override
public String getCommandUsage(ICommandSender p) {
// TODO Auto-generated method stub
return "/admin - Admin commands.";
}
boolean hasPermission(ICommandSender p) {
boolean approved=false;
if (p instanceof EntityPlayer) {
if (main.AdminList.contains(((EntityPlayer) p).getDisplayName())) {
approved=true;
}
} else {
//Has to come from console.
approved=true;
}
return approved;
}
public static void sendMessage(ICommandSender p, String message) {
if (p instanceof EntityPlayer) {
EntityPlayer pl = (EntityPlayer)p;
pl.addChatComponentMessage(new ChatComponentText(message));
} else {
System.out.println(message);
}
}
@Override
public void processCommand(ICommandSender p, String[] msg) {
// TODO Auto-generated method stub
if (hasPermission(p)) {
switch(msg.length) {
case 1:{
switch (msg[0]) {
case "addAdmin":{
sendMessage(p,"addAdmin <name>");
}break;
case "removeAdmin":{
sendMessage(p,"removeAdmin <name>");
}break;
case "addBuff":{
sendMessage(p,"addBuff <id> <lv> <duration>");
}break;
case "removeBuff":{
sendMessage(p,"removeBuff <id>");
}break;
case "displayBuffs":{
if (p instanceof EntityPlayer) {
EntityPlayer pl = (EntityPlayer) p;
PlayerData pd = main.getPlayerData(pl.getDisplayName());
int i=0;
for (Buff buff : pd.getBuffs()) {
sendMessage(p,"Buff "+i+": ID:"+buff.getID()+",Level:"+buff.getLevel()+",Duration:"+buff.getDuration());
i++;
}
}
}break;
default:{
sendMessage(p,"Unknown Command.");
}
}
}break;
case 2:{
switch (msg[0]) {
case "addAdmin":{
boolean found=false;
boolean found2=false;
for (String s2 : main.AdminList) {
if (msg[1].equalsIgnoreCase(s2)) {
//main.AdminList.remove(s2);
//sendMessage(p,"Removed player "+msg[1]+" from list of Admins.");
found2=true;
break;
}
}
if (!found2) {
EntityPlayer pl = PlayerData.getPlayer(msg[1]);
if (pl!=null && pl.getDisplayName().equalsIgnoreCase(msg[1])) {
//sendMessage(p,"Comparing "+msg[1]+" to "+s);
main.AdminList.add(PlayerData.getPlayer(msg[1]).getDisplayName());
sendMessage(p,"Added player "+msg[1]+" to list of Admins.");
found=true;
break;
}
} else {
sendMessage(p,"Player "+msg[1]+" already added to admin list.");
}
if (!found && !found2) {sendMessage(p,"Could not find player "+msg[1]+".");}
}break;
case "removeAdmin":{
boolean found=false;
for (String s : main.AdminList) {
if (msg[1].equalsIgnoreCase(s)) {
main.AdminList.remove(s);
sendMessage(p,"Removed player "+msg[1]+" from list of Admins.");
found=true;
break;
}
}
if (!found) {sendMessage(p,"Could not find player "+msg[1]+".");}
}break;
case "removeBuff":{
if (p instanceof EntityPlayer) {
sendMessage(p,"removeBuff "+msg[1]);
EntityPlayer pl = (EntityPlayer)p;
PlayerData pd = main.getPlayerData(pl.getDisplayName());
if (pd!=null) {
pd.removeBuff(Integer.valueOf(msg[1]));
} else {
sendMessage(p,"Could not remove buff from Player "+pl.getDisplayName());
}
}
}break;
default:{
sendMessage(p,"Unknown Command.");
}
}
}break;
case 4:{
switch (msg[0]) {
case "addBuff":{
if (p instanceof EntityPlayer) {
sendMessage(p,"addBuff "+msg[1]+" "+msg[2]+" "+msg[3]);
EntityPlayer pl = (EntityPlayer)p;
PlayerData pd = main.getPlayerData(pl.getDisplayName());
if (pd!=null) {
pd.applyBuff(Integer.valueOf(msg[1]), Integer.valueOf(msg[2]), Integer.valueOf(msg[3]));
} else {
sendMessage(p,"Could not add buff to Player "+pl.getDisplayName());
}
} else {
sendMessage(p,"Can only be done to a player.");
}
}break;
default:{
sendMessage(p,"Unknown Command.");
}
}
}break;
default:{
sendMessage(p,"Unknown Command.");
}
}
} else {
sendMessage(p,EnumChatFormatting.RED+"You do not have permissions to do this.");
}
}
@Override
public boolean canCommandSenderUseCommand(ICommandSender p) {
if (hasPermission(p)) {
return true;
} else {
return false;
}
}
}

@ -0,0 +1,37 @@
package org.sig.jobs.Commands;
import java.text.NumberFormat;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
public class TimeCommandHandler extends CommandBase {
@Override
public String getCommandName() {
// TODO Auto-generated method stub
return "servertime";
}
@Override
public String getCommandUsage(ICommandSender p) {
// TODO Auto-generated method stub
return "/servertime - Returns the current server time.";
}
@Override
public void processCommand(ICommandSender p, String[] msg) {
// TODO Auto-generated method stub
AdminCommandHandler.sendMessage(p, "Current Server Time: "+EnumChatFormatting.GRAY+MinecraftServer.getServer().getTickCounter());
}
@Override
public boolean canCommandSenderUseCommand(ICommandSender p) {
return true;
}
}

@ -2,7 +2,9 @@ package org.sig.jobs;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
public class EntityHandler {
@SubscribeEvent
@ -16,12 +18,34 @@ public class EntityHandler {
//Initialize jobs.
for (int i=0;i<main.MAX_JOBS;i++) {
if (!p.getEntityData().hasKey("job"+(i+1))) {
System.out.println("Player "+p.getUniqueID()+" does not have data. Writing new data.");
System.out.println("Player "+p.getPersistentID()+" does not have data. Writing new data.");
p.getEntityData().setString("job"+(i+1), "");
p.getEntityData().setInteger("joblv"+(i+1), 1);
p.getEntityData().setInteger("jobexp"+(i+1), 0);
}
}
boolean alreadyExists=false;
for (PlayerData pd : main.PlayerList) {
if (pd.getID().equals(p.getPersistentID())) {
alreadyExists=true;
break;
}
}
if (!alreadyExists) {
main.PlayerList.add(new PlayerData(p.getPersistentID()));
}
}
}
@SubscribeEvent
public void onPlayerDamage(LivingHurtEvent ev) {
if (ev.entityLiving instanceof EntityPlayer) {
//This is a player that has been hit.
EntityPlayer p = (EntityPlayer) ev.entityLiving;
float dmg = ev.ammount;
//Check for any damage reduction buffs.
}
}
}

@ -0,0 +1,203 @@
package org.sig.jobs;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.sig.jobs.Players.Buff;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
/**
* A structure that defines data for each entering player.
* @author sigonasr2
*
*/
public class PlayerData {
UUID id; //Stores the ID this PlayerData refers to.
String name; //The player's name.
List<Buff> buffs; //A list of buffs this PlayerData contains.
/**
* Returns the player instance tied to this PlayerData.
* @return Will return null if it cannot find the player!
*/
EntityPlayer getPlayer() {
//return MinecraftServer.getServer().getEntityWorld().getPlayerEntityByName(name);
return getPlayer(id);
}
//@Deprecated
/**
* Returns a player instance given a UUID.
* @param id The UUID.
* @return Will return null if it cannot find the player!
*/
public static EntityPlayer getPlayer(UUID id) {
MinecraftServer serv = MinecraftServer.getServer();
List<EntityPlayer> playerEntities = serv.getEntityWorld().playerEntities;
for (EntityPlayer p : playerEntities) {
//System.out.println("Comparing "+p.getUniqueID()+" to "+id+".");
if (p.getUniqueID().equals(id)) {
return p;
}
}
/*
for (int i=0;i<playerEntities.size();i++) {
if (playerEntities.get(i) instanceof EntityPlayer) {
EntityPlayer p = (EntityPlayer)playerEntities.get(i);
if (p.getUniqueID().equals(id)) {
return p;
}
}
}*/
return null;
}
/**
* Returns a player instance given a name.
* @param name The name of the player.
* @return
*/
public static EntityPlayer getPlayer(String name) {
MinecraftServer serv = MinecraftServer.getServer();
List<EntityPlayer> playerEntities = serv.getEntityWorld().playerEntities;
for (EntityPlayer p : playerEntities) {
//System.out.println("Comparing "+p.getUniqueID()+" to "+id+".");
if (p.getDisplayName().equalsIgnoreCase(name)) {
return p;
}
}
/*
for (int i=0;i<playerEntities.size();i++) {
if (playerEntities.get(i) instanceof EntityPlayer) {
EntityPlayer p = (EntityPlayer)playerEntities.get(i);
if (p.getUniqueID().equals(id)) {
return p;
}
}
}*/
return null;
//return MinecraftServer.getServer().getEntityWorld().getPlayerEntityByName(name);
}
/**
*
* @param name
*/
public PlayerData(UUID id) {
this.id=id;
//Get the player's name.
//this.name = getPlayer(id).getDisplayName();
buffs = new ArrayList<Buff>();
}
@Deprecated
public PlayerData(String name) {
this.id=MinecraftServer.getServer().getEntityWorld().getPlayerEntityByName(name).getUniqueID();
//Get the player's name.
//this.name = MinecraftServer.getServer().getEntityWorld().getPlayerEntityByName(name).getDisplayName();
buffs = new ArrayList<Buff>();
}
/**
* Returns the UUID of the player tied to this PlayerData.
* @return
*/
public UUID getID() {
return id;
}
/**
* Returns the buff array of this player.
* @return
*/
public List<Buff> getBuffs() {
return buffs;
}
/**
* Returns true if this player has a certain buff. False otherwise.
* @param id The ID of the buff we are searching for. See main.BUFF_*
* @return
*/
public boolean hasBuff(int id) {
for (int i=0;i<buffs.size();i++) {
if (buffs.get(i).getID()==id) {
return true;
}
}
return false;
}
/**
* Returns the Buff object for a particular buff on the player.
* @param id The ID of the buff we are searching for. See main.BUFF_*
* @return Returns null if it cannot find it.
*/
public Buff getBuff(int id) {
for (int i=0;i<buffs.size();i++) {
if (buffs.get(i).getID()==id) {
return buffs.get(i);
}
}
return null;
}
/**
* Applies a buff to this PlayerData object. If the buff already exists, it will
* attempt to override it if the lv of this buff is >= the current buff. You can
* forceOverride this by setting the appropriate argument to true. Returns whether it worked or not.
* @param id The ID of the buff. See main.BUFF_*
* @param lv The Level/Power of the buff.
* @param duration The duration (in ticks) of this buff.
*/
public boolean applyBuff(int id, int lv, int duration) {
return applyBuff(id,lv,duration,false);
}
/**
* Applies a buff to this PlayerData object. If the buff already exists, it will
* attempt to override it if the lv of this buff is >= the current buff. You can
* forceOverride this by setting the appropriate argument to true. Returns whether it worked or not.
* @param id The ID of the buff. See main.BUFF_*
* @param lv The Level/Power of the buff.
* @param duration The duration (in ticks) of this buff.
* @param forceOverride Whether or not to forcefully apply the buff, regardless of current buff level.
*/
public boolean applyBuff(int id, int lv, int duration, boolean forceOverride) {
if (hasBuff(id)) {
System.out.println("Found this buff.");
Buff buff = getBuff(id);
if (buff!=null && (forceOverride || buff.getLevel()<=lv)) {
//We're allowed to override it.
buffs.remove(buff);
buffs.add(new Buff(id,lv,duration));
return true;
} else {
return false;
}
} else {
buffs.add(new Buff(id,lv,duration));
return true;
}
}
/**
* Attempts to remove a buff on this PlayerData object. Returns whether it worked or not.
* @param id The ID of the buff. See main.BUFF_*
* @return
*/
public boolean removeBuff(int id) {
if (hasBuff(id)) {
Buff buff = getBuff(id);
buffs.remove(buff);
return true;
} else {
return false;
}
}
}

@ -0,0 +1,59 @@
package org.sig.jobs.Players;
import net.minecraft.server.MinecraftServer;
/**
* A buff structure contains a duration, and a buff ID, which determines what type of buff it is.
* @author sigonasr2
*
*/
public class Buff {
int id;
int lv; //Level/Potency of the buff.
int timeoff; //The server tick time when the buff wears out.
int duration; //Saves the duration of this buff.
public Buff(int id, int level,int duration) {
this.id=id;
this.lv=level;
this.timeoff=MinecraftServer.getServer().getTickCounter()+duration;
this.duration=duration;
}
/**
* Returns the ID of this buff.
*/
public int getID() {
return id;
}
/**
* Returns the level of this buff.
*/
public int getLevel() {
return lv;
}
/**
* Returns the total duration of this buff.
* @return
*/
public int getDuration() {
return duration;
}
/**
* Returns the time this buff has left (in ticks).
*/
public int getTimeRemaining() {
return timeoff-MinecraftServer.getServer().getTickCounter();
}
/**
* Returns the server time when this buff wears off.
*/
public int getTimeOff() {
return timeoff;
}
}

@ -3,9 +3,12 @@ package org.sig.jobs;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.sig.jobs.Commands.AdminCommandHandler;
import org.sig.jobs.Commands.JobsCommandHandler;
import org.sig.jobs.Commands.MoneyCommandHandler;
import org.sig.jobs.Commands.TimeCommandHandler;
import net.minecraft.command.ICommandManager;
import net.minecraft.command.ServerCommandManager;
@ -42,11 +45,17 @@ public class main {
"GREEN", "Woodcutter"
};
public static final int BUFF_DAMAGEREDUCTION = 0;
public static final int BUFF_DAMAGEINCREASE = 0;
public static double STARTMONEY = 100;
public static final String MODID = "SigJobs";
public static final String VERSION = "0.0.1";
public static List<PlayerData> PlayerList;
public static List<String> AdminList;
public static int JOBMAXPLAYERS = 0; //The maximum amount of players that can occupy one job.
/*This is a factor of the total number of players that have played on the server.*/
@ -100,6 +109,45 @@ public class main {
public static List<String> JOB_INFO14;
public static List<String> JOB_STATINFO14;
/**
* Returns the PlayerData instance of a player.
* @param id The UUID of the player to search for.
* @return Returns null if this fails.
*/
public PlayerData getPlayerData(UUID id) {
for (PlayerData i : PlayerList) {
if (i.getID().equals(id)) {
return i;
}
}
/*
for (int i=0;i<PlayerList.size();i++) {
if (PlayerList.get(i).getID()==id) {
return PlayerList.get(i);
}
}*/
return null;
}
/**
* Returns the PlayerData instance of a player.
* @param name The name of the player. (Case-insensitive)
* @return Returns null if this fails.
*/
public static PlayerData getPlayerData(String name) {
for (PlayerData i : PlayerList) {
if (i.getPlayer().getDisplayName().equalsIgnoreCase(name)) {
return i;
}
}
/*
for (int i=0;i<PlayerList.size();i++) {
if (PlayerList.get(i).getPlayer().getDisplayName().equalsIgnoreCase(name)) {
return PlayerList.get(i);
}
}*/
return null;
}
@EventHandler
public void preInit(FMLPreInitializationEvent ev) {
@ -275,9 +323,9 @@ public class main {
JOB_STATINFO5.add("Lv40: All foods cooked will provide full health heals when eaten and include a temporary 10 HP shield (30 sec).");
JOB_INFO6 = new ArrayList<String>();
JOB_INFO6.add(EnumChatFormatting.YELLOW+"Cook");
JOB_INFO6.add(EnumChatFormatting.GOLD+"Digger");
JOB_INFO6.add("");
JOB_INFO6.add("A Cook can create delicacies that have interesting effects beyond just keeping yourself full.");
JOB_INFO6.add("A Digger");
JOB_INFO6.add("");
JOB_INFO6.add("COOK:");
JOB_INFO6.add("There are a large abundance of foods to create. Ones that have more steps and ingredients give much more than simpler recipes.");
@ -338,6 +386,9 @@ public class main {
JOB_STATINFO14.add("Lv38: -30% damage reduction");
JOB_STATINFO14.add("Lv40: Trees can be cut down in one click by right-clicking with an axe.");
PlayerList = new ArrayList<PlayerData>();
AdminList = new ArrayList<String>();
Configuration config = new Configuration(new File("SigJobs.cfg"));
config.load(); //Create/load our config.
for (int i=0;i<JOBLIMIT.length;i++) {
@ -348,6 +399,12 @@ public class main {
expmult = config.get("SIGJOBS", "XPMULT", expmult).getDouble();
baseexp = config.get("SIGJOBS", "BASEXP", baseexp).getInt();
startexp = config.get("SIGJOBS", "STARTXP", startexp).getInt();
String[] emptyset = {};
String[] adminslist = config.get("SIGJOBS", "ADMINS", emptyset).getStringList();
for (int i=0;i<adminslist.length;i++) {
AdminList.add(adminslist[i]);
}
config.save(); //Save our config file.
}
@ -356,7 +413,7 @@ public class main {
public void init(FMLInitializationEvent ev) {
MinecraftForge.EVENT_BUS.register(new BlockBreakHandler());
MinecraftForge.EVENT_BUS.register(new EntityHandler());
MinecraftForge.EVENT_BUS.register(new Jobs());
//MinecraftForge.EVENT_BUS.register(new Jobs());
}
@EventHandler
@ -366,6 +423,8 @@ public class main {
ServerCommandManager servmanager = (ServerCommandManager)commander;
servmanager.registerCommand(new JobsCommandHandler());
servmanager.registerCommand(new MoneyCommandHandler());
servmanager.registerCommand(new TimeCommandHandler());
servmanager.registerCommand(new AdminCommandHandler());
}
@EventHandler
@ -375,6 +434,9 @@ public class main {
for (int i=0;i<JOBLIMIT.length;i++) {
config.get("SIGJOBS", "JOB"+i, 0).set(JOBLIMIT[i]);
}
String[] emptyset = {};
String[] adminlist = AdminList.toArray(new String[AdminList.size()]);
config.get("SIGJOBS", "ADMINS", emptyset).set(adminlist);
config.save();
}
}

Loading…
Cancel
Save