From 6057044935bb104706bd892671edb932350bfd4c Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sat, 5 Jan 2019 21:13:59 +0100 Subject: [PATCH] #509: Add DungeonChest sign; rename Chest sign to RewardChest sign [WIP] --- .../de/erethon/dungeonsxl/sign/ChestSign.java | 98 +------------- .../dungeonsxl/sign/DSignTypeDefault.java | 5 +- .../dungeonsxl/sign/DungeonChestSign.java | 92 +++++++++++++ .../dungeonsxl/sign/RewardChestSign.java | 128 ++++++++++++++++++ 4 files changed, 231 insertions(+), 92 deletions(-) create mode 100644 src/main/java/de/erethon/dungeonsxl/sign/DungeonChestSign.java create mode 100644 src/main/java/de/erethon/dungeonsxl/sign/RewardChestSign.java diff --git a/src/main/java/de/erethon/dungeonsxl/sign/ChestSign.java b/src/main/java/de/erethon/dungeonsxl/sign/ChestSign.java index 1b93d04b..b63b64c3 100644 --- a/src/main/java/de/erethon/dungeonsxl/sign/ChestSign.java +++ b/src/main/java/de/erethon/dungeonsxl/sign/ChestSign.java @@ -17,15 +17,9 @@ package de.erethon.dungeonsxl.sign; import de.erethon.caliburn.category.Category; -import de.erethon.caliburn.item.VanillaItem; import de.erethon.caliburn.loottable.LootTable; -import de.erethon.commons.misc.NumberUtil; import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.world.DGameWorld; -import de.erethon.dungeonsxl.world.block.RewardChest; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; import org.bukkit.block.Block; import org.bukkit.block.Chest; import org.bukkit.block.Sign; @@ -34,50 +28,18 @@ import org.bukkit.inventory.ItemStack; /** * @author Frank Baumann, Daniel Saukel */ -public class ChestSign extends DSign { +public abstract class ChestSign extends DSign { - private DSignType type = DSignTypeDefault.CHEST; + protected Block chest; - private Block chest; + protected ItemStack[] chestContent; + protected LootTable lootTable; - private double moneyReward; - private int levelReward; - private ItemStack[] chestContent; - private LootTable lootTable; - - public ChestSign(DungeonsXL plugin, Sign sign, String[] lines, DGameWorld gameWorld) { + protected ChestSign(DungeonsXL plugin, Sign sign, String[] lines, DGameWorld gameWorld) { super(plugin, 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 chest contents */ @@ -91,7 +53,7 @@ public class ChestSign extends DSign { /** * @param items the items to set as chest contents */ - public void setItemReward(ItemStack[] items) { + public void setChestContents(ItemStack[] items) { chestContent = items; } @@ -109,16 +71,11 @@ public class ChestSign extends DSign { this.lootTable = lootTable; } - @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() { + protected void checkChest() { Block sign = getSign().getBlock(); for (int i = -1; i <= 1; i++) { Block xRelative = sign.getRelative(i, 0, 0); @@ -146,45 +103,4 @@ public class ChestSign extends DSign { } } - @Override - public boolean check() { - return true; - } - - @Override - public void onInit() { - if (!lines[1].isEmpty()) { - String[] attributes = lines[1].split(","); - if (attributes.length >= 1) { - moneyReward = NumberUtil.parseDouble(attributes[0]); - } - if (attributes.length >= 2) { - levelReward = NumberUtil.parseInt(attributes[1]); - } - } - - if (!lines[2].isEmpty()) { - lootTable = plugin.getCaliburn().getLootTable(lines[2]); - } - - if (chest == null) { - checkChest(); - } - - if (chest != null) { - ItemStack[] itemReward = chestContent; - if (lootTable != null) { - List list = new LinkedList<>(Arrays.asList(chestContent)); - list.addAll(lootTable.generateLootList()); - itemReward = list.toArray(new ItemStack[list.size()]); - } - - getGameWorld().addGameBlock(new RewardChest(plugin, chest, moneyReward, levelReward, itemReward)); - getSign().getBlock().setType(VanillaItem.AIR.getMaterial()); - - } else { - markAsErroneous("No chest attached"); - } - } - } diff --git a/src/main/java/de/erethon/dungeonsxl/sign/DSignTypeDefault.java b/src/main/java/de/erethon/dungeonsxl/sign/DSignTypeDefault.java index b3aacb69..dd1e65e6 100644 --- a/src/main/java/de/erethon/dungeonsxl/sign/DSignTypeDefault.java +++ b/src/main/java/de/erethon/dungeonsxl/sign/DSignTypeDefault.java @@ -39,11 +39,13 @@ public enum DSignTypeDefault implements DSignType { BLOCK("Block", "block", false, true, BlockSign.class), BOSS_SHOP("BossShop", "bossshop", false, true, BossShopSign.class), CHECKPOINT("Checkpoint", "checkpoint", false, false, CheckpointSign.class), - CHEST("Chest", "chest", false, false, ChestSign.class), + @Deprecated + CHEST("Chest", "chest", false, false, RewardChestSign.class), CHUNK_UPDATER("ChunkUpdater", "chunkupdater", true, false, ChunkUpdaterSign.class), CLASSES("Classes", "classes", true, true, ClassesSign.class), COMMAND("CMD", "cmd", false, false, CommandSign.class), DROP("Drop", "drop", false, false, DropSign.class), + DUNGEON_CHEST("DungeonChest", "dungeonchest", true, false, DungeonChestSign.class), END("End", "end", false, true, EndSign.class), @Deprecated EXTERNAL_MOB("ExternalMob", "mob", false, false, MobSign.class), @@ -63,6 +65,7 @@ public enum DSignTypeDefault implements DSignType { READY("Ready", "ready", true, true, ReadySign.class), REDSTONE("Redstone", "redstone", false, false, RedstoneSign.class), RESOURCE_PACK("ResourcePack", "resourcepack", true, true, ResourcePackSign.class), + REWARD_CHEST("RewardChest", "rewardchest", false, false, RewardChestSign.class), SCRIPT("Script", "script", false, false, ScriptSign.class), SOUND_MESSAGE("SoundMSG", "soundmsg", false, false, SoundMessageSign.class), START("Start", "start", true, false, StartSign.class), diff --git a/src/main/java/de/erethon/dungeonsxl/sign/DungeonChestSign.java b/src/main/java/de/erethon/dungeonsxl/sign/DungeonChestSign.java new file mode 100644 index 00000000..68a549a6 --- /dev/null +++ b/src/main/java/de/erethon/dungeonsxl/sign/DungeonChestSign.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2012-2018 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 . + */ +package de.erethon.dungeonsxl.sign; + +import de.erethon.caliburn.item.VanillaItem; +import de.erethon.dungeonsxl.DungeonsXL; +import de.erethon.dungeonsxl.world.DGameWorld; +import java.util.Arrays; +import java.util.List; +import org.bukkit.block.Chest; +import org.bukkit.block.Sign; +import org.bukkit.inventory.ItemStack; + +/** + * @author Daniel Saukel + */ +public class DungeonChestSign extends ChestSign { + + private DSignType type = DSignTypeDefault.DUNGEON_CHEST; + + public DungeonChestSign(DungeonsXL plugin, Sign sign, String[] lines, DGameWorld gameWorld) { + super(plugin, sign, lines, gameWorld); + } + + /* Getters and setters */ + @Override + public DSignType getType() { + return type; + } + + /* Actions */ + @Override + public boolean check() { + return true; + } + + @Override + public void onInit() { + if (!lines[2].isEmpty()) { + lootTable = plugin.getCaliburn().getLootTable(lines[2]); + } + + if (chest == null) { + checkChest(); + } + + if (chest != null) { + getSign().getBlock().setType(VanillaItem.AIR.getMaterial()); + } else { + getSign().getBlock().setType(VanillaItem.CHEST.getMaterial()); + chest = getSign().getBlock(); + } + + Chest state = (Chest) chest.getState(); + List list = null; + if (lootTable != null) { + list = lootTable.generateLootList(); + } + if (chestContent != null) { + if (list != null) { + list = Arrays.asList(chestContent); + } else { + list.addAll(Arrays.asList(chestContent)); + } + } + if (list == null) { + return; + } + ItemStack[] contents = list.toArray(new ItemStack[list.size()]); + if (contents.length > state.getBlockInventory().getSize()) { + contents = Arrays.copyOfRange(contents, 0, state.getBlockInventory().getSize()); + } + state.getBlockInventory().setContents(contents); + state.update(); + System.out.println(Arrays.toString(state.getBlockInventory().getContents())); + } + +} diff --git a/src/main/java/de/erethon/dungeonsxl/sign/RewardChestSign.java b/src/main/java/de/erethon/dungeonsxl/sign/RewardChestSign.java new file mode 100644 index 00000000..8db85d28 --- /dev/null +++ b/src/main/java/de/erethon/dungeonsxl/sign/RewardChestSign.java @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2012-2018 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 . + */ +package de.erethon.dungeonsxl.sign; + +import de.erethon.caliburn.item.VanillaItem; +import de.erethon.commons.misc.NumberUtil; +import de.erethon.dungeonsxl.DungeonsXL; +import de.erethon.dungeonsxl.world.DGameWorld; +import de.erethon.dungeonsxl.world.block.RewardChest; +import java.util.Arrays; +import java.util.List; +import org.bukkit.block.Sign; +import org.bukkit.inventory.ItemStack; + +/** + * @author Daniel Saukel + */ +public class RewardChestSign extends ChestSign { + + private DSignType type = DSignTypeDefault.REWARD_CHEST; + + private double moneyReward; + private int levelReward; + + public RewardChestSign(DungeonsXL plugin, Sign sign, String[] lines, DGameWorld gameWorld) { + super(plugin, 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; + } + + @Override + public DSignType getType() { + return type; + } + + /* Actions */ + @Override + public boolean check() { + return true; + } + + @Override + public void onInit() { + if (!lines[1].isEmpty()) { + String[] attributes = lines[1].split(","); + if (attributes.length >= 1) { + moneyReward = NumberUtil.parseDouble(attributes[0]); + } + if (attributes.length >= 2) { + levelReward = NumberUtil.parseInt(attributes[1]); + } + } + + if (!lines[2].isEmpty()) { + lootTable = plugin.getCaliburn().getLootTable(lines[2]); + } + + if (chest == null) { + checkChest(); + } + + if (chest != null) { + getSign().getBlock().setType(VanillaItem.AIR.getMaterial()); + } else { + getSign().getBlock().setType(VanillaItem.CHEST.getMaterial()); + chest = getSign().getBlock(); + } + + List list = null; + if (lootTable != null) { + list = lootTable.generateLootList(); + } + if (chestContent != null) { + if (list != null) { + list = Arrays.asList(chestContent); + } else { + list.addAll(Arrays.asList(chestContent)); + } + } + if (list == null) { + return; + } + + getGameWorld().addGameBlock(new RewardChest(plugin, chest, moneyReward, levelReward, list.toArray(new ItemStack[list.size()]))); + } + +}