From 64b2beac27bd092c3eb19417005be84caaa80d41 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Sat, 25 Feb 2012 18:27:30 +0100 Subject: [PATCH] (still in) v0.9.5-beta: - Optimized Player Respawn --- .../minecraft/limitedcreative/LCPlayer.java | 34 +++++---- .../minecraft/limitedcreative/Players.java | 6 +- .../listeners/MainListener.java | 5 ++ .../regions/RegionListener.java | 4 +- .../minecraft/worldguard/CListener.java | 10 +++ .../events/PlayerChangedAreaEvent.java | 32 +-------- .../events/PlayerNewLocationAreaEvent.java | 70 +++++++++++++++++++ 7 files changed, 113 insertions(+), 48 deletions(-) create mode 100644 src/de/jaschastarke/minecraft/worldguard/events/PlayerNewLocationAreaEvent.java diff --git a/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java b/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java index 136b9ce..67ca9be 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java @@ -51,32 +51,32 @@ import de.jaschastarke.minecraft.worldguard.events.PlayerChangedAreaEvent; public class LCPlayer { private static Core plugin = Core.plugin; - //private Player player; - private String name; + private Player player; + //private String name; private Inventory _inv; private GameMode _permanent_gamemode = null; - private long _timestamp; + //private long _timestamp; public LCPlayer(Player player) { - //this.player = player; - name = player.getName(); - touch(); + this.player = player; + //name = player.getName(); + //touch(); if (!this.isRegionGameMode(player.getGameMode())) { setPermanentGameMode(player.getGameMode()); } } - /*private void updatePlayer(Player player) { + + public void updatePlayer(Player player) { this.player = player; - _inv = null; - }*/ + } public Player getPlayer() { - //return player; - return plugin.getServer().getPlayerExact(name); + return player; + //return plugin.getServer().getPlayerExact(name); (doesn't work will revive) } public String getName() { - return name; + return player.getName(); } public Inventory getInv() { @@ -85,13 +85,13 @@ public class LCPlayer { return _inv; } - public void touch() { + /*public void touch() { _timestamp = System.currentTimeMillis(); } public boolean isOutdated() { return (getPlayer() == null || !getPlayer().isOnline()) && _timestamp < (System.currentTimeMillis() - Players.CLEANUP_TIMEOUT); - } + }*/ private Map options = new HashMap(); public void setRegionGameMode(final GameMode gm) { @@ -208,6 +208,12 @@ public class LCPlayer { } return true; } + + public void onRevive() { + if (getPlayer().getGameMode() == GameMode.CREATIVE) { + setCreativeArmor(); + } + } public void setCreativeArmor() { Map armor = plugin.config.getCreativeArmor(); diff --git a/src/de/jaschastarke/minecraft/limitedcreative/Players.java b/src/de/jaschastarke/minecraft/limitedcreative/Players.java index 0468ed0..1882cb6 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/Players.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/Players.java @@ -37,9 +37,9 @@ public class Players { return p; } else { LCPlayer p = players.get(player.getName()); - /*if (player != p.getPlayer()) - p.updatePlayer(player);*/ - p.touch(); + if (player != p.getPlayer()) + p.updatePlayer(player); + //p.touch(); return p; } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/listeners/MainListener.java b/src/de/jaschastarke/minecraft/limitedcreative/listeners/MainListener.java index d694668..106126a 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/listeners/MainListener.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/listeners/MainListener.java @@ -25,6 +25,7 @@ import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerRespawnEvent; import de.jaschastarke.minecraft.limitedcreative.Core; import de.jaschastarke.minecraft.limitedcreative.Players; @@ -60,6 +61,10 @@ public class MainListener implements Listener { if (!Players.get(event.getPlayer()).onSetGameMode(event.getNewGameMode())) event.setCancelled(true); } + @EventHandler + public void onPlayerRespawn(PlayerRespawnEvent event) { + Players.get(event.getPlayer()).onRevive(); + } /** * Also needed if WorldGuard-Feature is enabled, so can not moved to optional Listener "Limit". diff --git a/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java b/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java index fb62fbf..ae0dd72 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java @@ -21,7 +21,7 @@ import de.jaschastarke.minecraft.limitedcreative.Players; import de.jaschastarke.minecraft.utils.Util; import de.jaschastarke.minecraft.worldguard.ApplicableRegions; import de.jaschastarke.minecraft.worldguard.CRegionManager; -import de.jaschastarke.minecraft.worldguard.events.PlayerChangedAreaEvent; +import de.jaschastarke.minecraft.worldguard.events.PlayerNewLocationAreaEvent; import de.jaschastarke.minecraft.worldguard.events.PlayerSetAreaEvent; import de.jaschastarke.minecraft.worldguard.events.PlayerUpdateAreaEvent; @@ -92,7 +92,7 @@ public class RegionListener implements Listener { } @EventHandler - public void onPlayerChangedArea(PlayerChangedAreaEvent event) { + public void onPlayerChangedArea(PlayerNewLocationAreaEvent event) { Players.get(event.getPlayer()).setRegionCreativeAllowed(event.getRegionSet().allows(Flags.CREATIVE, event.getPlayer()), event); } diff --git a/src/de/jaschastarke/minecraft/worldguard/CListener.java b/src/de/jaschastarke/minecraft/worldguard/CListener.java index c351355..ca956a0 100644 --- a/src/de/jaschastarke/minecraft/worldguard/CListener.java +++ b/src/de/jaschastarke/minecraft/worldguard/CListener.java @@ -25,9 +25,11 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; import de.jaschastarke.minecraft.worldguard.events.PlayerChangedAreaEvent; +import de.jaschastarke.minecraft.worldguard.events.PlayerNewLocationAreaEvent; import de.jaschastarke.minecraft.worldguard.events.PlayerSetAreaEvent; public class CListener implements Listener { @@ -65,6 +67,14 @@ public class CListener implements Listener { onPlayerMove(event); } + @EventHandler + public void onPlayerRespawn(PlayerRespawnEvent event) { + String new_hash = com.getRegionManager().getRegionsHash(event.getRespawnLocation()); + PlayerNewLocationAreaEvent areaevent = new PlayerNewLocationAreaEvent(event.getPlayer(), event.getRespawnLocation(), new_hash); + Bukkit.getServer().getPluginManager().callEvent(areaevent); + CPlayer.get(event.getPlayer()).setHash(new_hash); + } + @EventHandler public void onPlayerLogin(PlayerJoinEvent event) { String new_hash = com.getRegionManager().getRegionsHash(event.getPlayer().getLocation()); diff --git a/src/de/jaschastarke/minecraft/worldguard/events/PlayerChangedAreaEvent.java b/src/de/jaschastarke/minecraft/worldguard/events/PlayerChangedAreaEvent.java index 62a7a27..f9a20f8 100644 --- a/src/de/jaschastarke/minecraft/worldguard/events/PlayerChangedAreaEvent.java +++ b/src/de/jaschastarke/minecraft/worldguard/events/PlayerChangedAreaEvent.java @@ -17,9 +17,7 @@ */ package de.jaschastarke.minecraft.worldguard.events; -import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; -import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerTeleportEvent; @@ -27,34 +25,19 @@ import de.jaschastarke.minecraft.worldguard.ApplicableRegions; import de.jaschastarke.minecraft.worldguard.Interface; @SuppressWarnings("serial") -public class PlayerChangedAreaEvent extends PlayerAreaEvent implements Cancellable { +public class PlayerChangedAreaEvent extends PlayerNewLocationAreaEvent implements Cancellable { private PlayerMoveEvent event; private String _previous_hash; - private String _hash; private boolean _cancelled = false; public PlayerChangedAreaEvent(PlayerMoveEvent moveevent) { + super(moveevent.getPlayer(), moveevent.getTo()); event = moveevent; } public PlayerChangedAreaEvent(PlayerMoveEvent moveevent, String previous_hash, String new_hash) { + super(moveevent.getPlayer(), moveevent.getTo(), new_hash); event = moveevent; _previous_hash = previous_hash; - _hash = new_hash; - } - - @Override - public String getRegionHash() { - if (_hash == null) - _hash = Interface.getInstance().getRegionManager().getRegionsHash(event.getTo()); - return _hash; - } - @Override - public ApplicableRegions getRegionSet() { - return Interface.getInstance().getRegionManager().getRegionSet(event.getTo()); - } - - public Player getPlayer() { - return event.getPlayer(); } public boolean isTeleport() { @@ -79,15 +62,6 @@ public class PlayerChangedAreaEvent extends PlayerAreaEvent implements Cancellab return getClass().getSimpleName()+"["+getPreviousRegionHash()+" -> "+getRegionHash()+"]"; } - private static final HandlerList handlers = new HandlerList(); - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } @Override public boolean isCancelled() { return _cancelled; diff --git a/src/de/jaschastarke/minecraft/worldguard/events/PlayerNewLocationAreaEvent.java b/src/de/jaschastarke/minecraft/worldguard/events/PlayerNewLocationAreaEvent.java new file mode 100644 index 0000000..26adfb0 --- /dev/null +++ b/src/de/jaschastarke/minecraft/worldguard/events/PlayerNewLocationAreaEvent.java @@ -0,0 +1,70 @@ +/* + * 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 . + */ +package de.jaschastarke.minecraft.worldguard.events; + +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; + +import de.jaschastarke.minecraft.worldguard.ApplicableRegions; +import de.jaschastarke.minecraft.worldguard.Interface; + +@SuppressWarnings("serial") +public class PlayerNewLocationAreaEvent extends PlayerAreaEvent { + private Location location; + private Player player; + private String _hash; + + public PlayerNewLocationAreaEvent(Player player, Location new_location) { + this.player = player; + location = new_location; + } + public PlayerNewLocationAreaEvent(Player player, Location new_location, String new_hash) { + this(player, new_location); + _hash = new_hash; + } + + @Override + public String getRegionHash() { + if (_hash == null) + _hash = Interface.getInstance().getRegionManager().getRegionsHash(location); + return _hash; + } + @Override + public ApplicableRegions getRegionSet() { + return Interface.getInstance().getRegionManager().getRegionSet(location); + } + + public Player getPlayer() { + return player; + } + + public String toString() { + return getClass().getSimpleName()+"["+getRegionHash()+"]"; + } + + private static final HandlerList handlers = new HandlerList(); + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +}