From ef90814e8e17165c3b64f267fc7ea5e97cbed273 Mon Sep 17 00:00:00 2001 From: Chris Kearney <chris@kearneymail.com> Date: Sun, 14 Aug 2016 14:34:58 -0700 Subject: [PATCH] fixing the learn spell twice problem, made itemuse actions threadsafe, because now they share some state --- .../creeper/Items/ItemUseAction.java | 8 +- .../creeper/Items/ItemUseHandler.java | 37 +++++++-- .../creeper/Items/ItemUseRegistry.java | 79 ------------------- .../Items/use/DefaultApplyStatsAction.java | 37 +++++++-- .../creeper/Items/use/DirtyBombUseAction.java | 6 +- .../use/LightningSpellBookUseAction.java | 21 ++--- .../Items/use/ResetAllEffectsUseAction.java | 4 +- .../java/com/comandante/creeper/Main.java | 4 - .../command/admin/GiveGoldCommand.java | 3 +- .../creeper/player/NpcTestHarness.java | 1 - 10 files changed, 83 insertions(+), 117 deletions(-) delete mode 100644 src/main/java/com/comandante/creeper/Items/ItemUseRegistry.java diff --git a/src/main/java/com/comandante/creeper/Items/ItemUseAction.java b/src/main/java/com/comandante/creeper/Items/ItemUseAction.java index ac65a53e..e30cb325 100644 --- a/src/main/java/com/comandante/creeper/Items/ItemUseAction.java +++ b/src/main/java/com/comandante/creeper/Items/ItemUseAction.java @@ -7,12 +7,12 @@ import com.comandante.creeper.spells.Effect; import java.util.Set; public interface ItemUseAction { - public Integer getItemTypeId(); + Integer getItemTypeId(); - public void executeAction(GameManager gameManager, Player player, Item item); + void executeAction(GameManager gameManager, Player player, Item item); - public void postExecuteAction(GameManager gameManager, Player player, Item item); + void postExecuteAction(GameManager gameManager, Player player, Item item); - public Set<Effect> getEffects(); + Set<Effect> getEffects(); } diff --git a/src/main/java/com/comandante/creeper/Items/ItemUseHandler.java b/src/main/java/com/comandante/creeper/Items/ItemUseHandler.java index 83df53a4..21a1b3ab 100644 --- a/src/main/java/com/comandante/creeper/Items/ItemUseHandler.java +++ b/src/main/java/com/comandante/creeper/Items/ItemUseHandler.java @@ -1,14 +1,15 @@ package com.comandante.creeper.Items; +import com.comandante.creeper.Items.use.DefaultApplyStatsAction; +import com.comandante.creeper.Items.use.LightningSpellBookUseAction; import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.player.Player; -import com.comandante.creeper.server.CreeperSession; -import com.comandante.creeper.spells.Effect; +import com.comandante.creeper.stat.Stats; +import com.comandante.creeper.stat.StatsBuilder; +import com.google.common.collect.Sets; import org.apache.log4j.Logger; -import java.util.Set; - public class ItemUseHandler { private static final Logger log = Logger.getLogger(ItemUseHandler.class); @@ -19,10 +20,36 @@ public class ItemUseHandler { } public void handle(Player player, Item item) { - ItemUseAction itemUseAction = ItemUseRegistry.getItemUseAction(item.getItemTypeId()); + ItemUseAction itemUseAction = null; + switch (ItemType.itemTypeFromCode(item.getItemTypeId())) { + case LIGHTNING_SPELLBOOKNG: + itemUseAction = new LightningSpellBookUseAction(ItemType.LIGHTNING_SPELLBOOKNG); + break; + case PURPLE_DRANK: + itemUseAction = new DefaultApplyStatsAction(ItemType.PURPLE_DRANK, buildStats(500, 0), Sets.newHashSet()); + break; + case MARIJUANA: + itemUseAction = new DefaultApplyStatsAction(ItemType.MARIJUANA, buildStats(500, 500), Sets.newHashSet()); + break; + case SMALL_HEALTH_POTION: + itemUseAction = new DefaultApplyStatsAction(ItemType.SMALL_HEALTH_POTION, buildStats(100, 0), Sets.newHashSet()); + break; + } if (itemUseAction != null) { itemUseAction.executeAction(gameManager, player, item); itemUseAction.postExecuteAction(gameManager, player, item); } } + + private static Stats buildStats(int health, int mana) { + StatsBuilder statsBuilder = new StatsBuilder(); + statsBuilder.setCurrentHealth(health); + statsBuilder.setCurrentMana(mana); + return statsBuilder.createStats(); + } + + public static void incrementUses(Item item) { + item.setNumberOfUses(item.getNumberOfUses() + 1); + } } + diff --git a/src/main/java/com/comandante/creeper/Items/ItemUseRegistry.java b/src/main/java/com/comandante/creeper/Items/ItemUseRegistry.java deleted file mode 100644 index fe76f1bd..00000000 --- a/src/main/java/com/comandante/creeper/Items/ItemUseRegistry.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.comandante.creeper.Items; - -import com.comandante.creeper.Items.use.DefaultApplyStatsAction; -import com.comandante.creeper.Items.use.LightningSpellBookUseAction; -import com.comandante.creeper.managers.GameManager; -import com.comandante.creeper.player.Player; -import com.comandante.creeper.spells.Effect; -import com.comandante.creeper.stat.Stats; -import com.comandante.creeper.stat.StatsBuilder; -import com.google.api.client.util.Maps; -import com.google.api.client.util.Sets; -import org.apache.log4j.Logger; - -import java.util.Map; -import java.util.Set; - -public class ItemUseRegistry { - - private static final Map<Integer, ItemUseAction> itemUseActionMap = Maps.newHashMap(); - private static final Logger log = Logger.getLogger(ItemUseRegistry.class); - - - public static void addItemUseAction(ItemUseAction itemUseAction) { - itemUseActionMap.put(itemUseAction.getItemTypeId(), itemUseAction); - } - - public static ItemUseAction getItemUseAction(Integer id) { - return itemUseActionMap.get(id); - } - - public static void configure() { - //Beer - addItemUseAction(new DefaultApplyStatsAction(ItemType.SMALL_HEALTH_POTION, buildStats(100, 0), Sets.<Effect>newHashSet())); - - //Purple Drank - addItemUseAction(new DefaultApplyStatsAction(ItemType.PURPLE_DRANK, buildStats(500, 0), Sets.<Effect>newHashSet())); - - //Marijuana - addItemUseAction(new DefaultApplyStatsAction(ItemType.MARIJUANA, buildStats(500,500), Sets.<Effect>newHashSet())); - - //Lightning Spellbook - addItemUseAction(new LightningSpellBookUseAction(ItemType.LIGHTNING_SPELLBOOKNG)); - } - - private static Stats buildStats(int health, int mana) { - StatsBuilder statsBuilder = new StatsBuilder(); - statsBuilder.setCurrentHealth(health); - statsBuilder.setCurrentMana(mana); - return statsBuilder.createStats(); - } - - public static void processEffects(GameManager gameManager, Player player, Set<Effect> effects) { - if (effects == null) { - return; - } - for (Effect effect : effects) { - Effect nEffect = new Effect(effect); - nEffect.setPlayerId(player.getPlayerId()); - gameManager.getEntityManager().saveEffect(nEffect); - boolean effectResult = player.addEffect(nEffect.getEntityId()); - if (effect.getDurationStats() != null) { - if (effect.getDurationStats().getCurrentHealth() < 0) { - log.error("ERROR! Someone added an effect with a health modifier which won't work for various reasons."); - continue; - } - } - if (effectResult) { - gameManager.getChannelUtils().write(player.getPlayerId(), "You feel " + effect.getEffectName() + "\r\n"); - } else { - gameManager.getChannelUtils().write(player.getPlayerId(), "Unable to apply effect.\r\n"); - } - } - } - - public static void incrementUses(Item item) { - item.setNumberOfUses(item.getNumberOfUses() + 1); - } -} - diff --git a/src/main/java/com/comandante/creeper/Items/use/DefaultApplyStatsAction.java b/src/main/java/com/comandante/creeper/Items/use/DefaultApplyStatsAction.java index 9aff2d23..c92eddea 100644 --- a/src/main/java/com/comandante/creeper/Items/use/DefaultApplyStatsAction.java +++ b/src/main/java/com/comandante/creeper/Items/use/DefaultApplyStatsAction.java @@ -1,13 +1,14 @@ package com.comandante.creeper.Items.use; -import com.codahale.metrics.MetricRegistry; -import com.comandante.creeper.Items.*; -import com.comandante.creeper.Main; +import com.comandante.creeper.Items.Item; +import com.comandante.creeper.Items.ItemType; +import com.comandante.creeper.Items.ItemUseAction; +import com.comandante.creeper.Items.ItemUseHandler; import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.player.Player; -import com.comandante.creeper.server.Color; import com.comandante.creeper.spells.Effect; import com.comandante.creeper.stat.Stats; +import org.apache.log4j.Logger; import java.util.Set; @@ -16,6 +17,7 @@ public class DefaultApplyStatsAction implements ItemUseAction { private final Integer itemTypeId; private final Stats stats; private final Set<Effect> effectSet; + private static final Logger log = Logger.getLogger(DefaultApplyStatsAction.class); public DefaultApplyStatsAction(ItemType itemType, Stats stats, Set<Effect> effects) { this.itemTypeId = itemType.getItemTypeCode(); @@ -41,12 +43,12 @@ public class DefaultApplyStatsAction implements ItemUseAction { } player.addMana(stats.getCurrentMana()); player.updatePlayerHealth(stats.getCurrentHealth(), null); - ItemUseRegistry.processEffects(gameManager, player, effectSet); + processEffects(gameManager, player, effectSet); } @Override public void postExecuteAction(GameManager gameManager, Player player, Item item) { - ItemUseRegistry.incrementUses(item); + ItemUseHandler.incrementUses(item); if (ItemType.itemTypeFromCode(item.getItemTypeId()).isDisposable()) { if (item.getNumberOfUses() < ItemType.itemTypeFromCode(item.getItemTypeId()).getMaxUses()) { gameManager.getEntityManager().saveItem(item); @@ -61,4 +63,27 @@ public class DefaultApplyStatsAction implements ItemUseAction { public Set<Effect> getEffects() { return effectSet; } + + public static void processEffects(GameManager gameManager, Player player, Set<Effect> effects) { + if (effects == null) { + return; + } + for (Effect effect : effects) { + Effect nEffect = new Effect(effect); + nEffect.setPlayerId(player.getPlayerId()); + gameManager.getEntityManager().saveEffect(nEffect); + boolean effectResult = player.addEffect(nEffect.getEntityId()); + if (effect.getDurationStats() != null) { + if (effect.getDurationStats().getCurrentHealth() < 0) { + log.error("ERROR! Someone added an effect with a health modifier which won't work for various reasons."); + continue; + } + } + if (effectResult) { + gameManager.getChannelUtils().write(player.getPlayerId(), "You feel " + effect.getEffectName() + "\r\n"); + } else { + gameManager.getChannelUtils().write(player.getPlayerId(), "Unable to apply effect.\r\n"); + } + } + } } diff --git a/src/main/java/com/comandante/creeper/Items/use/DirtyBombUseAction.java b/src/main/java/com/comandante/creeper/Items/use/DirtyBombUseAction.java index 373dc892..e291a979 100644 --- a/src/main/java/com/comandante/creeper/Items/use/DirtyBombUseAction.java +++ b/src/main/java/com/comandante/creeper/Items/use/DirtyBombUseAction.java @@ -3,11 +3,10 @@ package com.comandante.creeper.Items.use; import com.comandante.creeper.Items.Item; import com.comandante.creeper.Items.ItemType; import com.comandante.creeper.Items.ItemUseAction; -import com.comandante.creeper.Items.ItemUseRegistry; +import com.comandante.creeper.Items.ItemUseHandler; import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.npc.Npc; import com.comandante.creeper.npc.NpcStatsChangeBuilder; -import com.comandante.creeper.player.EquipmentSlotType; import com.comandante.creeper.player.Player; import com.comandante.creeper.server.Color; import com.comandante.creeper.spells.Effect; @@ -17,7 +16,6 @@ import com.comandante.creeper.world.Room; import java.text.NumberFormat; import java.util.Arrays; import java.util.Locale; -import java.util.Objects; import java.util.Set; public class DirtyBombUseAction implements ItemUseAction { @@ -65,7 +63,7 @@ public class DirtyBombUseAction implements ItemUseAction { @Override public void postExecuteAction(GameManager gameManager, Player player, Item item) { - ItemUseRegistry.incrementUses(item); + ItemUseHandler.incrementUses(item); if (ItemType.itemTypeFromCode(item.getItemTypeId()).isDisposable()) { if (item.getNumberOfUses() < ItemType.itemTypeFromCode(item.getItemTypeId()).getMaxUses()) { gameManager.getEntityManager().saveItem(item); diff --git a/src/main/java/com/comandante/creeper/Items/use/LightningSpellBookUseAction.java b/src/main/java/com/comandante/creeper/Items/use/LightningSpellBookUseAction.java index 4d3c205c..bf40f416 100644 --- a/src/main/java/com/comandante/creeper/Items/use/LightningSpellBookUseAction.java +++ b/src/main/java/com/comandante/creeper/Items/use/LightningSpellBookUseAction.java @@ -4,18 +4,10 @@ import com.comandante.creeper.Items.Item; import com.comandante.creeper.Items.ItemType; import com.comandante.creeper.Items.ItemUseAction; import com.comandante.creeper.managers.GameManager; -import com.comandante.creeper.npc.Npc; -import com.comandante.creeper.npc.NpcStatsChangeBuilder; import com.comandante.creeper.player.Player; -import com.comandante.creeper.server.Color; import com.comandante.creeper.spells.Effect; import com.comandante.creeper.spells.LightningSpell; -import com.comandante.creeper.stat.StatsBuilder; -import com.comandante.creeper.world.Room; -import java.text.NumberFormat; -import java.util.Arrays; -import java.util.Locale; import java.util.Set; public class LightningSpellBookUseAction implements ItemUseAction { @@ -26,6 +18,8 @@ public class LightningSpellBookUseAction implements ItemUseAction { this.itemType = itemType; } + private Boolean dontDelete = Boolean.FALSE; + @Override public Integer getItemTypeId() { return itemType.getItemTypeCode(); @@ -33,14 +27,21 @@ public class LightningSpellBookUseAction implements ItemUseAction { @Override public void executeAction(GameManager gameManager, Player player, Item item) { + if (player.getLearnedSpells().contains(LightningSpell.NAME)) { + gameManager.getChannelUtils().write(player.getPlayerId(), "You already know how to use " + LightningSpell.NAME); + dontDelete = true; + return; + } gameManager.writeToPlayerCurrentRoom(player.getPlayerId(), player.getPlayerName() + " reads a leatherbound aging spell book and gains knowledge about lightning spells."); player.addLearnedSpellByName(LightningSpell.NAME); } @Override public void postExecuteAction(GameManager gameManager, Player player, Item item) { - player.removeInventoryId(item.getItemId()); - gameManager.getEntityManager().removeItem(item); + if (!dontDelete) { + player.removeInventoryId(item.getItemId()); + gameManager.getEntityManager().removeItem(item); + } } @Override diff --git a/src/main/java/com/comandante/creeper/Items/use/ResetAllEffectsUseAction.java b/src/main/java/com/comandante/creeper/Items/use/ResetAllEffectsUseAction.java index eb610a27..bae77982 100644 --- a/src/main/java/com/comandante/creeper/Items/use/ResetAllEffectsUseAction.java +++ b/src/main/java/com/comandante/creeper/Items/use/ResetAllEffectsUseAction.java @@ -3,7 +3,7 @@ package com.comandante.creeper.Items.use; import com.comandante.creeper.Items.Item; import com.comandante.creeper.Items.ItemType; import com.comandante.creeper.Items.ItemUseAction; -import com.comandante.creeper.Items.ItemUseRegistry; +import com.comandante.creeper.Items.ItemUseHandler; import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.player.Player; import com.comandante.creeper.spells.Effect; @@ -32,7 +32,7 @@ public class ResetAllEffectsUseAction implements ItemUseAction { @Override public void postExecuteAction(GameManager gameManager, Player player, Item item) { - ItemUseRegistry.incrementUses(item); + ItemUseHandler.incrementUses(item); if (ItemType.itemTypeFromCode(item.getItemTypeId()).isDisposable()) { if (item.getNumberOfUses() < ItemType.itemTypeFromCode(item.getItemTypeId()).getMaxUses()) { gameManager.getEntityManager().saveItem(item); diff --git a/src/main/java/com/comandante/creeper/Main.java b/src/main/java/com/comandante/creeper/Main.java index 213d0757..5f720a60 100644 --- a/src/main/java/com/comandante/creeper/Main.java +++ b/src/main/java/com/comandante/creeper/Main.java @@ -5,7 +5,6 @@ import com.codahale.metrics.MetricFilter; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.graphite.GraphiteReporter; import com.codahale.metrics.graphite.PickledGraphite; -import com.comandante.creeper.Items.ItemUseRegistry; import com.comandante.creeper.entity.EntityManager; import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.managers.SessionManager; @@ -115,9 +114,6 @@ public class Main { startUpMessage("Generating map data."); mapsManager.generateAllMaps(); - startUpMessage("Configuring Item Use Registry"); - ItemUseRegistry.configure(); - startUpMessage("Configuring default inventorySize limits"); BackportCommands.configureDefaultInventorySize(entityManager, gameManager); diff --git a/src/main/java/com/comandante/creeper/command/admin/GiveGoldCommand.java b/src/main/java/com/comandante/creeper/command/admin/GiveGoldCommand.java index 0eeae1d8..e054aca1 100644 --- a/src/main/java/com/comandante/creeper/command/admin/GiveGoldCommand.java +++ b/src/main/java/com/comandante/creeper/command/admin/GiveGoldCommand.java @@ -1,7 +1,6 @@ package com.comandante.creeper.command.admin; import com.comandante.creeper.command.Command; -import com.comandante.creeper.command.CommandRunnable; import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.player.Player; import com.comandante.creeper.player.PlayerRole; @@ -19,7 +18,7 @@ public class GiveGoldCommand extends Command { final static List<String> validTriggers = Arrays.asList("givegold"); final static String description = "Give Gold to a Player"; final static String correctUsage = "givegold <player name> <amt>"; - final static Set<PlayerRole> roles = Sets.newHashSet(PlayerRole.ADMIN); + final static Set<PlayerRole> roles = Sets.newHashSet(); public GiveGoldCommand(GameManager gameManager) { diff --git a/src/test/com/comandante/creeper/player/NpcTestHarness.java b/src/test/com/comandante/creeper/player/NpcTestHarness.java index 6594b564..b48a6529 100644 --- a/src/test/com/comandante/creeper/player/NpcTestHarness.java +++ b/src/test/com/comandante/creeper/player/NpcTestHarness.java @@ -4,7 +4,6 @@ import com.comandante.creeper.ConfigureCommands; import com.comandante.creeper.CreeperConfiguration; import com.comandante.creeper.Items.Item; import com.comandante.creeper.Items.ItemType; -import com.comandante.creeper.Items.ItemUseRegistry; import com.comandante.creeper.Main; import com.comandante.creeper.entity.EntityManager; import com.comandante.creeper.managers.GameManager; -- GitLab