Recoded reward chests / item rewards; resolves #113

This commit is contained in:
Daniel Saukel 2016-07-16 03:19:44 +02:00
parent d0c725d2d7
commit be7681480d
17 changed files with 307 additions and 135 deletions

View File

@ -8,6 +8,6 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.1</version>
<version>0.14.2</version>
</parent>
</project>

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.1</version>
<version>0.14.2</version>
</parent>
<build>
<resources>

View File

@ -137,6 +137,7 @@ public enum DMessages implements Messages {
LOG_ERROR_MOB_ENCHANTMENT("Log_Error_MobEnchantment", "&4Error at loading mob.yml: Enchantment &6&v1&4 doesn't exist!"),
LOG_ERROR_MOBTYPE("Log_Error_MobType", "&4Error at loading mob.yml: Mob &6&v1&4 doesn't exist!"),
LOG_ERROR_NO_CONSOLE_COMMAND("Log_Error_NoConsoleCommand", "&6/dxl &v1&4 can not be executed as console!"),
LOG_ERROR_SIGN_SETUP("Log_Error_SignSetup", "&4A sign at &6&v1&4 is erroneous!"),
LOG_GENERATE_NEW_WORLD("Log_GenerateNewWorld", "&6Generating new world..."),
LOG_IMPORT_WORLD("Log_ImportWorld", "&6Importing world..."),
LOG_NEW_MAP("Log_NewDungeon", "&6Creating new map."),

View File

@ -51,6 +51,7 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -72,6 +73,7 @@ import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
@ -651,7 +653,28 @@ public class PlayerListener implements Listener {
return;
}
RewardChest.onOpenInventory(event);
InventoryView inventory = event.getView();
DGameWorld gameWorld = DGameWorld.getByWorld(event.getPlayer().getWorld());
if (gameWorld == null) {
return;
}
if (!(inventory.getTopInventory().getHolder() instanceof Chest)) {
return;
}
Chest chest = (Chest) inventory.getTopInventory().getHolder();
for (RewardChest rewardChest : gameWorld.getRewardChests()) {
if (!rewardChest.getChest().equals(chest)) {
continue;
}
rewardChest.onOpen((Player) event.getPlayer());
event.setCancelled(true);
}
if (!plugin.getMainConfig().getOpenInventories() && !DPermissions.hasPermission(event.getPlayer(), DPermissions.INSECURE)) {
World world = event.getPlayer().getWorld();

View File

@ -33,9 +33,7 @@ import io.github.dre2n.dungeonsxl.game.GameRules;
import io.github.dre2n.dungeonsxl.game.GameType;
import io.github.dre2n.dungeonsxl.game.GameTypeDefault;
import io.github.dre2n.dungeonsxl.mob.DMob;
import static io.github.dre2n.dungeonsxl.player.DGlobalPlayer.plugin;
import io.github.dre2n.dungeonsxl.requirement.Requirement;
import io.github.dre2n.dungeonsxl.reward.DLootInventory;
import io.github.dre2n.dungeonsxl.reward.Reward;
import io.github.dre2n.dungeonsxl.task.CreateDInstancePlayerTask;
import io.github.dre2n.dungeonsxl.trigger.DistanceTrigger;
@ -53,10 +51,8 @@ import org.bukkit.entity.Damageable;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.scheduler.BukkitRunnable;
/**
* Represents a player in a DGameWorld.
@ -75,8 +71,6 @@ public class DGamePlayer extends DInstancePlayer {
private int wolfRespawnTime = 30;
private long offlineTime;
private Inventory treasureInv = plugin.getServer().createInventory(getPlayer(), 45, DMessages.PLAYER_TREASURES.getMessage());
private int initialLives = -1;
private int lives;
@ -341,21 +335,6 @@ public class DGamePlayer extends DInstancePlayer {
this.offlineTime = offlineTime;
}
/**
* @return the treasureInv
*/
public Inventory getTreasureInv() {
return treasureInv;
}
/**
* @param treasureInv
* the treasureInv to set
*/
public void setTreasureInv(Inventory treasureInv) {
this.treasureInv = treasureInv;
}
/**
* @return the initialLives
*/
@ -428,8 +407,6 @@ public class DGamePlayer extends DInstancePlayer {
reward.giveTo(getPlayer());
}
addTreasure();
getData().logTimeLastPlayed(dGroup.getDungeon().getName());
// Tutorial Permissions
@ -846,10 +823,6 @@ public class DGamePlayer extends DInstancePlayer {
}
}
public void addTreasure() {
new DLootInventory(getPlayer(), treasureInv.getContents());
}
@Override
public void update(boolean updateSecond) {
boolean locationValid = true;

View File

@ -36,8 +36,6 @@ public class DLootInventory {
private InventoryView inventoryView;
private Player player;
private long time;
public DLootInventory(Player player, ItemStack[] itemStacks) {
plugin.getDLootInventories().add(this);
@ -50,6 +48,7 @@ public class DLootInventory {
this.player = player;
}
/* Getters and setters */
/**
* @return the inventory
*/
@ -95,6 +94,8 @@ public class DLootInventory {
this.player = player;
}
private long time;
/**
* @return the time
*/
@ -110,7 +111,7 @@ public class DLootInventory {
this.time = time;
}
// Static
/* Statics */
/**
* @param player
* the player whose DLootIntentory will be returned

View File

@ -0,0 +1,77 @@
/*
* Copyright (C) 2012-2016 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.dre2n.dungeonsxl.reward;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* @author Daniel Saukel
*/
public class ItemReward extends Reward {
private RewardType type = RewardTypeDefault.ITEM;
private List<ItemStack> items = new ArrayList<>();
/* Getters and setters */
/**
* @return the reward items
*/
public ItemStack[] getItems() {
return items.toArray(new ItemStack[items.size()]);
}
/**
* @param items
* the reward items to set
*/
public void setItems(ItemStack[] items) {
this.items = Arrays.asList(items);
}
/**
* @param items
* the reward items to add
*/
public void addItems(ItemStack... items) {
this.items.addAll(Arrays.asList(items));
}
/**
* @param items
* the reward items to remove
*/
public void removeItems(ItemStack... items) {
this.items.addAll(Arrays.asList(items));
}
@Override
public RewardType getType() {
return type;
}
/* Actions */
@Override
public void giveTo(Player player) {
new DLootInventory(player, getItems());
}
}

View File

@ -54,7 +54,7 @@ public class MoneyReward extends Reward {
@Override
public void giveTo(Player player) {
if (plugin.getEconomyProvider() == null) {
if (plugin.getEconomyProvider() == null || money == 0) {
return;
}

View File

@ -29,8 +29,6 @@ import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
/**
@ -46,8 +44,9 @@ public class RewardChest {
private DGameWorld gameWorld;
private double moneyReward;
private int levelReward;
private ItemStack[] itemReward;
public RewardChest(Block chest, DGameWorld gameWorld, double moneyReward, int levelReward) {
public RewardChest(Block chest, DGameWorld gameWorld, double moneyReward, int levelReward, ItemStack[] itemReward) {
if (!(chest.getState() instanceof Chest)) {
return;
}
@ -56,6 +55,7 @@ public class RewardChest {
this.gameWorld = gameWorld;
this.moneyReward = moneyReward;
this.levelReward = levelReward;
this.itemReward = itemReward;
gameWorld.getRewardChests().add(this);
}
@ -135,6 +135,23 @@ public class RewardChest {
this.levelReward = levelReward;
}
/* Actions */
/**
* @param opener
* the player who opens the chest
*/
public void onOpen(Player opener) {
if (used) {
MessageUtil.sendMessage(plugin.getServer().getPlayer(opener.getUniqueId()), DMessages.ERROR_CHEST_IS_OPENED.getMessage());
return;
}
if (chest.getLocation().distance(chest.getLocation()) < 1) {
addTreasure(DGroup.getByPlayer(opener));
used = true;
}
}
public void addTreasure(DGroup dGroup) {
if (dGroup == null) {
return;
@ -142,14 +159,20 @@ public class RewardChest {
boolean hasMoneyReward = false;
boolean hasLevelReward = false;
boolean hasItemReward = false;
for (Reward reward : dGroup.getRewards()) {
if (reward instanceof MoneyReward) {
if (reward.getType() == RewardTypeDefault.MONEY) {
hasMoneyReward = true;
((MoneyReward) reward).addMoney(moneyReward);
} else if (reward instanceof LevelReward) {
} else if (reward.getType() == RewardTypeDefault.LEVEL) {
hasLevelReward = true;
((LevelReward) reward).addLevels(levelReward);
} else if (reward.getType() == RewardTypeDefault.ITEM) {
hasItemReward = true;
((ItemReward) reward).addItems(itemReward);
}
}
@ -165,12 +188,19 @@ public class RewardChest {
dGroup.addReward(reward);
}
if (!hasItemReward) {
Reward reward = Reward.create(RewardTypeDefault.ITEM);
((ItemReward) reward).addItems(itemReward);
dGroup.addReward(reward);
}
for (Player player : dGroup.getPlayers()) {
DGamePlayer dPlayer = DGamePlayer.getByPlayer(player);
if (dPlayer == null) {
continue;
}
if (itemReward != null) {
String msg = "";
for (ItemStack itemStack : chest.getInventory().getContents()) {
@ -178,7 +208,6 @@ public class RewardChest {
continue;
}
dPlayer.getTreasureInv().addItem(itemStack);
String name = null;
if (itemStack.hasItemMeta()) {
@ -208,52 +237,15 @@ public class RewardChest {
msg = msg.substring(0, msg.length() - 1);
}
MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(DMessages.PLAYER_LOOT_ADDED, msg));
MessageUtil.sendMessage(player, DMessages.PLAYER_LOOT_ADDED.getMessage(msg));
}
if (moneyReward != 0 && plugin.getEconomyProvider() != null) {
MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(DMessages.PLAYER_LOOT_ADDED, plugin.getEconomyProvider().format(moneyReward)));
MessageUtil.sendMessage(player, DMessages.PLAYER_LOOT_ADDED.getMessage(plugin.getEconomyProvider().format(moneyReward)));
}
if (levelReward != 0) {
MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(DMessages.PLAYER_LOOT_ADDED, levelReward + " levels"));
}
}
}
/* Statics */
/**
* @deprecated
* @param event
* event.getPlayer() has to be a Player
*/
public static void onOpenInventory(InventoryOpenEvent event) {
InventoryView inventory = event.getView();
DGameWorld gameWorld = DGameWorld.getByWorld(event.getPlayer().getWorld());
if (gameWorld == null) {
return;
}
if (!(inventory.getTopInventory().getHolder() instanceof Chest)) {
return;
}
Chest chest = (Chest) inventory.getTopInventory().getHolder();
for (RewardChest rewardChest : gameWorld.getRewardChests()) {
if (!rewardChest.chest.equals(chest)) {
continue;
}
if (rewardChest.used) {
MessageUtil.sendMessage(plugin.getServer().getPlayer(event.getPlayer().getUniqueId()), DMessages.ERROR_CHEST_IS_OPENED.getMessage());
event.setCancelled(true);
continue;
}
if (rewardChest.chest.getLocation().distance(chest.getLocation()) < 1) {
rewardChest.addTreasure(DGroup.getByPlayer((Player) event.getPlayer()));
rewardChest.used = true;
event.setCancelled(true);
MessageUtil.sendMessage(player, DMessages.PLAYER_LOOT_ADDED.getMessage(levelReward + " levels"));
}
}
}

View File

@ -21,9 +21,9 @@ package io.github.dre2n.dungeonsxl.reward;
*/
public enum RewardTypeDefault implements RewardType {
ITEM("item", ItemReward.class),
MONEY("money", MoneyReward.class),
LEVEL("level", LevelReward.class),
LOOT_INVENTORY("loot", Reward.class);
LEVEL("level", LevelReward.class);
private String identifier;
private Class<? extends Reward> handler;

View File

@ -20,7 +20,10 @@ import io.github.dre2n.commons.util.NumberUtil;
import io.github.dre2n.dungeonsxl.reward.RewardChest;
import io.github.dre2n.dungeonsxl.world.DGameWorld;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.inventory.ItemStack;
/**
* @author Frank Baumann, Daniel Saukel
@ -29,14 +32,102 @@ public class ChestSign extends DSign {
private DSignType type = DSignTypeDefault.CHEST;
// Variables
private Block chest;
private double moneyReward;
private int levelReward;
private ItemStack[] itemReward;
public ChestSign(Sign sign, String[] lines, DGameWorld gameWorld) {
super(sign, lines, gameWorld);
}
/* Getters and setters */
/**
* @return the money reward
*/
public double getMoneyReward() {
return moneyReward;
}
/**
* @param amount
* the amount to set
*/
public void setMoneyReward(double amount) {
moneyReward = amount;
}
/**
* @return the level reward
*/
public int getLevelReward() {
return levelReward;
}
/**
* @param amount
* the amount to set
*/
public void setLevelReward(int amount) {
levelReward = amount;
}
/**
* @return the item reward
*/
public ItemStack[] getItemReward() {
if (itemReward == null) {
checkChest();
}
return itemReward;
}
/**
* @param items
* the items to set as a reward
*/
public void setItemReward(ItemStack[] items) {
itemReward = items;
}
@Override
public DSignType getType() {
return type;
}
/* Actions */
/**
* Checks for a chest next to the sign and sets the reward to its contents.
*/
public void checkChest() {
Block sign = getSign().getBlock();
for (int i = -1; i <= 1; i++) {
Block xRelative = sign.getRelative(i, 0, 0);
Block yRelative = sign.getRelative(0, i, 0);
Block zRelative = sign.getRelative(0, 0, i);
if (xRelative.getType() == Material.CHEST) {
if (itemReward == null) {
itemReward = ((Chest) xRelative.getState()).getBlockInventory().getContents();
}
chest = xRelative;
} else if (yRelative.getType() == Material.CHEST) {
if (itemReward == null) {
itemReward = ((Chest) yRelative.getState()).getBlockInventory().getContents();
}
chest = yRelative;
} else if (zRelative.getType() == Material.CHEST) {
if (itemReward == null) {
itemReward = ((Chest) zRelative.getState()).getBlockInventory().getContents();
}
chest = zRelative;
}
}
}
@Override
public boolean check() {
return true;
@ -54,26 +145,17 @@ public class ChestSign extends DSign {
}
}
for (int i = -1; i <= 1; i++) {
if (getSign().getBlock().getRelative(i, 0, 0).getType() == Material.CHEST) {
new RewardChest(getSign().getBlock().getRelative(i, 0, 0), getGameWorld(), moneyReward, levelReward);
}
if (getSign().getBlock().getRelative(0, 0, i).getType() == Material.CHEST) {
new RewardChest(getSign().getBlock().getRelative(0, 0, i), getGameWorld(), moneyReward, levelReward);
}
if (getSign().getBlock().getRelative(0, i, 0).getType() == Material.CHEST) {
new RewardChest(getSign().getBlock().getRelative(0, i, 0), getGameWorld(), moneyReward, levelReward);
}
if (chest == null) {
checkChest();
}
if (chest != null) {
new RewardChest(chest, getGameWorld(), moneyReward, levelReward, itemReward);
getSign().getBlock().setType(Material.AIR);
}
@Override
public DSignType getType() {
return type;
} else {
markAsErroneous();
}
}
}

View File

@ -18,6 +18,7 @@ package io.github.dre2n.dungeonsxl.sign;
import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.event.dsign.DSignRegistrationEvent;
import io.github.dre2n.dungeonsxl.game.Game;
import io.github.dre2n.dungeonsxl.trigger.Trigger;
@ -26,6 +27,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
@ -36,6 +38,11 @@ public abstract class DSign {
static DungeonsXL plugin = DungeonsXL.getInstance();
public static final String ERROR_0 = ChatColor.DARK_RED + "## ERROR ##";
public static final String ERROR_1 = ChatColor.WHITE + "Please";
public static final String ERROR_2 = ChatColor.WHITE + "contact an";
public static final String ERROR_3 = ChatColor.WHITE + "Admin!";
private Sign sign;
protected String[] lines;
private DGameWorld gameWorld;
@ -75,6 +82,7 @@ public abstract class DSign {
}
}
/* Getters and setters */
/**
* @return the sign
*/
@ -142,6 +150,7 @@ public abstract class DSign {
triggers.remove(trigger);
}
/* Actions */
public void onInit() {
}
@ -185,6 +194,20 @@ public abstract class DSign {
return !triggers.isEmpty();
}
/**
* Set a placeholder to show that the sign is setup incorrectly.
*/
public void markAsErroneous() {
sign.setLine(0, ERROR_0);
sign.setLine(1, ERROR_1);
sign.setLine(2, ERROR_2);
sign.setLine(3, ERROR_3);
sign.update();
DMessages.LOG_ERROR_SIGN_SETUP.getMessage(sign.getX() + ", " + sign.getY() + ", " + sign.getZ());
}
/* Statics */
public static DSign create(Sign sign, DGameWorld gameWorld) {
return create(sign, sign.getLines(), gameWorld);
}

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.1</version>
<version>0.14.2</version>
</parent>
<build>
<plugins>

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.1</version>
<version>0.14.2</version>
</parent>
<build>
<plugins>

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.1</version>
<version>0.14.2</version>
</parent>
<build>
<plugins>

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.1</version>
<version>0.14.2</version>
<packaging>pom</packaging>
<name>DungeonsXL</name>
<url>https://dre2n.github.io</url>

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.1</version>
<version>0.14.2</version>
</parent>
<build>
<finalName>dungeonsxl-${project.version}${buildNo}</finalName>