From 05ca15ef0cf1fd88cdfb993d7ab87e0dd961a8a6 Mon Sep 17 00:00:00 2001 From: NK Date: Mon, 21 Nov 2022 20:30:00 +0000 Subject: [PATCH] mining faster update + npc --- src/main/java/me/night/nullvalkyrie/Main.java | 4 +- .../nullvalkyrie/commands/AnvilCommand.java | 13 ++- .../nullvalkyrie/commands/BetaCommand.java | 22 ++--- .../nullvalkyrie/events/CustomItemEvents.java | 29 ++++--- .../me/night/nullvalkyrie/items/Pickaxe.java | 26 ++++++ .../java/me/night/nullvalkyrie/npc/NPC.java | 87 +++++++++++++++++++ .../java/me/night/nullvalkyrie/npc/Skin.java | 49 +++++++++++ .../nullvalkyrie/rank/ScoreboardListener.java | 8 +- 8 files changed, 198 insertions(+), 40 deletions(-) create mode 100644 src/main/java/me/night/nullvalkyrie/items/Pickaxe.java create mode 100644 src/main/java/me/night/nullvalkyrie/npc/NPC.java create mode 100644 src/main/java/me/night/nullvalkyrie/npc/Skin.java diff --git a/src/main/java/me/night/nullvalkyrie/Main.java b/src/main/java/me/night/nullvalkyrie/Main.java index 89b33aa..3067e4f 100644 --- a/src/main/java/me/night/nullvalkyrie/Main.java +++ b/src/main/java/me/night/nullvalkyrie/Main.java @@ -8,13 +8,12 @@ import me.night.nullvalkyrie.events.CustomItemEvents; import me.night.nullvalkyrie.events.DamageEffect; import me.night.nullvalkyrie.hardpoint.ConfigManager; import me.night.nullvalkyrie.items.CustomItemManager; +import me.night.nullvalkyrie.npc.NPC; import me.night.nullvalkyrie.rank.ScoreboardListener; import me.night.nullvalkyrie.util.Util; import me.night.nullvalkyrie.commands.*; import me.night.nullvalkyrie.database.Client; import me.night.nullvalkyrie.miners.CryptoMiner; -import net.md_5.bungee.api.ChatMessageType; -import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.*; import org.bukkit.boss.BarColor; import org.bukkit.boss.BarStyle; @@ -37,6 +36,7 @@ public final class Main extends JavaPlugin implements Listener { public void onEnable() { getConfig().options().copyDefaults(); saveDefaultConfig(); + new NPC(this); EnchantmentManager.register(); new CustomItemManager(this); updateYamlFilesToPlugin("shop.yml"); diff --git a/src/main/java/me/night/nullvalkyrie/commands/AnvilCommand.java b/src/main/java/me/night/nullvalkyrie/commands/AnvilCommand.java index fb125b1..750697b 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/AnvilCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/AnvilCommand.java @@ -2,12 +2,11 @@ package me.night.nullvalkyrie.commands; import net.minecraft.network.protocol.game.PacketPlayOutOpenWindow; import net.minecraft.server.level.EntityPlayer; -import net.minecraft.server.network.PlayerConnection; import net.minecraft.world.inventory.Containers; - import org.bukkit.command.CommandSender; import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_19_R1.util.CraftChatMessage; + import org.bukkit.entity.Player; import java.util.List; @@ -28,11 +27,11 @@ public class AnvilCommand extends Command { if (sender instanceof Player) { Player player = (Player) sender; CraftPlayer craftPlayer = (CraftPlayer) player; - EntityPlayer entityPlayer = craftPlayer.getHandle(); - int id = 0; - PacketPlayOutOpenWindow OpenWindow = new PacketPlayOutOpenWindow(id, Containers.h, CraftChatMessage.fromStringOrNull("Test")); - PlayerConnection playerConnection = entityPlayer.b; - playerConnection.a(OpenWindow); +// EntityPlayer entityPlayer = craftPlayer.getHandle(); +// int id = 0; +// PacketPlayOutOpenWindow OpenWindow = new PacketPlayOutOpenWindow(id, Containers.h, CraftChatMessage.fromStringOrNull("Test")); +// PlayerConnection playerConnection = entityPlayer.b; +// playerConnection.a(OpenWindow); } } diff --git a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java index b0c4ccd..05b9a3b 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java @@ -1,41 +1,29 @@ package me.night.nullvalkyrie.commands; import me.night.nullvalkyrie.Main; -import net.minecraft.network.protocol.game.PacketPlayOutEntityHeadRotation; -import net.minecraft.server.level.EntityPlayer; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; +import me.night.nullvalkyrie.npc.NPC; import org.bukkit.command.CommandSender; -import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import java.util.List; - public class BetaCommand extends Command { private Main main; public BetaCommand(Main main) { - super("beta", new String[]{"b"}, "Beta", ""); + super("beta", new String[]{"b", "npc"}, "Beta", ""); this.main = main; } @Override public void onCommand(CommandSender sender, String[] args) { if (sender instanceof Player) { - Player pa = (Player) sender; -// CraftPlayer cp = (CraftPlayer) sender; -// EntityPlayer ep = cp.getHandle(); -// PacketPlayOutUpdateHealth packet = new PacketPlayOutUpdateHealth(20f, 20, 5.0f); // health, food, food saturation -// ep.b.a(packet); // Sends the Packet + Player player = (Player) sender; + NPC.createNPC(player, "&a&lNK"); } } - @Override public List onTabComplete(CommandSender sender, String[] args) { return null; } -} +} \ No newline at end of file diff --git a/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java b/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java index 08595ec..4bf48e3 100644 --- a/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java +++ b/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java @@ -8,6 +8,7 @@ import com.comphenix.protocol.wrappers.BlockPosition; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import me.night.nullvalkyrie.items.CustomItemManager; +import me.night.nullvalkyrie.items.Pickaxe; import me.night.nullvalkyrie.items.Rarity; import me.night.nullvalkyrie.Main; import net.md_5.bungee.api.ChatMessageType; @@ -321,19 +322,26 @@ public class CustomItemEvents implements Listener { // System.out.println(e.getEntity().getLocation()); // e.getEntity().setCustomName(ChatColor.RED + "Changed name since you ust clicked lol"); // } - - private final Cache BreakAbility = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.MILLISECONDS).build(); private final HashMap blockStages = new HashMap<>(); - + private final HashMap miningCooldown = new HashMap<>(); @EventHandler - public void onAnimationEvent(PlayerAnimationEvent e) { + public void onAnimationEvent(PlayerAnimationEvent e) { //Material blockType, int mineInterval, Pickaxe x Player player = e.getPlayer(); + UUID uuid = player.getUniqueId(); if (!player.getGameMode().equals(GameMode.SURVIVAL)) return; - if (BreakAbility.asMap().containsKey(player.getUniqueId())) return; + if (miningCooldown.containsKey(uuid) && (miningCooldown.get(uuid) > System.currentTimeMillis())) return; + else miningCooldown.remove(uuid); + if (!e.getAnimationType().equals(PlayerAnimationType.ARM_SWING)) return; Block block = player.getTargetBlockExact(4); if (block == null) return; - BreakAbility.put(player.getUniqueId(), System.currentTimeMillis() + 60); + + Pickaxe pickaxe = new Pickaxe(player.getInventory().getItemInMainHand()); + List materialsThatCanBeMinedFast = pickaxe.multimap.get(pickaxe.getMaterial()); // to get all materials that the pickaxe can mine + if (!materialsThatCanBeMinedFast.contains(block.getType())) return; + + long miningPerPhase = pickaxe.getMiningPerPhase(block.getType()); + miningCooldown.put(uuid, System.currentTimeMillis() + miningPerPhase); int blockStage = blockStages.getOrDefault(block.getLocation(), 0); blockStage = blockStage == 10 ? 0 : blockStage + 1; blockStages.put(block.getLocation(), blockStage); @@ -343,9 +351,10 @@ public class CustomItemEvents implements Listener { block.breakNaturally(); } } - ProtocolManager manager = ProtocolLibrary.getProtocolManager(); - public void sendBlockDamage(Player player, Location location) { + + public void sendBlockDamage(Player player, Block block) { + Location location = block.getLocation(); int locationId = location.getBlockX() + location.getBlockY() + location.getBlockZ(); PacketContainer packet = manager.createPacket(PacketType.Play.Server.BLOCK_BREAK_ANIMATION); packet.getIntegers().write(0, locationId); // set entity ID to the location @@ -357,8 +366,4 @@ public class CustomItemEvents implements Listener { e.printStackTrace(); } } - - public void sendBlockDamage(Player player, Block block) { - sendBlockDamage(player, block.getLocation()); - } } \ No newline at end of file diff --git a/src/main/java/me/night/nullvalkyrie/items/Pickaxe.java b/src/main/java/me/night/nullvalkyrie/items/Pickaxe.java new file mode 100644 index 0000000..abe63d8 --- /dev/null +++ b/src/main/java/me/night/nullvalkyrie/items/Pickaxe.java @@ -0,0 +1,26 @@ +package me.night.nullvalkyrie.items; + +import com.google.common.collect.ArrayListMultimap; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; + +public class Pickaxe { + public ArrayListMultimap multimap = ArrayListMultimap.create(); + public HashMap phases = new HashMap<>(); + private final ItemStack itemStack; + public Pickaxe(ItemStack item) { + multimap.put(Material.STONE_PICKAXE, Material.IRON_ORE); //put some blocks and pickaxe to mine + multimap.put(Material.STONE_PICKAXE, Material.DIAMOND_ORE); + phases.put(Material.DIAMOND_ORE, 40L); + phases.put(Material.IRON_ORE, 30L); + itemStack = item; + } + public long getMiningPerPhase(Material material) { + return phases.get(material); + } + public Material getMaterial() { + return itemStack.getType(); + } +} diff --git a/src/main/java/me/night/nullvalkyrie/npc/NPC.java b/src/main/java/me/night/nullvalkyrie/npc/NPC.java new file mode 100644 index 0000000..53e806b --- /dev/null +++ b/src/main/java/me/night/nullvalkyrie/npc/NPC.java @@ -0,0 +1,87 @@ +package me.night.nullvalkyrie.npc; + +import com.mojang.authlib.GameProfile; +import com.mojang.authlib.properties.Property; +import com.mojang.datafixers.util.Pair; +import me.night.nullvalkyrie.Main; +import me.night.nullvalkyrie.util.Util; +import net.minecraft.network.protocol.game.*; +import net.minecraft.network.syncher.DataWatcher; +import net.minecraft.network.syncher.DataWatcherObject; +import net.minecraft.network.syncher.DataWatcherRegistry; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.EntityPlayer; +import net.minecraft.server.level.WorldServer; +import net.minecraft.server.network.PlayerConnection; +import net.minecraft.world.entity.EnumItemSlot; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class NPC { + private static Main main; + public NPC(Main main) { + NPC.main = main; + } + private static final List NPCs = new ArrayList<>(); + public static List getNPCs() { + return NPCs; + } + public static void createNPC(Player player, String name) { //name must be less than 16 characters including color codes ** + CraftPlayer creaftPlayer = (CraftPlayer) player; + EntityPlayer sp = creaftPlayer.getHandle(); + MinecraftServer server = sp.c; + WorldServer level = ((CraftWorld) Bukkit.getWorld(player.getWorld().getName())).getHandle(); + GameProfile gameProfile = new GameProfile(UUID.randomUUID(), Util.color(name)); + String[] skin = Skin.getSkin("Lendortv"); + gameProfile.getProperties().put("textures", new Property("textures", skin[0], skin[1])); + EntityPlayer npc = new EntityPlayer(server, level, gameProfile, null); + Location location = player.getLocation(); + npc.a(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); + addNPCPacket(npc); + NPCs.add(npc); + } + public static void addNPCPacket(EntityPlayer npc) { + for(Player player : Bukkit.getOnlinePlayers()) { + CraftPlayer creaftPlayer = (CraftPlayer) player; + EntityPlayer sp = creaftPlayer.getHandle(); + PlayerConnection pc = sp.b; + pc.a(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a, npc)); + pc.a(new PacketPlayOutNamedEntitySpawn(npc)); + pc.a(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.getBukkitYaw() * 256 / 360))); + DataWatcher watcher = npc.ai(); + watcher.b(new DataWatcherObject<>(17, DataWatcherRegistry.a), (byte) 127); + pc.a(new PacketPlayOutEntityMetadata(npc.ae(), watcher, true)); + Bukkit.getScheduler().runTaskLaterAsynchronously(main, () -> pc.a(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, npc)), 50); + ItemStack netheriteAxe = new ItemStack(Material.NETHERITE_AXE); + ItemStack anotherAxe = new ItemStack(Material.NETHERITE_INGOT); + List> list = new ArrayList<>(); + list.add(new Pair<>(EnumItemSlot.a, CraftItemStack.asNMSCopy(netheriteAxe))); + list.add(new Pair<>(EnumItemSlot.b, CraftItemStack.asNMSCopy(anotherAxe))); + pc.a(new PacketPlayOutEntityEquipment(npc.ae(), list)); + } + } + public static void addJoinPacket(Player player) { + for(EntityPlayer npc : NPCs) { + CraftPlayer creaftPlayer = (CraftPlayer) player; + EntityPlayer sp = creaftPlayer.getHandle(); + PlayerConnection pc = sp.b; + pc.a(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a, npc)); + pc.a(new PacketPlayOutNamedEntitySpawn(npc)); + pc.a(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.getBukkitYaw() * 256 / 360))); + DataWatcher watcher = npc.ai(); + watcher.b(new DataWatcherObject<>(17, DataWatcherRegistry.a), (byte) 127); + pc.a(new PacketPlayOutEntityMetadata(npc.ae(), watcher, true)); + Bukkit.getScheduler().runTaskLaterAsynchronously(main, () -> pc.a(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, npc)), 50); + } + } +} diff --git a/src/main/java/me/night/nullvalkyrie/npc/Skin.java b/src/main/java/me/night/nullvalkyrie/npc/Skin.java new file mode 100644 index 0000000..567b0e2 --- /dev/null +++ b/src/main/java/me/night/nullvalkyrie/npc/Skin.java @@ -0,0 +1,49 @@ +package me.night.nullvalkyrie.npc; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.mojang.authlib.GameProfile; +import com.mojang.authlib.properties.Property; +import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import java.io.InputStreamReader; +import java.net.URL; + +public class Skin { + private String texture; + private String signature; + public Skin(String texture, String signature) { + this.texture = texture; + this.signature = signature; + } + public String getTexture() { return texture; } + public String getSignature() { return signature; } + + public static String[] getSkin(String name) { + try { + URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + name); + InputStreamReader reader = new InputStreamReader(url.openStream()); + String uuid = new JsonParser().parse(reader).getAsJsonObject().get("id").getAsString(); + URL url2 = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false"); + InputStreamReader reader2 = new InputStreamReader(url2.openStream()); + JsonObject properties = new JsonParser().parse(reader2).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject(); + String texture = properties.get("value").getAsString(); + String signature = properties.get("signature").getAsString(); + return new String[] {texture, signature}; + } catch (Exception e) { + return new String[]{}; + } + } + public static String[] getSkin(Player player) { + try { + GameProfile profile = ((CraftPlayer) player).getProfile(); + Property property = profile.getProperties().get("textures").iterator().next(); + String texture = property.getValue(); + String signature = property.getSignature(); + return new String[]{texture, signature}; + } catch (Exception e) { + return new String[]{}; + } + } +} diff --git a/src/main/java/me/night/nullvalkyrie/rank/ScoreboardListener.java b/src/main/java/me/night/nullvalkyrie/rank/ScoreboardListener.java index aec4647..acc1e03 100644 --- a/src/main/java/me/night/nullvalkyrie/rank/ScoreboardListener.java +++ b/src/main/java/me/night/nullvalkyrie/rank/ScoreboardListener.java @@ -1,6 +1,7 @@ package me.night.nullvalkyrie.rank; import me.night.nullvalkyrie.Main; +import me.night.nullvalkyrie.npc.NPC; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -17,8 +18,8 @@ public class ScoreboardListener implements Listener { public static RankManager rankManager; public static NameTagManager nameTagManager; - private SideBarManager sideBarManager; - private BelowNameManager belowNameManager; + private final SideBarManager sideBarManager; + private final BelowNameManager belowNameManager; public ScoreboardListener(Main main) { nameTagManager = new NameTagManager(main); rankManager = new RankManager(main); @@ -41,6 +42,9 @@ public class ScoreboardListener implements Listener { sideBarManager.start(player); belowNameManager.setBelowName(player); e.setJoinMessage(rankManager.getRank(e.getPlayer().getUniqueId()).getDisplay() + " " + e.getPlayer().getName() + ChatColor.WHITE + " joined the server!"); + if(NPC.getNPCs() == null) return; + if(NPC.getNPCs().isEmpty()) return; + NPC.addJoinPacket(e.getPlayer()); } @EventHandler