Events reworked (moved all logic to LCPlayer, previously PlayerCore)
added ondie-dropping prevention, AND give items back on respawn
This commit is contained in:
parent
237ff18841
commit
d07a3a6b78
4 changed files with 214 additions and 98 deletions
154
src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java
Normal file
154
src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java
Normal file
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
* Limited Creative - (Bukkit Plugin)
|
||||
* Copyright (C) 2012 jascha@ja-s.de
|
||||
*
|
||||
* 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 de.jaschastarke.minecraft.limitedcreative;
|
||||
|
||||
import static de.jaschastarke.minecraft.utils.Locale.L;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.event.player.PlayerDropItemEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerPickupItemEvent;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import de.jaschastarke.minecraft.limitedcreative.serialize.Items;
|
||||
|
||||
public class LCPlayer {
|
||||
private static LimitedCreativeCore plugin = LimitedCreativeCore.plugin;
|
||||
private static Map<Player, LCPlayer> players = new HashMap<Player, LCPlayer>();
|
||||
private Player player;
|
||||
private Map<Integer, ItemStack> tempinv = null;
|
||||
|
||||
private LCPlayer(Player pplayer) {
|
||||
player = pplayer;
|
||||
}
|
||||
|
||||
public static LCPlayer get(Player pplayer) {
|
||||
if (!players.containsKey(pplayer)) {
|
||||
LCPlayer p = new LCPlayer(pplayer);
|
||||
players.put(pplayer, p);
|
||||
return p;
|
||||
} else {
|
||||
return players.get(pplayer);
|
||||
}
|
||||
}
|
||||
|
||||
public void onSetCreative() {
|
||||
if (plugin.config.getPermissionToKeepInventory() && player.hasPermission("limitedcreative.keepinventory"))
|
||||
return;
|
||||
Inventory inv = new Inventory(player);
|
||||
inv.save();
|
||||
if (plugin.config.getStoreCreative() && inv.isStored(GameMode.CREATIVE)) {
|
||||
inv.load(GameMode.CREATIVE);
|
||||
} else {
|
||||
inv.clear();
|
||||
}
|
||||
}
|
||||
public void onSetSurvival() {
|
||||
if (plugin.config.getPermissionToKeepInventory() && player.hasPermission("limitedcreative.keepinventory"))
|
||||
return;
|
||||
Inventory inv = new Inventory(player);
|
||||
if (plugin.config.getStoreCreative()) {
|
||||
inv.save();
|
||||
}
|
||||
if (inv.isStored(GameMode.SURVIVAL))
|
||||
inv.load(GameMode.SURVIVAL);
|
||||
}
|
||||
public void onDropItem(PlayerDropItemEvent event) {
|
||||
if (player.getGameMode() == GameMode.CREATIVE) {
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.drop"))
|
||||
return;
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
public void onPickupItem(PlayerPickupItemEvent event) {
|
||||
if (player.getGameMode() == GameMode.CREATIVE && plugin.config.getBlockPickupInCreative()) {
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.pickup"))
|
||||
return;
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void onDie(EntityDeathEvent event) {
|
||||
if (player.getGameMode() == GameMode.CREATIVE) {
|
||||
if (!plugin.config.getPermissionsEnabled() || !player.hasPermission("limitedcreative.nolimit.drop")) {
|
||||
event.getDrops().clear();
|
||||
tempinv = Items.storeInventory(player.getInventory());
|
||||
}
|
||||
}
|
||||
}
|
||||
public void onRespawn(PlayerRespawnEvent event) {
|
||||
if (player.getGameMode() == GameMode.CREATIVE) {
|
||||
if (!plugin.config.getPermissionsEnabled() || !player.hasPermission("limitedcreative.nolimit.drop")) {
|
||||
if (tempinv != null) {
|
||||
Items.restoreInventory(player.getInventory(), tempinv);
|
||||
}
|
||||
}
|
||||
}
|
||||
tempinv = null;
|
||||
}
|
||||
|
||||
public void onDamage(EntityDamageByEntityEvent event) {
|
||||
if (event.getDamager() instanceof Player) {
|
||||
// its PVP
|
||||
Player attacker = (Player) event.getDamager();
|
||||
if (attacker.getGameMode() == GameMode.CREATIVE) {
|
||||
if (!plugin.config.getPermissionsEnabled() || !attacker.hasPermission("limitedcreative.nolimit.pvp")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
if (player.getGameMode() == GameMode.CREATIVE) {
|
||||
if (!plugin.config.getPermissionsEnabled() || !player.hasPermission("limitedcreative.nolimit.pvp")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void onChestAccess(PlayerInteractEvent event) {
|
||||
if (player.getGameMode() != GameMode.CREATIVE)
|
||||
return;
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.chest"))
|
||||
return;
|
||||
event.getPlayer().sendMessage(L("blocked.chest"));
|
||||
event.setCancelled(true);
|
||||
}
|
||||
public void onChestAccess(PlayerInteractEntityEvent event) { // chest-minecarts are different events
|
||||
if (player.getGameMode() != GameMode.CREATIVE)
|
||||
return;
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.chest"))
|
||||
return;
|
||||
event.getPlayer().sendMessage(L("blocked.chest"));
|
||||
event.setCancelled(true);
|
||||
}
|
||||
public void onSignAccess(PlayerInteractEvent event) {
|
||||
if (!plugin.config.getSignBlock() || player.getGameMode() != GameMode.CREATIVE)
|
||||
return;
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.sign"))
|
||||
return;
|
||||
event.getPlayer().sendMessage(L("blocked.sign"));
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
}
|
|
@ -29,6 +29,7 @@ import org.bukkit.event.Event.Priority;
|
|||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.event.entity.EntityListener;
|
||||
import org.bukkit.event.player.PlayerDropItemEvent;
|
||||
import org.bukkit.event.player.PlayerGameModeChangeEvent;
|
||||
|
@ -36,8 +37,8 @@ import org.bukkit.event.player.PlayerInteractEntityEvent;
|
|||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerListener;
|
||||
import org.bukkit.event.player.PlayerPickupItemEvent;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import static de.jaschastarke.minecraft.utils.Locale.L;
|
||||
|
||||
public final class Listener {
|
||||
private static LimitedCreativeCore plugin;
|
||||
|
@ -47,52 +48,41 @@ public final class Listener {
|
|||
@Override
|
||||
public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
|
||||
if (event.getNewGameMode() == GameMode.CREATIVE) {
|
||||
new PlayerCore(plugin, event.getPlayer()).onSetCreative();
|
||||
LCPlayer.get(event.getPlayer()).onSetCreative();
|
||||
} else if (event.getNewGameMode() == GameMode.SURVIVAL) {
|
||||
new PlayerCore(plugin, event.getPlayer()).onSetSurvival();
|
||||
LCPlayer.get(event.getPlayer()).onSetSurvival();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPlayerRespawn(PlayerRespawnEvent event) {
|
||||
LCPlayer.get(event.getPlayer()).onRespawn(event);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPlayerDropItem(PlayerDropItemEvent event) {
|
||||
if (event.getPlayer().getGameMode() == GameMode.CREATIVE) {
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.drop"))
|
||||
return;
|
||||
event.setCancelled(true);
|
||||
}
|
||||
LCPlayer.get(event.getPlayer()).onDropItem(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
|
||||
if (event.getPlayer().getGameMode() == GameMode.CREATIVE && plugin.config.getBlockPickupInCreative()) {
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.pickup"))
|
||||
return;
|
||||
event.setCancelled(true);
|
||||
}
|
||||
LCPlayer.get(event.getPlayer()).onPickupItem(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
if (event.isCancelled() || event.getPlayer().getGameMode() == GameMode.SURVIVAL)
|
||||
return;
|
||||
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
|
||||
if (event.isCancelled() || event.getAction() != Action.RIGHT_CLICK_BLOCK)
|
||||
return;
|
||||
|
||||
Block block = event.getClickedBlock();
|
||||
|
||||
if (block.getState() instanceof ContainerBlock) {
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.chest"))
|
||||
return;
|
||||
event.getPlayer().sendMessage(L("blocked.chest"));
|
||||
event.setCancelled(true);
|
||||
LCPlayer.get(event.getPlayer()).onChestAccess(event);
|
||||
}
|
||||
if (plugin.config.getSignBlock() && block.getState() instanceof Sign) {
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.sign"))
|
||||
return;
|
||||
event.getPlayer().sendMessage(L("blocked.sign"));
|
||||
event.setCancelled(true);
|
||||
if (block.getState() instanceof Sign) {
|
||||
LCPlayer.get(event.getPlayer()).onSignAccess(event);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,10 +94,7 @@ public final class Listener {
|
|||
Entity entity = event.getRightClicked();
|
||||
|
||||
if (entity instanceof StorageMinecart) {
|
||||
if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.chest"))
|
||||
return;
|
||||
event.getPlayer().sendMessage(L("blocked.chest"));
|
||||
event.setCancelled(true);
|
||||
LCPlayer.get(event.getPlayer()).onChestAccess(event);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,6 +104,7 @@ public final class Listener {
|
|||
pm.registerEvent(Event.Type.PLAYER_PICKUP_ITEM, this, Priority.Normal, plugin);
|
||||
pm.registerEvent(Event.Type.PLAYER_INTERACT, this, Priority.Lowest, plugin);
|
||||
pm.registerEvent(Event.Type.PLAYER_INTERACT_ENTITY, this, Priority.Lowest, plugin);
|
||||
pm.registerEvent(Event.Type.PLAYER_RESPAWN, this, Priority.Normal, plugin);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,26 +113,23 @@ public final class Listener {
|
|||
public void onEntityDamage(EntityDamageEvent meta_event) {
|
||||
if (meta_event instanceof EntityDamageByEntityEvent) {
|
||||
EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) meta_event;
|
||||
if (event.getEntity() instanceof Player && event.getDamager() instanceof Player) {
|
||||
// its PVP
|
||||
Player attacker = (Player) event.getDamager();
|
||||
Player attacked = (Player) event.getEntity();
|
||||
if (attacker.getGameMode() == GameMode.CREATIVE) {
|
||||
if (!plugin.config.getPermissionsEnabled() || !attacker.hasPermission("limitedcreative.nolimit.pvp")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
if (attacked.getGameMode() == GameMode.CREATIVE) {
|
||||
if (!plugin.config.getPermissionsEnabled() || !attacked.hasPermission("limitedcreative.nolimit.pvp")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
if (event.getEntity() instanceof Player) {
|
||||
LCPlayer.get((Player) event.getEntity()).onDamage(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEntityDeath(EntityDeathEvent event) {
|
||||
if (event.getEntity() instanceof Player) {
|
||||
Player player = (Player) event.getEntity();
|
||||
LCPlayer.get(player).onDie(event);
|
||||
}
|
||||
}
|
||||
|
||||
private void register() {
|
||||
pm.registerEvent(Event.Type.ENTITY_DAMAGE, this, Priority.Normal, plugin);
|
||||
pm.registerEvent(Event.Type.ENTITY_DEATH, this, Priority.Low, plugin);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
* Limited Creative - (Bukkit Plugin)
|
||||
* Copyright (C) 2012 jascha@ja-s.de
|
||||
*
|
||||
* 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 de.jaschastarke.minecraft.limitedcreative;
|
||||
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class PlayerCore {
|
||||
private static LimitedCreativeCore plugin;
|
||||
private Player player;
|
||||
|
||||
public PlayerCore(LimitedCreativeCore pplugin, Player pplayer) {
|
||||
plugin = pplugin;
|
||||
player = pplayer;
|
||||
}
|
||||
|
||||
public void onSetCreative() {
|
||||
if (plugin.config.getPermissionToKeepInventory() && player.hasPermission("limitedcreative.keepinventory"))
|
||||
return;
|
||||
Inventory inv = new Inventory(player);
|
||||
inv.save();
|
||||
if (plugin.config.getStoreCreative() && inv.isStored(GameMode.CREATIVE)) {
|
||||
inv.load(GameMode.CREATIVE);
|
||||
} else {
|
||||
inv.clear();
|
||||
}
|
||||
}
|
||||
public void onSetSurvival() {
|
||||
if (plugin.config.getPermissionToKeepInventory() && player.hasPermission("limitedcreative.keepinventory"))
|
||||
return;
|
||||
Inventory inv = new Inventory(player);
|
||||
if (plugin.config.getStoreCreative()) {
|
||||
inv.save();
|
||||
}
|
||||
if (inv.isStored(GameMode.SURVIVAL))
|
||||
inv.load(GameMode.SURVIVAL);
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@
|
|||
*/
|
||||
package de.jaschastarke.minecraft.limitedcreative.serialize;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
@ -72,4 +73,33 @@ public class Items implements Storeable {
|
|||
return ItemStack.deserialize(serialize);
|
||||
}
|
||||
}
|
||||
public static Map<Integer, ItemStack> storeInventory(PlayerInventory inv) {
|
||||
Map<Integer, ItemStack> map = new HashMap<Integer, ItemStack>();
|
||||
for (int i = 0; i < inv.getSize(); i++) {
|
||||
if (inv.getItem(i) != null && inv.getItem(i).getTypeId() != 0) {
|
||||
map.put(i, inv.getItem(i));
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < inv.getArmorContents().length; i++) {
|
||||
map.put((i * -1) - 1, inv.getArmorContents()[i]);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
public static void restoreInventory(PlayerInventory inv, Map<Integer, ItemStack> map) {
|
||||
for (int i = 0; i < inv.getSize(); i++) {
|
||||
if (map.containsKey(i)) {
|
||||
inv.setItem(i, map.get(i));
|
||||
} else {
|
||||
inv.setItem(i, null);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < inv.getArmorContents().length; i++) {
|
||||
int _i = (i * -1) - 1;
|
||||
if (map.containsKey(_i)) {
|
||||
inv.getArmorContents()[i] = map.get(_i);
|
||||
} else {
|
||||
inv.getArmorContents()[i] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue