diff --git a/src/main/java/tech/nully/BossBarAPI/BossBar.java b/src/main/java/tech/nully/BossBarAPI/BossBar.java index e0b96eb..12408ff 100644 --- a/src/main/java/tech/nully/BossBarAPI/BossBar.java +++ b/src/main/java/tech/nully/BossBarAPI/BossBar.java @@ -6,10 +6,10 @@ import org.bukkit.Location; import org.bukkit.entity.Player; public class BossBar { - private int bossHealth = 200; + private int bossHealth = 300; private String text = "A BossBar!"; - private SpawnFakeWither.FakeWither dragon; + private SpawnFakeWither.FakeWither wither; private Player p; @@ -25,9 +25,9 @@ public class BossBar { public void setHealth(int bossHealth) { this.bossHealth = bossHealth; - if (dragon != null) { - if (dragon.created) { - dragon.setHealth(bossHealth); + if (wither != null) { + if (wither.created) { + wither.setHealth(bossHealth); } } } @@ -38,34 +38,35 @@ public class BossBar { public void setText(String text) { this.text = text; - if (dragon != null) { - if (dragon.created) { - dragon.setCustomName(text); + if (wither != null) { + if (wither.created) { + wither.setCustomName(text); } } } public void display() { - if (dragon != null) { - if (dragon.created) { - dragon.destroy(); + if (wither != null) { + if (wither.created) { + wither.destroy(); } } - dragon = new SpawnFakeWither.FakeWither(p, ProtocolLibrary.getProtocolManager()); - dragon.setCustomName(text); - dragon.create(); + wither = new SpawnFakeWither.FakeWither(p, ProtocolLibrary.getProtocolManager()); + wither.setCustomName(text); + wither.setVisible(false); + wither.create(); t = new TeleportScheduler(this); Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), t, 100); } public void delete() { - if (dragon != null) { - if (dragon.created) { + if (wither != null) { + if (wither.created) { if (t != null) { t.cancel(); } - dragon.destroy(); + wither.destroy(); } } } diff --git a/src/main/java/tech/nully/BossBarAPI/Main.java b/src/main/java/tech/nully/BossBarAPI/Main.java index 4241a2f..388f924 100644 --- a/src/main/java/tech/nully/BossBarAPI/Main.java +++ b/src/main/java/tech/nully/BossBarAPI/Main.java @@ -21,7 +21,6 @@ public class Main extends JavaPlugin { public void onEnable() { instance = this; System.out.println("BossBar is on"); - getCommand("bossbar").setExecutor(new FakeWitherCommand()); } // Overrides onDisable diff --git a/src/main/java/tech/nully/BossBarAPI/SpawnFakeWither.java b/src/main/java/tech/nully/BossBarAPI/SpawnFakeWither.java index 3b69bea..3409fef 100644 --- a/src/main/java/tech/nully/BossBarAPI/SpawnFakeWither.java +++ b/src/main/java/tech/nully/BossBarAPI/SpawnFakeWither.java @@ -4,6 +4,7 @@ import com.comphenix.packetwrapper.Packet18SpawnMob; import com.comphenix.packetwrapper.Packet1DDestroyEntity; import com.comphenix.packetwrapper.Packet28EntityMetadata; import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.injector.PlayerLoggedOutException; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import org.bukkit.Bukkit; @@ -16,36 +17,41 @@ import java.lang.reflect.InvocationTargetException; import java.util.logging.Level; public class SpawnFakeWither extends JavaPlugin { - public static final int TICKS_PER_SECOND = 20; + static final int TICKS_PER_SECOND = 20; // You could also use a full-fledged API like RemoteEntities - public static class FakeWither { + static class FakeWither { public static final byte INVISIBLE = 0x20; + // Just a guess + private static final int HEALTH_RANGE = 80 * 80; // Next entity ID - public static int NEXT_ID = 6000; + private static int NEXT_ID = 6000; - public static final int METADATA_WITHER_HEALTH = 16; // 1.5.2 -> Change to 16 + private static final int METADATA_WITHER_HEALTH = 16; // 1.5.2 -> Change to 16 // Metadata indices - public static final int METADATA_FLAGS = 0; - public static final int METADATA_NAME = 5; // 1.5.2 -> Change to 5 - public static final int METADATA_SHOW_NAME = 6; // 1.5.2 -> Change to 6 + private static final int METADATA_FLAGS = 0; + private static final int METADATA_NAME = 5; // 1.5.2 -> Change to 5 + private static final int METADATA_SHOW_NAME = 6; // 1.5.2 -> Change to 6 // Unique ID - public int id = NEXT_ID++; + private int id = NEXT_ID++; // Default health - public int health = 200; - public String customName; - public boolean created; + private int health = 300; - public Location location; - public ProtocolManager manager; - public Player p; + private boolean visible; + private String customName; + boolean created; + + private Location location; + private ProtocolManager manager; + + private Player p; public FakeWither(Player p, ProtocolManager manager) { - this.location = new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY() - 45, p.getLocation().getZ()); - this.manager = manager; this.p = p; + this.location = new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY() - 25, p.getLocation().getZ()); + this.manager = manager; } public int getHealth() { @@ -63,6 +69,17 @@ public class SpawnFakeWither extends JavaPlugin { this.health = health; } + public void setVisible(boolean visible) { + // Make visible or invisible + if (created) { + WrappedDataWatcher watcher = new WrappedDataWatcher(); + + watcher.setObject(METADATA_FLAGS, visible ? (byte)0 : INVISIBLE); + sendMetadata(watcher); + } + this.visible = visible; + } + public void setCustomName(String name) { if (created) { WrappedDataWatcher watcher = new WrappedDataWatcher(); @@ -81,16 +98,12 @@ public class SpawnFakeWither extends JavaPlugin { this.customName = name; } - public void sendMetadata(WrappedDataWatcher watcher) { + private void sendMetadata(WrappedDataWatcher watcher) { Packet28EntityMetadata update = new Packet28EntityMetadata(); update.setEntityId(id); update.setEntityMetadata(watcher.getWatchableObjects()); - try { - manager.sendServerPacket(p, update.getHandle()); - } catch (InvocationTargetException e) { - Bukkit.getLogger().log(Level.WARNING, "Cannot send " + update.getHandle() + " to " + p, e); - } + sendPacket(update.getHandle(), p); } public int getId() { @@ -101,27 +114,22 @@ public class SpawnFakeWither extends JavaPlugin { Packet18SpawnMob spawnMob = new Packet18SpawnMob(); WrappedDataWatcher watcher = new WrappedDataWatcher(); - watcher.setObject(METADATA_FLAGS, INVISIBLE); + watcher.setObject(METADATA_FLAGS, visible ? (byte)0 : INVISIBLE); watcher.setObject(METADATA_WITHER_HEALTH, (int) health); // 1.5.2 -> Change to (int) - if (customName != null) { watcher.setObject(METADATA_NAME, customName); watcher.setObject(METADATA_SHOW_NAME, (byte) 1); } spawnMob.setEntityID(id); - spawnMob.setType(EntityType.ENDER_DRAGON); + spawnMob.setType(EntityType.WITHER); spawnMob.setX(location.getX()); spawnMob.setY(location.getY()); spawnMob.setZ(location.getZ()); spawnMob.setMetadata(watcher); - try { - manager.sendServerPacket(p, spawnMob.getHandle()); - } catch (InvocationTargetException e) { - Bukkit.getLogger().log(Level.WARNING, "Cannot send " + spawnMob.getHandle() + " to " + p, e); - } catch (PlayerLoggedOutException ignored) {} + sendPacket(spawnMob.getHandle(), p); created = true; } @@ -130,14 +138,18 @@ public class SpawnFakeWither extends JavaPlugin { throw new IllegalStateException("Cannot kill a killed entity."); Packet1DDestroyEntity destroyMe = new Packet1DDestroyEntity(); - destroyMe.setEntities(new int[]{id}); + destroyMe.setEntities(new int[] { id }); - try { - manager.sendServerPacket(p, destroyMe.getHandle()); - } catch (InvocationTargetException e) { - Bukkit.getLogger().log(Level.WARNING, "Cannot send " + destroyMe.getHandle() + " to " + p, e); - } catch (PlayerLoggedOutException ignored) {} + sendPacket(destroyMe.getHandle(), p); created = false; } + + private void sendPacket(PacketContainer packet, Player p) { + try { + manager.sendServerPacket(p, packet); + } catch (InvocationTargetException e) { + Bukkit.getLogger().log(Level.WARNING, "Cannot send " + packet + " to " + p, e); + } catch (PlayerLoggedOutException ignore) {} + } } } \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3412f00..25b8c2d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,7 +6,4 @@ authors: [BongoCat] description: An API for eaglercraft to be able to display bossbars depend: [ProtocolLib] website: nully.tech -commands: - bossbar: - usage: / - description: Displays a bossbar \ No newline at end of file +commands: \ No newline at end of file diff --git a/target/classes/plugin.yml b/target/classes/plugin.yml new file mode 100644 index 0000000..25b8c2d --- /dev/null +++ b/target/classes/plugin.yml @@ -0,0 +1,9 @@ +name: BossBarAPI +version: 0.0.1 +main: tech.nully.BossBarAPI.Main +prefix: [BossBarAPI] +authors: [BongoCat] +description: An API for eaglercraft to be able to display bossbars +depend: [ProtocolLib] +website: nully.tech +commands: \ No newline at end of file diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..dded0ed --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Sat Aug 13 15:24:33 PDT 2022 +version=0.0.1 +groupId=tech.nully +artifactId=BossBarAPI diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..e69de29 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..6f64b0e --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,5 @@ +/home/bongle/GitHub/ClassicCore/src/main/java/tech/nully/BossBarAPI/TeleportScheduler.java +/home/bongle/GitHub/ClassicCore/src/main/java/tech/nully/BossBarAPI/Main.java +/home/bongle/GitHub/ClassicCore/src/main/java/tech/nully/BossBarAPI/FakeWitherCommand.java +/home/bongle/GitHub/ClassicCore/src/main/java/tech/nully/BossBarAPI/SpawnFakeWither.java +/home/bongle/GitHub/ClassicCore/src/main/java/tech/nully/BossBarAPI/BossBar.java