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>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>

View File

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

View File

@ -18,9 +18,13 @@ import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option;
@ -29,24 +33,18 @@ import org.bukkit.scoreboard.Team.OptionStatus;
import org.bukkit.scheduler.BukkitRunnable;
class Tick extends BukkitRunnable {
Main main;
Tick(Main main) {
this.main = main;
}
public void run() {
for (Player target: Bukkit.getOnlinePlayers()) {
Player controller = main.controllerFor.get(target.getUniqueId());
final Player controller = Main.controllerFor.get(target.getUniqueId());
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 (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));
}
} 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.setFlying(controller.isFlying());
target.setFoodLevel(controller.getFoodLevel());
target.setMaxHealth(controller.getMaxHealth());
target.setHealth(controller.getHealth());
target.setLevel(controller.getLevel());
target.setSneaking(controller.isSneaking());
target.setSprinting(controller.isSprinting());
for (Player player: Bukkit.getOnlinePlayers()) {
player.hidePlayer(main, controller);
player.hidePlayer(JavaPlugin.getPlugin(Main.class), controller);
}
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.getTeam("iControlU_List");
final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.getTeam("icuDisableCollision");
if (team == null) {
team = scoreboard.registerNewTeam("iControlU_List");
team = scoreboard.registerNewTeam("icuDisableCollision");
}
team.setCanSeeFriendlyInvisibles(false);
team.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
if (team.hasEntry(controller.getName()) == false) {
if (!team.hasEntry(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 {
Main main;
Events(Main main) {
this.main = main;
}
@EventHandler
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);
}
if (main.targetFor.containsKey(player.getUniqueId())) {
Player target = main.targetFor.get(player.getUniqueId());
if (Main.targetFor.containsKey(player.getUniqueId())) {
final Player target = Main.targetFor.get(player.getUniqueId());
target.chat(event.getMessage());
event.setCancelled(true);
}
@ -109,105 +113,100 @@ class Events implements Listener {
@EventHandler
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);
}
}
/*@EventHandler
void onPlayerAnimation(PlayerAnimationEvent event) {
Player controller = event.getPlayer();
Player target = main.targetFor.get(controller.getUniqueId());
if (target != null) {
event.setCancelled(true);
}
}*/
@EventHandler
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);
}
}
@EventHandler
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);
}
}
@EventHandler
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);
}
}
@EventHandler
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);
}
}
@EventHandler
void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
final Player player = event.getPlayer();
for (Player otherPlayer: Bukkit.getOnlinePlayers()) {
/* Target disconnects */
if (main.controllerFor.containsKey(player.getUniqueId()) && main.controllerFor.get(player.getUniqueId()).equals(otherPlayer)) {
main.targetFor.remove(otherPlayer.getUniqueId());
main.controllerFor.remove(player.getUniqueId());
if (Main.controllerFor.containsKey(player.getUniqueId()) &&
Main.controllerFor.get(player.getUniqueId()).equals(otherPlayer)) {
Main.targetFor.remove(otherPlayer.getUniqueId());
Main.controllerFor.remove(player.getUniqueId());
final Player controller = otherPlayer;
Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
new BukkitRunnable() {
public void run() {
for (Player allPlayers: Bukkit.getOnlinePlayers()) {
allPlayers.showPlayer(main, controller);
allPlayers.showPlayer(JavaPlugin.getPlugin(Main.class), controller);
}
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.getTeam("iControlU_List");
if (team != null && team.hasEntry(controller.getName()) == true) {
final Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
final Team team = scoreboard.getTeam("icuDisableCollision");
if (team != null &&
team.hasEntry(controller.getName()) == true) {
team.removeEntry(controller.getName());
}
controller.removePotionEffect(PotionEffectType.INVISIBILITY);
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.");
}
/* Controller disconnects */
if (main.targetFor.containsKey(player.getUniqueId()) && main.targetFor.get(player.getUniqueId()).equals(otherPlayer)) {
main.targetFor.remove(player.getUniqueId());
main.controllerFor.remove(otherPlayer.getUniqueId());
if (Main.targetFor.containsKey(player.getUniqueId()) &&
Main.targetFor.get(player.getUniqueId()).equals(otherPlayer)) {
Main.targetFor.remove(player.getUniqueId());
Main.controllerFor.remove(otherPlayer.getUniqueId());
}
}
}
@EventHandler
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());
}
}

View File

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