Fix inventory and health issues

This commit is contained in:
mathiascode 2019-12-15 07:32:41 +02:00
parent b3fea2f1e8
commit 27c0caa435
4 changed files with 104 additions and 103 deletions

View file

@ -5,8 +5,8 @@
<version>master</version> <version>master</version>
<properties> <properties>
<maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target> <maven.compiler.target>1.7</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>

View file

@ -10,22 +10,21 @@ import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
class CommandIcu implements CommandExecutor { class CommandIcu implements CommandExecutor {
Main main;
CommandIcu(Main main) {
this.main = main;
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof ConsoleCommandSender) { if (sender instanceof ConsoleCommandSender) {
sender.sendMessage("Command has to be run by a player"); sender.sendMessage("Command has to be run by a player");
} else { } else {
Player controller = (Player) sender; final Player controller = (Player) sender;
if (args.length == 0) { if (args.length == 0) {
controller.sendMessage(ChatColor.RED + "Usage: /" + label + " <control|stop>"); controller.sendMessage(ChatColor.RED + "Usage: /" + label + " <control|stop>");
@ -33,25 +32,26 @@ class CommandIcu implements CommandExecutor {
if (args.length == 1) { if (args.length == 1) {
controller.sendMessage(ChatColor.RED + "Usage: /" + label + " control <player>"); controller.sendMessage(ChatColor.RED + "Usage: /" + label + " control <player>");
} else { } else {
Player target = Bukkit.getPlayer(args[1]); final Player target = Bukkit.getPlayer(args[1]);
if (target != null) { if (target != null) {
if (target == controller) { if (target == controller) {
controller.sendMessage("You are already controlling yourself"); controller.sendMessage("You are already controlling yourself");
} else if (main.targetFor.containsKey(controller.getUniqueId())) { } else if (Main.targetFor.containsKey(controller.getUniqueId())) {
controller.sendMessage("You are already controlling \"" + target.getName() + "\""); controller.sendMessage("You are already controlling \"" + target.getName() + "\"");
} else if (main.controllerFor.containsKey(target.getUniqueId())) { } else if (Main.controllerFor.containsKey(target.getUniqueId())) {
controller.sendMessage("Player \"" + target.getName() + "\" is already being controlled"); controller.sendMessage("Player \"" + target.getName() + "\" is already being controlled");
} else if (main.targetFor.containsKey(target.getUniqueId())) { } else if (Main.targetFor.containsKey(target.getUniqueId())) {
controller.sendMessage("Player \"" + target.getName() + "\" is already controlling another player"); controller.sendMessage("Player \"" + target.getName() + "\" is already controlling another player");
} else if (controller.canSee(target) == false) { } else if (!controller.canSee(target)) {
controller.sendMessage("You may not control this player"); controller.sendMessage("You may not control this player");
} else { } else {
controller.teleport(target); controller.teleportAsync(target.getLocation());
controller.getInventory().setContents(target.getInventory().getContents()); controller.getInventory().setContents(target.getInventory().getContents());
main.targetFor.put(controller.getUniqueId(), target); Main.targetFor.put(controller.getUniqueId(), target);
main.controllerFor.put(target.getUniqueId(), controller); Main.controllerFor.put(target.getUniqueId(), controller);
controller.sendMessage("You are now controlling \"" + target.getName() + "\""); controller.sendMessage("You are now controlling \"" + target.getName() + "\"");
} }
@ -60,30 +60,28 @@ class CommandIcu implements CommandExecutor {
} }
} }
} else if (args[0].equalsIgnoreCase("stop")) { } else if (args[0].equalsIgnoreCase("stop")) {
Player target = main.targetFor.get(controller.getUniqueId()); final Player target = Main.targetFor.get(controller.getUniqueId());
if (target != null) { if (target != null) {
main.targetFor.remove(controller.getUniqueId()); Main.targetFor.remove(controller.getUniqueId());
main.controllerFor.remove(target.getUniqueId()); Main.controllerFor.remove(target.getUniqueId());
final Player controllerRun = controller; new BukkitRunnable() {
Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
public void run() { public void run() {
for (Player player: Bukkit.getOnlinePlayers()) { for (Player player: Bukkit.getOnlinePlayers()) {
player.showPlayer(main, controllerRun); player.showPlayer(JavaPlugin.getPlugin(Main.class), controller);
} }
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.getTeam("iControlU_List"); Team team = scoreboard.getTeam("icuDisableCollision");
if (team != null && team.hasEntry(controllerRun.getName()) == true) { if (team != null && team.hasEntry(controller.getName())) {
team.removeEntry(controllerRun.getName()); team.removeEntry(controller.getName());
} }
controllerRun.removePotionEffect(PotionEffectType.INVISIBILITY); controller.removePotionEffect(PotionEffectType.INVISIBILITY);
controllerRun.sendMessage("You are now visible"); controller.sendMessage("You are now visible");
} }
}, 200L); }.runTaskLater(JavaPlugin.getPlugin(Main.class), 200);
controller.sendMessage("You are no longer controlling \"" + target.getName() + "\". You are invisible for 10 seconds."); controller.sendMessage("You are no longer controlling \"" + target.getName() + "\". You are invisible for 10 seconds.");
} else { } else {

View file

@ -18,9 +18,13 @@ import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option; import org.bukkit.scoreboard.Team.Option;
@ -29,24 +33,18 @@ import org.bukkit.scoreboard.Team.OptionStatus;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
class Tick extends BukkitRunnable { class Tick extends BukkitRunnable {
Main main;
Tick(Main main) {
this.main = main;
}
public void run() { public void run() {
for (Player target: Bukkit.getOnlinePlayers()) { for (Player target: Bukkit.getOnlinePlayers()) {
Player controller = main.controllerFor.get(target.getUniqueId()); final Player controller = Main.controllerFor.get(target.getUniqueId());
if (controller != null) { if (controller != null) {
for (int i = 0; i < 40; ++i) { for (int i = 0; i < controller.getInventory().getSize(); i++) {
if (controller.getInventory().getItem(i) != null) { if (controller.getInventory().getItem(i) != null) {
if (target.getInventory().getItem(i) != controller.getInventory().getItem(i)) { if (!controller.getInventory().getItem(i).equals(target.getInventory().getItem(i))) {
target.getInventory().setItem(i, controller.getInventory().getItem(i)); target.getInventory().setItem(i, controller.getInventory().getItem(i));
} }
} else { } else {
if (target.getInventory().getItem(i) != null) { target.getInventory().setItem(i, null);
target.getInventory().setItem(i, null);
}
} }
} }
@ -58,50 +56,56 @@ class Tick extends BukkitRunnable {
target.setExhaustion(controller.getExhaustion()); target.setExhaustion(controller.getExhaustion());
target.setFlying(controller.isFlying()); target.setFlying(controller.isFlying());
target.setFoodLevel(controller.getFoodLevel()); target.setFoodLevel(controller.getFoodLevel());
target.setMaxHealth(controller.getMaxHealth());
target.setHealth(controller.getHealth()); target.setHealth(controller.getHealth());
target.setLevel(controller.getLevel()); target.setLevel(controller.getLevel());
target.setSneaking(controller.isSneaking()); target.setSneaking(controller.isSneaking());
target.setSprinting(controller.isSprinting()); target.setSprinting(controller.isSprinting());
for (Player player: Bukkit.getOnlinePlayers()) { for (Player player: Bukkit.getOnlinePlayers()) {
player.hidePlayer(main, controller); player.hidePlayer(JavaPlugin.getPlugin(Main.class), controller);
} }
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.getTeam("iControlU_List"); Team team = scoreboard.getTeam("icuDisableCollision");
if (team == null) { if (team == null) {
team = scoreboard.registerNewTeam("iControlU_List"); team = scoreboard.registerNewTeam("icuDisableCollision");
} }
team.setCanSeeFriendlyInvisibles(false); team.setCanSeeFriendlyInvisibles(false);
team.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER); team.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
if (team.hasEntry(controller.getName()) == false) { if (!team.hasEntry(controller.getName())) {
team.addEntry(controller.getName()); team.addEntry(controller.getName());
} }
controller.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 0, false, false)); controller.addPotionEffect(
new PotionEffect(
PotionEffectType.INVISIBILITY,
99999,
0,
false,
false
)
);
} }
} }
} }
} }
class Events implements Listener { class Events implements Listener {
Main main;
Events(Main main) {
this.main = main;
}
@EventHandler @EventHandler
void onAsyncPlayerChat(AsyncPlayerChatEvent event) { void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer(); final Player player = event.getPlayer();
if (main.controllerFor.containsKey(player.getUniqueId())) { if (Main.controllerFor.containsKey(player.getUniqueId())) {
event.setCancelled(true); event.setCancelled(true);
} }
if (main.targetFor.containsKey(player.getUniqueId())) { if (Main.targetFor.containsKey(player.getUniqueId())) {
Player target = main.targetFor.get(player.getUniqueId()); final Player target = Main.targetFor.get(player.getUniqueId());
target.chat(event.getMessage()); target.chat(event.getMessage());
event.setCancelled(true); event.setCancelled(true);
} }
@ -109,105 +113,100 @@ class Events implements Listener {
@EventHandler @EventHandler
void onEntityDamage(EntityDamageEvent event) { void onEntityDamage(EntityDamageEvent event) {
Entity player = event.getEntity(); final Entity player = event.getEntity();
if (main.targetFor.containsKey(player.getUniqueId())) { if (Main.targetFor.containsKey(player.getUniqueId())) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
/*@EventHandler
void onPlayerAnimation(PlayerAnimationEvent event) {
Player controller = event.getPlayer();
Player target = main.targetFor.get(controller.getUniqueId());
if (target != null) {
event.setCancelled(true);
}
}*/
@EventHandler @EventHandler
void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer(); final Player player = event.getPlayer();
if (main.controllerFor.containsKey(player.getUniqueId())) { if (Main.controllerFor.containsKey(player.getUniqueId())) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerDropItem(PlayerDropItemEvent event) { void onPlayerDropItem(PlayerDropItemEvent event) {
Player player = event.getPlayer(); final Player player = event.getPlayer();
if (main.controllerFor.containsKey(player.getUniqueId())) { if (Main.controllerFor.containsKey(player.getUniqueId())) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerInteract(PlayerInteractEvent event) { void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer(); final Player player = event.getPlayer();
if (main.controllerFor.containsKey(player.getUniqueId())) { if (Main.controllerFor.containsKey(player.getUniqueId())) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerMove(PlayerMoveEvent event) { void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer(); final Player player = event.getPlayer();
if (main.controllerFor.containsKey(player.getUniqueId())) { if (Main.controllerFor.containsKey(player.getUniqueId())) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler @EventHandler
void onPlayerQuit(PlayerQuitEvent event) { void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer(); final Player player = event.getPlayer();
for (Player otherPlayer: Bukkit.getOnlinePlayers()) { for (Player otherPlayer: Bukkit.getOnlinePlayers()) {
/* Target disconnects */ /* Target disconnects */
if (main.controllerFor.containsKey(player.getUniqueId()) && main.controllerFor.get(player.getUniqueId()).equals(otherPlayer)) { if (Main.controllerFor.containsKey(player.getUniqueId()) &&
main.targetFor.remove(otherPlayer.getUniqueId()); Main.controllerFor.get(player.getUniqueId()).equals(otherPlayer)) {
main.controllerFor.remove(player.getUniqueId()); Main.targetFor.remove(otherPlayer.getUniqueId());
Main.controllerFor.remove(player.getUniqueId());
final Player controller = otherPlayer; final Player controller = otherPlayer;
Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() { new BukkitRunnable() {
public void run() { public void run() {
for (Player allPlayers: Bukkit.getOnlinePlayers()) { for (Player allPlayers: Bukkit.getOnlinePlayers()) {
allPlayers.showPlayer(main, controller); allPlayers.showPlayer(JavaPlugin.getPlugin(Main.class), controller);
} }
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.getTeam("iControlU_List"); final Team team = scoreboard.getTeam("icuDisableCollision");
if (team != null && team.hasEntry(controller.getName()) == true) {
if (team != null &&
team.hasEntry(controller.getName()) == true) {
team.removeEntry(controller.getName()); team.removeEntry(controller.getName());
} }
controller.removePotionEffect(PotionEffectType.INVISIBILITY); controller.removePotionEffect(PotionEffectType.INVISIBILITY);
controller.sendMessage("You are now visible"); controller.sendMessage("You are now visible");
} }
}, 200L); }.runTaskLater(JavaPlugin.getPlugin(Main.class), 200);
otherPlayer.sendMessage("The player you were controlling has disconnected. You are invisible for 10 seconds."); otherPlayer.sendMessage("The player you were controlling has disconnected. You are invisible for 10 seconds.");
} }
/* Controller disconnects */ /* Controller disconnects */
if (main.targetFor.containsKey(player.getUniqueId()) && main.targetFor.get(player.getUniqueId()).equals(otherPlayer)) { if (Main.targetFor.containsKey(player.getUniqueId()) &&
main.targetFor.remove(player.getUniqueId()); Main.targetFor.get(player.getUniqueId()).equals(otherPlayer)) {
main.controllerFor.remove(otherPlayer.getUniqueId()); Main.targetFor.remove(player.getUniqueId());
Main.controllerFor.remove(otherPlayer.getUniqueId());
} }
} }
} }
@EventHandler @EventHandler
void onPlayerRespawn(PlayerRespawnEvent event) { void onPlayerRespawn(PlayerRespawnEvent event) {
Player player = event.getPlayer(); final Player player = event.getPlayer();
if (Main.controllerFor.containsKey(player.getUniqueId())) {
final Player controller = Main.controllerFor.get(player.getUniqueId());
if (main.controllerFor.containsKey(player.getUniqueId())) {
Player controller = main.controllerFor.get(player.getUniqueId());
controller.teleportAsync(player.getLocation()); controller.teleportAsync(player.getLocation());
} }
} }

View file

@ -15,32 +15,36 @@ import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
public class Main extends JavaPlugin { public class Main extends JavaPlugin {
static HashMap<UUID, Player> controllerFor = new HashMap<UUID, Player>(); static HashMap<UUID, Player> controllerFor = new HashMap<>();
static HashMap<UUID, Player> targetFor = new HashMap<UUID, Player>(); static HashMap<UUID, Player> targetFor = new HashMap<>();
public void onEnable() { public void onEnable() {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); /* Setup scoreboard team to prevent player collisions */
Team team = scoreboard.getTeam("iControlU_List"); final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
final Team team = scoreboard.getTeam("icuDisableCollision");
if (team != null) { if (team != null) {
team.unregister(); team.unregister();
} }
this.getCommand("icu").setExecutor(new CommandIcu(this)); /* Commands */
this.getCommand("icu").setExecutor(new CommandIcu());
new Tick(this).runTaskTimer(this, 0, 1); new Tick().runTaskTimer(this, 0, 1);
this.getServer().getPluginManager().registerEvents(new Events(this), this); this.getServer().getPluginManager().registerEvents(new Events(), this);
} }
public void onDisable() { public void onDisable() {
for (Player controller: Bukkit.getOnlinePlayers()) { for (Player controller: Bukkit.getOnlinePlayers()) {
Player target = Main.targetFor.get(controller.getUniqueId()); final Player target = Main.targetFor.get(controller.getUniqueId());
if (target != null) { if (target != null) {
for (Player player: Bukkit.getOnlinePlayers()) { for (Player player: Bukkit.getOnlinePlayers()) {
player.showPlayer(this, controller); player.showPlayer(this, controller);
} }
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.getTeam("iControlU_List"); final Team team = scoreboard.getTeam("icuDisableCollision");
if (team != null) { if (team != null) {
team.unregister(); team.unregister();
} }