From d8e34953c04fcbc3f3868c272b595badfbee1bf8 Mon Sep 17 00:00:00 2001 From: Mads Boddum Date: Thu, 31 Mar 2016 17:38:29 +0200 Subject: [PATCH] Removed the old Buff class and refactored existing code to use the new one --- src/resources/Buff.java | 71 ------------------- .../objects/creature/CreatureObject.java | 11 ++- src/services/commands/BuffService.java | 38 +++++----- 3 files changed, 22 insertions(+), 98 deletions(-) delete mode 100644 src/resources/Buff.java diff --git a/src/resources/Buff.java b/src/resources/Buff.java deleted file mode 100644 index 90b1a4439..000000000 --- a/src/resources/Buff.java +++ /dev/null @@ -1,71 +0,0 @@ -package resources; - -import java.io.Serializable; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -import resources.encodables.Encodable; - -public class Buff implements Encodable, Serializable { - - private static final long serialVersionUID = 1; - - private int duration; - private long bufferId; - private int endTime; - private long stackCount; - private float skillMod1Value; - - public Buff(long bufferId, int playTime, int duration, float skillMod1Value) { - this.bufferId = bufferId; - this.duration = duration; - this.skillMod1Value = skillMod1Value; - endTime = duration + playTime; - stackCount = 1; - } - - @Override - public byte[] encode() { - ByteBuffer data = ByteBuffer.allocate(Integer.BYTES * 2 + Float.BYTES + Long.BYTES * 2 + Short.BYTES ).order(ByteOrder.LITTLE_ENDIAN); - - data.putInt(endTime); // Buff duration + time played on character - data.putFloat(skillMod1Value); // The value for skillMod #1 on the buff. Displayed on the client as skillMod1Value * stackCount. - data.putInt(duration); // Icon shadow "clock" overlay. - data.putLong(bufferId); // Object ID of the buffer - data.putLong(stackCount); // Stack count - data.putShort((short) 0); // Unknown. Could also be two bytes. - - return data.array(); - } - - @Override - public void decode(ByteBuffer data) { - endTime = data.getInt(); - skillMod1Value = data.getFloat(); - duration = data.getInt(); - bufferId = data.getLong(); - stackCount = data.getLong(); - data.getShort(); // unknown - } - - public int getDuration() { - return duration; - } - - public long getStackCount() { - return stackCount; - } - - /** - * Adjusts the stack count. This method doesn't send any delta. - * @param adjustment - */ - public void adjustStackCount(int adjustment) { - stackCount += adjustment; - } - - public int getEndTime() { - return endTime; - } - -} diff --git a/src/resources/objects/creature/CreatureObject.java b/src/resources/objects/creature/CreatureObject.java index 46e105e58..f76e3ff41 100644 --- a/src/resources/objects/creature/CreatureObject.java +++ b/src/resources/objects/creature/CreatureObject.java @@ -39,7 +39,6 @@ import network.packets.swg.zone.UpdatePostureMessage; import network.packets.swg.zone.UpdatePvpStatusMessage; import network.packets.swg.zone.baselines.Baseline.BaselineType; import network.packets.swg.zone.object_controller.PostureUpdate; -import resources.Buff; import resources.HologramColour; import resources.Posture; import resources.PvpFlag; @@ -667,7 +666,7 @@ public class CreatureObject extends TangibleObject { return skillMod != null ? skillMod.getValue() : 0; } - public void addBuff(int buffCrc, Buff buff) { + public void addBuff(CRC buffCrc, Buff buff) { if(!buffs.containsKey(buffCrc)) { buffs.put(buffCrc, buff); buffs.sendDeltaMessage(this); @@ -675,7 +674,7 @@ public class CreatureObject extends TangibleObject { } } - public void removeBuff(int buffCrc) { + public void removeBuff(CRC buffCrc) { // If a value was associated with the key, then send a delta. if(buffs.containsKey(buffCrc)) { buffs.remove(buffCrc); @@ -684,11 +683,11 @@ public class CreatureObject extends TangibleObject { } } - public Buff getBuffByCrc(int buffCrc) { + public Buff getBuffByCrc(CRC buffCrc) { return buffs.get(buffCrc); } - public void adjustBuffStackCount(int buffCrc, int adjustment) { + public void adjustBuffStackCount(CRC buffCrc, int adjustment) { Buff buff = buffs.get(buffCrc); buff.adjustStackCount(adjustment); // Adjust the stack count // TODO reset time remaining? @@ -701,7 +700,7 @@ public class CreatureObject extends TangibleObject { * {@code Buff} values in the belief that deltas will be sent because * they won't - this is incorrect usage. */ - public Map getBuffs() { + public Map getBuffs() { return new HashMap<>(buffs); } diff --git a/src/services/commands/BuffService.java b/src/services/commands/BuffService.java index 3585f51a3..2cfba7f50 100644 --- a/src/services/commands/BuffService.java +++ b/src/services/commands/BuffService.java @@ -39,12 +39,12 @@ import intents.BuffIntent; import intents.PlayerEventIntent; import intents.SkillModIntent; import network.packets.swg.zone.spatial.PlayClientEffectObjectMessage; -import resources.Buff; import resources.client_info.ClientFactory; import resources.client_info.visitors.DatatableData; import resources.common.CRC; import resources.control.Intent; import resources.control.Service; +import resources.objects.creature.Buff; import resources.objects.creature.CreatureObject; import resources.objects.player.PlayerObject; @@ -66,15 +66,13 @@ public class BuffService extends Service { // TODO test buff stacks - // TODO cache conversions from buffName to buffCrc in buffNameToCrc() - // private static final byte GROUP_BUFF_RANGE = 100; private DatatableData buffTable; private final DelayQueue buffRemoval; private final ExecutorService executor; private boolean stopBuffRemover; - private final Map dataMap; + private final Map dataMap; public BuffService() { registerForIntent(BuffIntent.TYPE); @@ -122,7 +120,7 @@ public class BuffService extends Service { buffTable = (DatatableData) ClientFactory.getInfoFromFile("datatables/buff/buff.iff"); for(int row = 0; row < buffTable.getRowCount(); row++) { - dataMap.put(buffNameToCrc((String) buffTable.getCell(row, 0)), new BuffData( + dataMap.put(new CRC((String) buffTable.getCell(row, 0)), new BuffData( (int) buffTable.getCell(row, 28), // max stacks (String) buffTable.getCell(row, 7), // effect1 (float) buffTable.getCell(row, 8), // value1 @@ -152,28 +150,26 @@ public class BuffService extends Service { } private void handleFirstZone(CreatureObject creature) { - Map buffs = creature.getBuffs(); + Map buffs = creature.getBuffs(); buffs.forEach((crc, buff) -> manageBuff(buff, crc, creature)); } - private int buffNameToCrc(String buffName) { - return CRC.getCrc(buffName); - } - private void handleBuffIntentAdd(BuffIntent bi) { - addBuff(buffNameToCrc(bi.getBuffName()), bi.getReceiver(), bi.getBuffer()); + addBuff(new CRC(bi.getBuffName()), bi.getReceiver(), bi.getBuffer()); } - private void addBuff(int buffCrc, CreatureObject receiver, CreatureObject buffer) { + private void addBuff(CRC buffCrc, CreatureObject receiver, CreatureObject buffer) { BuffData buffData = dataMap.get(buffCrc); if(buffData == null) return; + // TODO stack counts upon add/remove probably need to be defined on a per-buff basis due to skillmod influence. + int stackCount = 1; + int buffDuration = (int) buffData.getDefaultDuration(); + Buff buff = new Buff(receiver.getPlayerObject().getPlayTime() + buffDuration, buffData.getEffect1Value(), buffDuration, buffer.getObjectId(), stackCount); - Buff buff = new Buff(buffer.getObjectId(), receiver.getPlayerObject().getPlayTime(), (int) buffData.getDefaultDuration(), buffData.getEffect1Value()); - - sendSkillModIntent(buffData, receiver, stopBuffRemover); + sendSkillModIntent(buffData, receiver, false); receiver.addBuff(buffCrc, buff); manageBuff(buff, buffCrc, receiver); @@ -185,10 +181,10 @@ public class BuffService extends Service { } private void handleBuffIntentRemove(BuffIntent bi) { - removeBuff(bi.getReceiver(), buffNameToCrc(bi.getBuffName()), false); + removeBuff(bi.getReceiver(), new CRC(bi.getBuffName()), false); } - private void manageBuff(Buff buff, int buffCrc, CreatureObject creature) { + private void manageBuff(Buff buff, CRC buffCrc, CreatureObject creature) { // If this buff has less than or 0 seconds left, then remove it. if(buff.getEndTime() <= 0) { removeBuff(creature, buffCrc, true); @@ -198,7 +194,7 @@ public class BuffService extends Service { } } - private void removeBuff(CreatureObject creature, int buffCrc, boolean expired) { + private void removeBuff(CreatureObject creature, CRC buffCrc, boolean expired) { // Get the BuffData for this buff name. BuffData buffData = dataMap.get(buffCrc); @@ -229,7 +225,7 @@ public class BuffService extends Service { if(callback.isEmpty()) return; - int callbackCrc = buffNameToCrc(callback); + CRC callbackCrc = new CRC(callback); if(dataMap.containsKey(callbackCrc)) { // Apply the callback buff addBuff(callbackCrc, creature, creature); @@ -280,11 +276,11 @@ public class BuffService extends Service { private class BuffDelayed implements Delayed { private final Buff buff; - private final int buffCrc; + private final CRC buffCrc; private final PlayerObject owner; private final CreatureObject creature; - private BuffDelayed(Buff buff, int buffCrc, CreatureObject creature) { + private BuffDelayed(Buff buff, CRC buffCrc, CreatureObject creature) { this.buff = buff; this.buffCrc = buffCrc; this.creature = creature;