Skip to content

Commit

Permalink
full and correct 1.16.1 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
7isenko committed Jul 21, 2020
1 parent e13b410 commit 8587de5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 49 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 7isenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
50 changes: 20 additions & 30 deletions src/main/java/io/github/_7isenko/dreamcompass/CompassCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github._7isenko.dreamcompass;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand All @@ -14,38 +12,30 @@

public class CompassCommand implements CommandExecutor {
@Override
@SuppressWarnings("ConstantConditions")
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player))
if (!(sender instanceof Player)) {
sender.sendMessage("This command is available only for players");
return false;

if (args.length == 0) {
sendMessageWithColors(sender, "&c&lUse /compass <player>");
return true;
}
if (args.length != 1) return false;

if (args.length == 1) {
Player player = (Player) sender;
String name = args[0];
Player target = Bukkit.getPlayer(name);
if (target != null) {
DreamCompass.hunters.put(player, name);
ItemStack compass = new ItemStack(Material.COMPASS);
CompassMeta meta = (CompassMeta) compass.getItemMeta();
meta.setLodestone(target.getLocation());
meta.setLodestoneTracked(false);
meta.setDisplayName(target.getName());
compass.setItemMeta(meta);
player.getInventory().addItem(compass);
sender.sendMessage(ChatColor.GOLD + "Compass is pointing to " + name);
return true;
}
sendMessageWithColors(sender, "&c&lPlayer &4&l" + name + "&c&l is offline");
return true;
}
return false;
}
Player player = (Player) sender;
String name = args[0];
Player target = Bukkit.getPlayer(name);

private void sendMessageWithColors(CommandSender sender, String message) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
ItemStack compass = new ItemStack(Material.COMPASS);
CompassMeta meta = (CompassMeta) compass.getItemMeta();
meta.setDisplayName(name);
if (target != null) {
meta.setLodestoneTracked(false);
meta.setLodestone(target.getLocation());
sender.sendMessage(ChatColor.GOLD + "New compass is pointing to " + name);
} else
sender.sendMessage(ChatColor.GOLD + "New compass is pointing to " + name + ", but this player is offline");
compass.setItemMeta(meta);
player.getInventory().addItem(compass);
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.bukkit.inventory.meta.CompassMeta;

public class CompassHelper {
@SuppressWarnings("ConstantConditions")
public static void setTarget(ItemStack compass, Player target) {
if (target != null) {
CompassMeta meta = (CompassMeta) compass.getItemMeta();
meta.setLodestone(target.getLocation());
meta.setLodestoneTracked(false);
compass.setItemMeta(meta);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,33 @@
import org.bukkit.inventory.ItemStack;

public class CompassInteractListener implements Listener {

@EventHandler
@SuppressWarnings({"ConstantConditions", "unused"})
public void onEvent(PlayerInteractEvent e) {
if (DreamCompass.hunters.containsKey(e.getPlayer())) {
Player player = e.getPlayer();
Player target;
ItemStack compass;
if (e.getPlayer().getInventory().getItemInMainHand().getType() == Material.COMPASS) {
target = Bukkit.getPlayer(player.getInventory().getItemInMainHand().getItemMeta().getDisplayName());
compass = player.getInventory().getItemInMainHand();
} else if (e.getPlayer().getInventory().getItemInOffHand().getType() == Material.COMPASS) {
target = Bukkit.getPlayer(player.getInventory().getItemInOffHand().getItemMeta().getDisplayName());
compass = player.getInventory().getItemInOffHand();
} else return;
Player player = e.getPlayer();
ItemStack compass = getCompass(player);
if (compass == null) return;
if (!compass.getItemMeta().hasDisplayName()) return;
String name = compass.getItemMeta().getDisplayName();
Player target = Bukkit.getPlayer(name);
if (target != null) {
CompassHelper.setTarget(compass, target);
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatColor.translateAlternateColorCodes('&', "&3&bCompass is pointing to " + compass.getItemMeta().getDisplayName())));
sendToActionBar(player, "&3&bCompass is pointing to " + name);
} else sendToActionBar(player, "&3&b" + name + " is offline");

}

private ItemStack getCompass(Player player) {
ItemStack compass = null;
if (player.getInventory().getItemInMainHand().getType() == Material.COMPASS) {
compass = player.getInventory().getItemInMainHand();
} else if (player.getInventory().getItemInOffHand().getType() == Material.COMPASS) {
compass = player.getInventory().getItemInOffHand();
}
return compass;
}

private void sendToActionBar(Player player, String message) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatColor.translateAlternateColorCodes('&', message)));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.github._7isenko.dreamcompass;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Objects;

public final class DreamCompass extends JavaPlugin implements Listener {
public static HashMap<Player, String> hunters = new HashMap<>();
@SuppressWarnings("unused")
public final class DreamCompass extends JavaPlugin {

@Override
public void onEnable() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: DreamCompass
version: "3.0"
version: "2.0"
main: io.github._7isenko.dreamcompass.DreamCompass
authors: [prosteDeni, 7isenko]
author: 7isenko
description: Gives you compass always pointing to specified player
commands:
compass:
Expand Down

0 comments on commit 8587de5

Please sign in to comment.