From 3ca11242a46864659a463d7c3e8447018e72f673 Mon Sep 17 00:00:00 2001 From: Chris Kearney <chris.kearney@urbanairship.com> Date: Tue, 26 Aug 2014 00:26:13 -0700 Subject: [PATCH] multiple item triggers --- .../com/comandante/creeper/Items/Item.java | 15 +++++++------ .../comandante/creeper/Items/ItemType.java | 22 ++++++++++--------- .../creeper/command/commands/DropCommand.java | 6 +++-- .../command/commands/PickUpCommand.java | 5 ++++- .../creeper/command/commands/TellCommand.java | 4 ++-- .../creeper/command/commands/UseCommand.java | 6 +++-- 6 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/comandante/creeper/Items/Item.java b/src/main/java/com/comandante/creeper/Items/Item.java index a3645280..0a0c3527 100644 --- a/src/main/java/com/comandante/creeper/Items/Item.java +++ b/src/main/java/com/comandante/creeper/Items/Item.java @@ -2,22 +2,23 @@ package com.comandante.creeper.Items; import java.io.Serializable; +import java.util.List; public class Item implements Serializable { private String itemName; private String itemDescription; - private String shortName; + private List<String> itemTriggers; private String restingName; private String itemId; private Integer itemTypeId; private int numberOfUses; private boolean isWithPlayer; - protected Item(String itemName, String itemDescription, String shortName, String restingName, String itemId, Integer itemTypeId, int numberOfUses, boolean isWithPlayer) { + protected Item(String itemName, String itemDescription, List<String> itemTriggers, String restingName, String itemId, Integer itemTypeId, int numberOfUses, boolean isWithPlayer) { this.itemName = itemName; this.itemDescription = itemDescription; - this.shortName = shortName; + this.itemTriggers = itemTriggers; this.restingName = restingName; this.itemId = itemId; this.itemTypeId = itemTypeId; @@ -64,8 +65,8 @@ public class Item implements Serializable { return itemDescription; } - public String getShortName() { - return shortName; + public List<String> getItemTriggers() { + return itemTriggers; } public void setItemName(String itemName) { @@ -76,8 +77,8 @@ public class Item implements Serializable { this.itemDescription = itemDescription; } - public void setShortName(String shortName) { - this.shortName = shortName; + public void setItemTriggers(List<String> itemTriggers) { + this.itemTriggers = itemTriggers; } public String getRestingName() { diff --git a/src/main/java/com/comandante/creeper/Items/ItemType.java b/src/main/java/com/comandante/creeper/Items/ItemType.java index 08be3210..e6b90813 100644 --- a/src/main/java/com/comandante/creeper/Items/ItemType.java +++ b/src/main/java/com/comandante/creeper/Items/ItemType.java @@ -1,26 +1,28 @@ package com.comandante.creeper.Items; +import java.util.Arrays; +import java.util.List; import java.util.UUID; import static com.comandante.creeper.model.Color.*; public enum ItemType { - UNKNOWN(0, "", "", "", "", false, 0, 0), - KEY(1, "key", + UNKNOWN(0, Arrays.asList(""), "", "", "", false, 0, 0), + KEY(1, Arrays.asList("key", "gold key", "shiny gold key"), YELLOW + "a shiny gold key" + RESET, YELLOW + "a shiny gold key" + RESET + " catches your eye.", "A basic key with nothing really remarkable other than its made of gold.", false, 0, 60), - BEER(2, "beer", + BEER(2, Arrays.asList("beer", "can of beer"), CYAN + "a dented can of beer" + RESET, CYAN + "a beer" + RESET + " lies on the ground, unopened", "This beer looks sketch but you'll probably drink it anyways.", false, 0, 60), - BOOK(3, "beer", + BOOK(3, Arrays.asList("book", "used book"), MAGENTA + "a leather book" + RESET, MAGENTA + "a well used book" + RESET + " with what looks like a leather back rests here.", "A book written in a foreign language. Doesn't matter as you can't read.", @@ -29,7 +31,7 @@ public enum ItemType { 60); private final Integer itemTypeCode; - private final String itemShortName; + private final List<String> itemTriggers; private final String restingName; private final String itemName; private final String itemDescription; @@ -37,9 +39,9 @@ public enum ItemType { private final int maxUses; private final int itemHalfLifeTicks; - ItemType(Integer itemTypeCode, String itemShortName, String itemName, String restingName, String itemDescription, boolean isDisposable, int maxUses, int itemHalfLifeTicks) { + ItemType(Integer itemTypeCode, List<String> itemTriggers, String itemName, String restingName, String itemDescription, boolean isDisposable, int maxUses, int itemHalfLifeTicks) { this.itemTypeCode = itemTypeCode; - this.itemShortName = itemShortName; + this.itemTriggers = itemTriggers; this.itemName = itemName; this.restingName = restingName; this.itemDescription = itemDescription; @@ -53,7 +55,7 @@ public enum ItemType { } public Item create() { - return new Item(getItemName(), getItemDescription(), getItemShortName(), getRestingName(), UUID.randomUUID().toString(), getItemTypeCode(), 0, false); + return new Item(getItemName(), getItemDescription(), getItemTriggers(), getRestingName(), UUID.randomUUID().toString(), getItemTypeCode(), 0, false); } @@ -65,8 +67,8 @@ public enum ItemType { return itemTypeCode; } - public String getItemShortName() { - return itemShortName; + public List<String> getItemTriggers() { + return itemTriggers; } public String getItemName() { diff --git a/src/main/java/com/comandante/creeper/command/commands/DropCommand.java b/src/main/java/com/comandante/creeper/command/commands/DropCommand.java index 9dc9a99d..dd2e6c8f 100644 --- a/src/main/java/com/comandante/creeper/command/commands/DropCommand.java +++ b/src/main/java/com/comandante/creeper/command/commands/DropCommand.java @@ -5,6 +5,7 @@ import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.model.PlayerMetadata; import com.comandante.creeper.model.Room; import com.comandante.creeper.server.CreeperSession; +import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import java.util.ArrayList; @@ -30,10 +31,11 @@ public class DropCommand extends Command { commandWrite("No item specified."); return; } - String itemTarget = originalMessageParts.get(1); + originalMessageParts.remove(0); + String itemTarget = Joiner.on(" ").join(originalMessageParts); for (String inventoryId : getGameManager().getPlayerManager().getPlayerMetadata(getPlayerId()).getInventory()) { Item itemEntity = getGameManager().getEntityManager().getItemEntity(inventoryId); - if (itemEntity.getShortName().equals(itemTarget)) { + if (itemEntity.getItemTriggers().contains(itemTarget)) { itemEntity.setWithPlayer(false); Room playerCurrentRoom = getGameManager().getRoomManager().getPlayerCurrentRoom(getGameManager().getPlayerManager().getPlayer(getPlayerId())).get(); getGameManager().placeItemInRoom(playerCurrentRoom.getRoomId(), itemEntity.getItemId()); diff --git a/src/main/java/com/comandante/creeper/command/commands/PickUpCommand.java b/src/main/java/com/comandante/creeper/command/commands/PickUpCommand.java index c3a7c153..6271a7d3 100644 --- a/src/main/java/com/comandante/creeper/command/commands/PickUpCommand.java +++ b/src/main/java/com/comandante/creeper/command/commands/PickUpCommand.java @@ -4,6 +4,7 @@ import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.Items.Item; import com.comandante.creeper.model.Player; import com.comandante.creeper.model.Room; +import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import java.util.ArrayList; @@ -31,7 +32,9 @@ public class PickUpCommand extends Command { Set<String> itemIds = playerCurrentRoom.getItemIds(); for (String next : itemIds) { Item itemEntity = getGameManager().getEntityManager().getItemEntity(next); - if (itemEntity.getShortName().equalsIgnoreCase(originalMessageParts.get(1))) { + originalMessageParts.remove(0); + String desiredPickUpItem = Joiner.on(" ").join(originalMessageParts); + if (itemEntity.getItemTriggers().contains(desiredPickUpItem)) { getGameManager().acquireItem(player, itemEntity.getItemId()); roomSay(playerCurrentRoom.getRoomId(), getGameManager().getPlayerManager().getPlayer(getPlayerId()).getPlayerName() + " picked up " + itemEntity.getItemName()); return; diff --git a/src/main/java/com/comandante/creeper/command/commands/TellCommand.java b/src/main/java/com/comandante/creeper/command/commands/TellCommand.java index d41d05d8..eb0c2c2a 100644 --- a/src/main/java/com/comandante/creeper/command/commands/TellCommand.java +++ b/src/main/java/com/comandante/creeper/command/commands/TellCommand.java @@ -2,8 +2,8 @@ package com.comandante.creeper.command.commands; import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.model.Player; +import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; -import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.Arrays; @@ -44,7 +44,7 @@ public class TellCommand extends Command { return; } parts.remove(0); - String tellMessage = StringUtils.join(parts, " "); + String tellMessage = Joiner.on(" ").join(parts); StringBuilder stringBuilder = new StringBuilder(); String destinationPlayercolor = YELLOW; stringBuilder.append("*").append(sourcePlayer.getPlayerName()).append("* "); diff --git a/src/main/java/com/comandante/creeper/command/commands/UseCommand.java b/src/main/java/com/comandante/creeper/command/commands/UseCommand.java index fcd78219..06b85b20 100644 --- a/src/main/java/com/comandante/creeper/command/commands/UseCommand.java +++ b/src/main/java/com/comandante/creeper/command/commands/UseCommand.java @@ -6,6 +6,7 @@ import com.comandante.creeper.Items.ItemType; import com.comandante.creeper.Items.ItemUseHandler; import com.comandante.creeper.managers.GameManager; import com.comandante.creeper.server.CreeperSession; +import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import java.util.ArrayList; @@ -31,10 +32,11 @@ public class UseCommand extends Command { commandWrite("No item specified."); return; } - String itemTarget = originalMessageParts.get(1); + originalMessageParts.remove(0); + String itemTarget = Joiner.on(" ").join(originalMessageParts); for (String inventoryId : getGameManager().getPlayerManager().getPlayerMetadata(getPlayerId()).getInventory()) { Item itemEntity = getGameManager().getEntityManager().getItemEntity(inventoryId); - if (itemEntity.getShortName().equals(itemTarget)) { + if (itemEntity.getItemTriggers().contains(itemTarget)) { new ItemUseHandler(itemEntity, creeperSession, getGameManager(), getPlayerId()).handle(); return; } -- GitLab