From 57a177a8cf06aa76a8680cadfdeb619f5180a300 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Tue, 14 Jul 2015 18:36:34 +0200 Subject: [PATCH] Full support for lives and keeping inventory in dungeons (0.9) --- src/com/dre/dungeonsxl/DConfig.java | 14 +++++++++ src/com/dre/dungeonsxl/DPlayer.java | 22 +++++++++---- src/com/dre/dungeonsxl/LanguageReader.java | 4 +++ src/com/dre/dungeonsxl/P.java | 11 +++++++ .../dre/dungeonsxl/commands/DCommandRoot.java | 2 ++ .../dungeonsxl/listener/DeathListener.java | 31 +++++++++++++++++++ src/plugin.yml | 2 +- 7 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 src/com/dre/dungeonsxl/listener/DeathListener.java diff --git a/src/com/dre/dungeonsxl/DConfig.java b/src/com/dre/dungeonsxl/DConfig.java index f5d378ee..e9a23858 100644 --- a/src/com/dre/dungeonsxl/DConfig.java +++ b/src/com/dre/dungeonsxl/DConfig.java @@ -30,6 +30,8 @@ public class DConfig { private CopyOnWriteArrayList invitedPlayers = new CopyOnWriteArrayList(); private CopyOnWriteArrayList secureObjects = new CopyOnWriteArrayList(); + private int initialLives = 3; + private boolean isLobbyDisabled = false; private int timeToNextPlay = 0; private int timeToNextLoot = 0; @@ -152,6 +154,13 @@ public class DConfig { keepInventory = mainConfig.keepInventory; } + /* keep Inventory */ + if (configFile.contains("initialLives")) { + initialLives = configFile.getInt("initialLives"); + } else { + initialLives = mainConfig.getInitialLives(); + } + /* Lobby */ if (configFile.contains("isLobbyDisabled")) { isLobbyDisabled = configFile.getBoolean("isLobbyDisabled"); @@ -297,6 +306,10 @@ public class DConfig { return keepInventory; } + public int getInitialLives() { + return initialLives; + } + public boolean isLobbyDisabled() { return isLobbyDisabled; } @@ -331,4 +344,5 @@ public class DConfig { public Set getMobTypes() { return mobTypes; } + } diff --git a/src/com/dre/dungeonsxl/DPlayer.java b/src/com/dre/dungeonsxl/DPlayer.java index 2ac2aadf..a2672caf 100644 --- a/src/com/dre/dungeonsxl/DPlayer.java +++ b/src/com/dre/dungeonsxl/DPlayer.java @@ -66,6 +66,7 @@ public class DPlayer { this.savePlayer = new DSavePlayer(player.getName(), player.getUniqueId(), player.getLocation(), player.getInventory().getContents(), player.getInventory().getArmorContents(), player.getLevel(), player.getTotalExperience(), (int) health, player.getFoodLevel(), player.getFireTicks(), player.getGameMode(), player.getActivePotionEffects()); + if (!(GameWorld.get(world).config.getKeepInventory())) { this.player.getInventory().clear(); this.player.getInventory().setArmorContents(null); this.player.setTotalExperience(0); @@ -75,6 +76,10 @@ public class DPlayer { for (PotionEffect effect : this.player.getActivePotionEffects()) { this.player.removePotionEffect(effect.getType()); } + } + + // Lives + P.lives.put(this.player, GameWorld.get(world).config.getInitialLives()); this.isEditing = isEditing; @@ -100,6 +105,9 @@ public class DPlayer { public void leave() { remove(this); + // Lives + P.lives.remove(player); + this.savePlayer.reset(); if (this.isEditing) { @@ -204,13 +212,15 @@ public class DPlayer { } // Respawn Items - if (this.respawnInventory != null || this.respawnArmor != null) { - this.player.getInventory().setContents(this.respawnInventory); - this.player.getInventory().setArmorContents(this.respawnArmor); - this.respawnInventory = null; - this.respawnArmor = null; - } + if (!(GameWorld.get(world).config.getKeepInventory())) { + if (this.respawnInventory != null || this.respawnArmor != null) { + this.player.getInventory().setContents(this.respawnInventory); + this.player.getInventory().setArmorContents(this.respawnArmor); + this.respawnInventory = null; + this.respawnArmor = null; + } // P.p.updateInventory(this.player); + } } public void finish() { diff --git a/src/com/dre/dungeonsxl/LanguageReader.java b/src/com/dre/dungeonsxl/LanguageReader.java index a9b12926..effccac8 100644 --- a/src/com/dre/dungeonsxl/LanguageReader.java +++ b/src/com/dre/dungeonsxl/LanguageReader.java @@ -62,6 +62,8 @@ public class LanguageReader { defaults.put("Player_SignCreated", "&6Sign created!"); defaults.put("Player_SignCopied", "&6Copied!"); defaults.put("Player_BlockInfo", "&6Block-ID: &2&v1"); + defaults.put("Player_Death", "&6You died, lives left: &2v1"); + defaults.put("Player_DeathKick", "&2v1&6 died and lost his last life."); /* Cmds */ defaults.put("Cmd_Chat_DungeonChat", "&6You have entered the Dungeon-chat"); @@ -76,6 +78,7 @@ public class LanguageReader { defaults.put("Cmd_Reload_Done", "&6DungeonsXL was successfully reloaded!"); defaults.put("Cmd_Save_Success", "&6Dungeon saved!"); defaults.put("Cmd_Uninvite_Success", "&4&v1&6 was successfully uninvited to edit the Dungeon &4&v1&6!"); + defaults.put("Cmd_Lives", "&4v1&6 has &4v2 &6lives left."); /* Errors */ defaults.put("Error_Enderchest", "&4You cannot use an enderchest while in a Dungeon!"); @@ -121,6 +124,7 @@ public class LanguageReader { defaults.put("Help_Cmd_Save", "/dxl save - Saves the current dungeon"); defaults.put("Help_Cmd_Test", "/dxl test [dungeon] - Tests a dungeon"); defaults.put("Help_Cmd_Uninvite", "/dxl uninvite - Uninvite a player to edit a dungeon"); + defaults.put("Help_Cmd_Lives", "/dxl lives - show the lives a player has left"); } diff --git a/src/com/dre/dungeonsxl/P.java b/src/com/dre/dungeonsxl/P.java index a2d56056..daf3ae12 100644 --- a/src/com/dre/dungeonsxl/P.java +++ b/src/com/dre/dungeonsxl/P.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.FileChannel; +import java.util.HashMap; import java.util.UUID; import java.util.concurrent.CopyOnWriteArrayList; @@ -37,6 +38,7 @@ import com.dre.dungeonsxl.commands.DCommandRoot; import com.dre.dungeonsxl.game.GameWorld; import com.dre.dungeonsxl.listener.BlockListener; import com.dre.dungeonsxl.listener.CommandListener; +import com.dre.dungeonsxl.listener.DeathListener; import com.dre.dungeonsxl.listener.EntityListener; import com.dre.dungeonsxl.listener.HangingListener; import com.dre.dungeonsxl.listener.PlayerListener; @@ -45,12 +47,16 @@ import com.dre.dungeonsxl.listener.WorldListener; public class P extends JavaPlugin { public static P p; + // Lives + public static HashMap lives; + // Listener private static Listener entityListener; private static Listener playerListener; private static Listener blockListener; private static Listener worldListener; private static Listener hangingListener; + private static Listener deathListener; // Main Config Reader public MainConfig mainConfig; @@ -65,6 +71,9 @@ public class P extends JavaPlugin { public void onEnable() { p = this; + // Lives + lives = new HashMap(); + // Commands getCommand("dungeonsxl").setExecutor(new CommandListener()); @@ -92,12 +101,14 @@ public class P extends JavaPlugin { blockListener = new BlockListener(); worldListener = new WorldListener(); hangingListener = new HangingListener(); + deathListener = new DeathListener(); Bukkit.getServer().getPluginManager().registerEvents(entityListener, this); Bukkit.getServer().getPluginManager().registerEvents(playerListener, this); Bukkit.getServer().getPluginManager().registerEvents(blockListener, this); Bukkit.getServer().getPluginManager().registerEvents(worldListener, this); Bukkit.getServer().getPluginManager().registerEvents(hangingListener, this); + Bukkit.getServer().getPluginManager().registerEvents(deathListener, this); // Load All this.loadAll(); diff --git a/src/com/dre/dungeonsxl/commands/DCommandRoot.java b/src/com/dre/dungeonsxl/commands/DCommandRoot.java index 1f094022..928b0170 100644 --- a/src/com/dre/dungeonsxl/commands/DCommandRoot.java +++ b/src/com/dre/dungeonsxl/commands/DCommandRoot.java @@ -25,6 +25,7 @@ public class DCommandRoot { public CMDTest cmdTest = new CMDTest(); public CMDHelp cmdHelp = new CMDHelp(); public CMDReload cmdReload = new CMDReload(); + public CMDLives cmdLives = new CMDLives(); // Methods public DCommandRoot() { @@ -47,5 +48,6 @@ public class DCommandRoot { this.commands.add(cmdTest); this.commands.add(cmdHelp); this.commands.add(cmdReload); + this.commands.add(cmdLives); } } diff --git a/src/com/dre/dungeonsxl/listener/DeathListener.java b/src/com/dre/dungeonsxl/listener/DeathListener.java new file mode 100644 index 00000000..58288d76 --- /dev/null +++ b/src/com/dre/dungeonsxl/listener/DeathListener.java @@ -0,0 +1,31 @@ +package com.dre.dungeonsxl.listener; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; + +import com.dre.dungeonsxl.P; + +public class DeathListener implements Listener { + + P p = P.p; + int lives = -1; + + @EventHandler + public void onDeath(PlayerDeathEvent event) { + Player player = event.getEntity(); + if (P.lives.containsKey(player)) { + lives = P.lives.get(player) - 1; + P.lives.put(player, lives); + } + if (lives == 0) { + Bukkit.broadcastMessage(p.language.get("Player_DeathKick").replaceAll("v1", player.getName()).replaceAll("&", "\u00a76")); + player.performCommand("dxl leave"); + } else if (!(lives == -1)) { + p.msg(player, p.language.get("Player_Death").replaceAll("v1", String.valueOf(lives))); + } + } + +} diff --git a/src/plugin.yml b/src/plugin.yml index ce0ebf03..f976e9b3 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: DungeonsXL main: com.dre.dungeonsxl.P -version: 0.9-SNAPSHOT +version: 0.9 author: Frank Baumann authors: [Frank Baumann, Milan Albrecht, Tobias Schmitz, Daniel Saukel] website: http://www.dre2n.ml