From 10e26e64efd60ef55ad95d50e4ac9e84633d8182 Mon Sep 17 00:00:00 2001 From: beito123 Date: Sun, 12 Apr 2020 17:57:58 +0900 Subject: [PATCH] Update Sit v2.2.0 - Fix some bugs - Support to let entity sit --- .gitignore | 2 +- Roto/build.gradle | 6 + .../com/gmx/mattcha/roto/EventListener.java | 87 ++++++++++++ .../java/com/gmx/mattcha/roto/MainClass.java | 130 ++++++++++++++++++ .../gmx/mattcha/roto/entity/EntityNPC.java | 26 ++++ Roto/src/main/resources/plugin.yml | 29 ++++ Sit/build.gradle | 2 +- .../com/gmx/mattcha/sit/EventListener.java | 25 ++-- .../java/com/gmx/mattcha/sit/MainClass.java | 93 ++----------- .../main/java/com/gmx/mattcha/sit/SitAPI.java | 120 ++++++++++++++++ Sit/src/main/resources/plugin.yml | 2 +- settings.gradle | 5 +- 12 files changed, 427 insertions(+), 100 deletions(-) create mode 100644 Roto/build.gradle create mode 100644 Roto/src/main/java/com/gmx/mattcha/roto/EventListener.java create mode 100644 Roto/src/main/java/com/gmx/mattcha/roto/MainClass.java create mode 100644 Roto/src/main/java/com/gmx/mattcha/roto/entity/EntityNPC.java create mode 100644 Roto/src/main/resources/plugin.yml create mode 100644 Sit/src/main/java/com/gmx/mattcha/sit/SitAPI.java diff --git a/.gitignore b/.gitignore index d4389fd..227a04d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ /dist/* /.gradle/* /*/build/* -*.cmd \ No newline at end of file +*.cmd diff --git a/Roto/build.gradle b/Roto/build.gradle new file mode 100644 index 0000000..9e70b28 --- /dev/null +++ b/Roto/build.gradle @@ -0,0 +1,6 @@ +group "com.gmx.mattcha.roto" +version "1.0.0" + +dependencies { + compile project(":Sit") +} \ No newline at end of file diff --git a/Roto/src/main/java/com/gmx/mattcha/roto/EventListener.java b/Roto/src/main/java/com/gmx/mattcha/roto/EventListener.java new file mode 100644 index 0000000..2aa1ae4 --- /dev/null +++ b/Roto/src/main/java/com/gmx/mattcha/roto/EventListener.java @@ -0,0 +1,87 @@ +package com.gmx.mattcha.roto; + +import cn.nukkit.Player; +import cn.nukkit.block.Block; +import cn.nukkit.block.BlockID; +import cn.nukkit.entity.Entity; +import cn.nukkit.event.EventHandler; +import cn.nukkit.event.Listener; +import cn.nukkit.event.entity.EntityDespawnEvent; +import cn.nukkit.event.player.PlayerInteractEntityEvent; +import cn.nukkit.event.player.PlayerInteractEvent; +import cn.nukkit.item.Item; +import cn.nukkit.item.ItemID; +import cn.nukkit.math.BlockFace; +import cn.nukkit.utils.Faceable; +import com.gmx.mattcha.sit.SitAPI; + +public class EventListener implements Listener { + + private MainClass plugin; + + public EventListener(MainClass plugin) { + this.plugin = plugin; + } + + public MainClass getPlugin() { + return this.plugin; + } + + @EventHandler + public void onEntityTap(PlayerInteractEntityEvent event) { + Entity entity = event.getEntity(); + + /*if (!this.plugin.npcList.containsKey(entity.getId())) { + return; + }*/ + + Item item = event.getItem(); + if (item.getId() != ItemID.POTATO) { + return; + } + + if (SitAPI.getInstance().hasSat(entity)) { + SitAPI.getInstance().standupEntity(entity); + return; + } + + SitAPI.getInstance().sitEntity(entity); + } + + @EventHandler + public void onDespawn(EntityDespawnEvent event) { + Entity entity = event.getEntity(); + + if (!this.plugin.npcList.containsKey(entity.getId())) { + return; + } + + this.plugin.npcList.remove(entity.getId()); + } + + @EventHandler + public void onTap(PlayerInteractEvent event) { + Player player = event.getPlayer(); + if (!this.plugin.isBlockMode(player)) { + return; + } + + Block block = event.getBlock(); + + if (block.getId() == BlockID.AIR) { + return; + } + + String msg = "---------- Block Info(" + block.getX() + ", " + block.getY() + ", " + block.getZ() + ") ----------\n"; + + msg += "Block: " + block.getName() + "(ID=" + block.getId() + ", Damage=" + block.getDamage() + ", SaveID=" + block.getSaveId() + ")\n"; + msg += "Chunk X=" + block.getChunkX() + ", Y=" + block.getChunkZ() + "\n"; + + if (block instanceof Faceable) { + BlockFace face = ((Faceable) block).getBlockFace(); + msg += "BlockFace: " + face.getName() + "(Index=" + face.getIndex() + ", Axis=" + face.getAxis() + ", HIndex=" + face.getHorizontalIndex() + ", HAngle=" + face.getHorizontalAngle() + ")\n"; + } + + player.sendMessage(msg); + } +} diff --git a/Roto/src/main/java/com/gmx/mattcha/roto/MainClass.java b/Roto/src/main/java/com/gmx/mattcha/roto/MainClass.java new file mode 100644 index 0000000..60ced03 --- /dev/null +++ b/Roto/src/main/java/com/gmx/mattcha/roto/MainClass.java @@ -0,0 +1,130 @@ +package com.gmx.mattcha.roto; + +import cn.nukkit.Player; +import cn.nukkit.Server; +import cn.nukkit.command.Command; +import cn.nukkit.command.CommandSender; +import cn.nukkit.entity.Entity; +import cn.nukkit.math.Vector3; +import cn.nukkit.nbt.tag.CompoundTag; +import cn.nukkit.plugin.PluginBase; +import cn.nukkit.utils.TextFormat; +import com.gmx.mattcha.roto.entity.EntityNPC; + +import java.util.*; + +public class MainClass extends PluginBase { + + private List tempBlockMode = new ArrayList<>(); + public Map npcList = new HashMap<>(); + + @Override + public void onEnable() { + // Register a entity + Entity.registerEntity("EntityNPC", EntityNPC.class, true); + + // Register event listener + this.getServer().getPluginManager().registerEvents(new EventListener(this), this); + + Server.getInstance().getLogger().notice(TextFormat.YELLOW + "Enabled Roto! " + + TextFormat.RED + "Don't use in production server!"); + } + + // Commands + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + switch (command.getName()) { + case "roto": + if (args.length < 1) { + return false; + } + + if (!(sender instanceof Player)) { + sender.sendMessage(TextFormat.RED + "Please run the command in game"); + return true; + } + + Player player = (Player) sender; + + switch (args[0]) { + case "block": + case "b": + if (this.isBlockMode(player)) { + this.removeBlockMode(player); + sender.sendMessage("You are not block mode"); + return true; + } + + this.addBlockMode(player); + sender.sendMessage("You are block mode"); + break; + default: + sender.sendMessage( + "----- Roto Commands -----\n" + + "/roto - This is not used now :P\n" + + "/npc - Make a fake NPC\n" + ); + } + + return true; + case "npc": + return this.execNPCCommand(sender, args); + } + + return false; + } + + private boolean execNPCCommand(CommandSender sender, String[] args) { + if (args.length < 1) { + return false; + } + + if (!(sender instanceof Player)) { + sender.sendMessage(TextFormat.RED + "Please run the command in game"); + return true; + } + + Player player = (Player) sender; + + switch (args[0]) { + case "clone": + case "c": + this.clonePlayerNPC(player); + return true; + default: + sender.sendMessage( + "----- Roto Commands -----\n" + + "/roto - This is not used now :P\n" + + "/npc - Make a fake NPC\n" + ); + return true; + } + } + + // API - BlockMode + + public boolean isBlockMode(Player player) { + return this.tempBlockMode.contains(player.getUniqueId()); + } + + public boolean addBlockMode(Player player) { + return this.tempBlockMode.add(player.getUniqueId()); + } + + public boolean removeBlockMode(Player player) { + return this.tempBlockMode.remove(player.getUniqueId()); + } + + // API - NPC + + public void clonePlayerNPC(Player player) { + CompoundTag nbt = Entity.getDefaultNBT(player, new Vector3(), (float) player.getYaw(), (float) player.getPitch()); + + EntityNPC enpc = (EntityNPC) Entity.createEntity("EntityNPC", player.chunk, nbt, player.getSkin()); + + enpc.spawnToAll(); + + this.npcList.put(enpc.getId(), enpc); + } +} diff --git a/Roto/src/main/java/com/gmx/mattcha/roto/entity/EntityNPC.java b/Roto/src/main/java/com/gmx/mattcha/roto/entity/EntityNPC.java new file mode 100644 index 0000000..dd7b0bf --- /dev/null +++ b/Roto/src/main/java/com/gmx/mattcha/roto/entity/EntityNPC.java @@ -0,0 +1,26 @@ +package com.gmx.mattcha.roto.entity; + +import cn.nukkit.entity.EntityHuman; +import cn.nukkit.entity.data.Skin; +import cn.nukkit.level.format.FullChunk; +import cn.nukkit.nbt.tag.CompoundTag; + +public class EntityNPC extends EntityHuman { + + public EntityNPC(FullChunk chunk, CompoundTag nbt, Skin skin) { + super(chunk, nbt); + + this.skin = skin; + } + + @Override + protected void initEntity() { + this.skin = new Skin(); + super.initEntity(); + } + + @Override + public int getNetworkId() { + return -1; + } +} diff --git a/Roto/src/main/resources/plugin.yml b/Roto/src/main/resources/plugin.yml new file mode 100644 index 0000000..92b9758 --- /dev/null +++ b/Roto/src/main/resources/plugin.yml @@ -0,0 +1,29 @@ +name: Roto +version: 1.0.0 +description: "a development tool" +author: beito3 +api: ["1.0.9"] +main: com.gmx.mattcha.roto.MainClass +load: STARTUP + +commands: + roto: + description: "roto main commands" + usage: "/roto help" + permission: roto.command.roto + aliases: ["r"] + npc: + description: "make npc" + usage: "/npc help" + permission: roto.command.npc + aliases: ["n"] + +permissions: + roto: + children: + roto.command: + children: + roto.command.roto: + default: true + roto.command.npc: + default: true \ No newline at end of file diff --git a/Sit/build.gradle b/Sit/build.gradle index 3cd8a02..e0370dc 100644 --- a/Sit/build.gradle +++ b/Sit/build.gradle @@ -1,2 +1,2 @@ group "com.gmx.mattcha.sit" -version "1.0.0" \ No newline at end of file +version "2.2.0" \ No newline at end of file diff --git a/Sit/src/main/java/com/gmx/mattcha/sit/EventListener.java b/Sit/src/main/java/com/gmx/mattcha/sit/EventListener.java index 6823f9f..34355ee 100644 --- a/Sit/src/main/java/com/gmx/mattcha/sit/EventListener.java +++ b/Sit/src/main/java/com/gmx/mattcha/sit/EventListener.java @@ -18,7 +18,6 @@ import cn.nukkit.event.player.*; import cn.nukkit.event.server.DataPacketReceiveEvent; import cn.nukkit.level.Position; -import cn.nukkit.math.Vector3f; import cn.nukkit.network.protocol.InteractPacket; import cn.nukkit.network.protocol.ProtocolInfo; import com.gmx.mattcha.sit.entity.Chair; @@ -87,15 +86,15 @@ public void onTapBlock(PlayerInteractEvent event) { } Player player = event.getPlayer(); - if (!this.plugin.hasSat(player)) { + if (!SitAPI.getInstance().hasSat(player)) { return; } Block block = event.getBlock(); if (block instanceof BlockStairs && (block.getDamage() & 0x04) == 0) { - this.plugin.sitPlayer(player, - block.add(0.5, 0, 0.5), - new Vector3f(0 , 1.58F, 0)); + SitAPI.getInstance().sitEntity(player, + block.add(SitAPI.getInstance().defaultSitStairPosition), + SitAPI.getInstance().defaultSitStairOffset); this.plugin.msg.sendTip(player, "sit.tapwarp.atStair.ok"); } @@ -103,9 +102,7 @@ public void onTapBlock(PlayerInteractEvent event) { @EventHandler public void onDespawn(EntityDespawnEvent event) { - if (event.getEntity() instanceof Player) { - this.plugin.closeChair((Player) event.getEntity()); - } + SitAPI.getInstance().closeChair(event.getEntity()); } @EventHandler @@ -114,7 +111,7 @@ public void onDead(PlayerDeathEvent event) { return; } - this.plugin.closeChair(event.getEntity()); + SitAPI.getInstance().closeChair(event.getEntity()); } @EventHandler @@ -123,7 +120,7 @@ public void onBedEnter(PlayerBedEnterEvent event) { return; } - this.plugin.closeChair(event.getPlayer()); + SitAPI.getInstance().closeChair(event.getPlayer()); } @EventHandler @@ -132,7 +129,7 @@ public void onTeleport(PlayerTeleportEvent event) { return; } - this.plugin.closeChair(event.getPlayer()); + SitAPI.getInstance().closeChair(event.getPlayer()); } @EventHandler @@ -143,7 +140,7 @@ public void onInvalidMove(PlayerInvalidMoveEvent event) { Player player = event.getPlayer(); - Chair chair = this.plugin.getChair(player); + Chair chair = SitAPI.getInstance().getChair(player); if (chair == null) { return; } @@ -168,7 +165,7 @@ public void onActionPacket(DataPacketReceiveEvent event) { if (event.getPacket().pid() == ProtocolInfo.INTERACT_PACKET) { Player player = event.getPlayer(); - if (!this.plugin.hasSat(player)) { + if (!SitAPI.getInstance().hasSat(player)) { return; } @@ -184,7 +181,7 @@ public void onActionPacket(DataPacketReceiveEvent event) { tempSit.remove(player.getUniqueId()); } - this.plugin.closeChair(player); + SitAPI.getInstance().closeChair(player); this.tempJustStoodup.put(player.getUniqueId(), new TempData(System.currentTimeMillis(), player)); // bad hack } } diff --git a/Sit/src/main/java/com/gmx/mattcha/sit/MainClass.java b/Sit/src/main/java/com/gmx/mattcha/sit/MainClass.java index 4b72130..be9d2c7 100644 --- a/Sit/src/main/java/com/gmx/mattcha/sit/MainClass.java +++ b/Sit/src/main/java/com/gmx/mattcha/sit/MainClass.java @@ -14,18 +14,15 @@ import cn.nukkit.command.Command; import cn.nukkit.command.CommandSender; import cn.nukkit.entity.Entity; -import cn.nukkit.math.Vector3; -import cn.nukkit.math.Vector3f; -import cn.nukkit.nbt.tag.CompoundTag; import cn.nukkit.plugin.PluginBase; import cn.nukkit.utils.Config; -import cn.nukkit.utils.TextFormat; import com.gmx.mattcha.sit.entity.Chair; -import com.gmx.mattcha.sit.event.PlayerSitEvent; import com.gmx.mattcha.sit.util.CustomMessage; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class MainClass extends PluginBase { @@ -33,13 +30,10 @@ public class MainClass extends PluginBase { public CustomMessage msg; - private Map usingChairs = new HashMap<>(); - public boolean enabledTapWarp; public boolean enabledBadhackKickFlying; - public Vector3 defaultSitPosition = new Vector3(0, 0F, 0); - public Vector3f defaultSitOffset = new Vector3f(0, 1.05F, 0); + private SitAPI sitAPI; @Override public void onEnable() { @@ -68,13 +62,15 @@ public void onEnable() { // Listen events this.getServer().getPluginManager().registerEvents(new EventListener(this), this); + + // Ready API + this.sitAPI = new SitAPI(this); } @Override public void onDisable() { - for (Map.Entry entry : usingChairs.entrySet()) { - entry.getValue().close(); - } + // close all chairs + SitAPI.getInstance().closeAllChairs(); } @Override @@ -90,82 +86,17 @@ public boolean onCommand(CommandSender sender, Command command, String label, St Player player = (Player) sender; - if (this.hasSat(player)) { - this.closeChair(player); + if (SitAPI.getInstance().hasSat(player)) { + this.sitAPI.closeChair(player); this.msg.sendTip(player, "command.sit.standup"); return true; } - this.sitPlayer(player, player.add(defaultSitPosition), defaultSitOffset);// adjust + this.sitAPI.sitEntity(player); this.msg.sendTip(player, "command.sit.ok"); - Server.getInstance().getLogger().info(TextFormat.BLUE + player.asVector3f().toString()); return true; } - - // API - - public boolean hasSat(Player player) { - return this.usingChairs.containsKey(player.getUniqueId()); - } - - public void closeChair(Player player) { - Chair chair = this.getChair(player); - if (chair == null) { - return; - } - - this.usingChairs.remove(player.getUniqueId()); - - if (chair.isClosed()) { - return; - } - - for (Entity passenger : new ArrayList<>(chair.getPassengers())) { - if (passenger == null) { - continue; - } - - chair.dismountEntity(passenger); - } - - chair.close(); - } - - public Chair getChair(Player player) { - if (!this.hasSat(player)) { - return null; - } - - return this.usingChairs.get(player.getUniqueId()); - } - - public boolean sitPlayer(Player player, Vector3 pos, Vector3f offset) { - closeChair(player); - - PlayerSitEvent ev; - Server.getInstance().getPluginManager().callEvent(ev = new PlayerSitEvent(this, player)); - if (ev.isCancelled()) { - return false; - } - CompoundTag nbt = Entity.getDefaultNBT(pos, new Vector3(), (float) player.getYaw(), 0); - - Chair chair = (Chair) Entity.createEntity("Chair", player.chunk, nbt); - - chair.MountedOffset = offset; - chair.setSeatPosition(new Vector3f(0, 0, 0)); // is not applied... - - chair.spawnToAll(); - chair.mountEntity(player); - - this.usingChairs.put(player.getUniqueId(), chair); - - return true; - } - - public void standupPlayer(Player player) { - closeChair(player); - } } diff --git a/Sit/src/main/java/com/gmx/mattcha/sit/SitAPI.java b/Sit/src/main/java/com/gmx/mattcha/sit/SitAPI.java new file mode 100644 index 0000000..f616113 --- /dev/null +++ b/Sit/src/main/java/com/gmx/mattcha/sit/SitAPI.java @@ -0,0 +1,120 @@ +package com.gmx.mattcha.sit; + +import cn.nukkit.Player; +import cn.nukkit.Server; +import cn.nukkit.entity.Entity; +import cn.nukkit.math.Vector3; +import cn.nukkit.math.Vector3f; +import cn.nukkit.nbt.tag.CompoundTag; +import com.gmx.mattcha.sit.entity.Chair; +import com.gmx.mattcha.sit.event.PlayerSitEvent; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class SitAPI { + + private static SitAPI INSTANCE; + + private MainClass plugin; + + protected SitAPI(MainClass plugin) { + this.plugin = plugin; + + INSTANCE = this; + } + + public static SitAPI getInstance() { + return INSTANCE; + } + + public Vector3 defaultSitPosition = new Vector3(0, 0F, 0); + public Vector3f defaultSitOffset = new Vector3f(0, 1.05F, 0); + + public Vector3 defaultSitStairPosition = new Vector3(0.5, 0, 0.5); + public Vector3f defaultSitStairOffset = new Vector3f(0 , 1.58F, 0); + + private Map usedChairs = new HashMap<>(); + + public boolean hasSat(Entity entity) { + return this.usedChairs.containsKey(entity.getId()); + } + + public Chair getChair(Entity entity) { + if (!this.hasSat(entity)) { + return null; + } + + return this.usedChairs.get(entity.getId()); + } + + public void closeChair(Entity entity) { + Chair chair = this.getChair(entity); + if (chair == null) { + return; + } + + this.usedChairs.remove(entity.getId()); + + if (chair.isClosed()) { + return; + } + + for (Entity passenger : new ArrayList<>(chair.getPassengers())) { + if (passenger == null) { + continue; + } + + chair.dismountEntity(passenger); + } + + chair.close(); + } + + public void closeAllChairs() { + for (Map.Entry e : this.usedChairs.entrySet()) { + e.getValue().close(); + } + } + + public boolean sitEntity(Entity entity) { + return this.sitEntity(entity, entity.add(defaultSitPosition)); + } + + public boolean sitEntity(Entity entity, Vector3 pos) { + return this.sitEntity(entity, pos, defaultSitOffset); + } + + public boolean sitEntity(Entity entity, Vector3 pos, Vector3f offset) { + closeChair(entity); + + if (entity instanceof Player) { + Player player = (Player) entity; + + PlayerSitEvent ev; + Server.getInstance().getPluginManager().callEvent(ev = new PlayerSitEvent(this.plugin, player)); + if (ev.isCancelled()) { + return false; + } + } + + CompoundTag nbt = Entity.getDefaultNBT(pos, new Vector3(), (float) entity.getYaw(), 0); + + Chair chair = (Chair) Entity.createEntity("Chair", entity.chunk, nbt); + + chair.MountedOffset = offset; + chair.setSeatPosition(new Vector3f(0, 0, 0)); // is not applied... + + chair.spawnToAll(); + chair.mountEntity(entity); + + this.usedChairs.put(entity.getId(), chair); + + return true; + } + + public void standupEntity(Entity entity) { + closeChair(entity); + } +} diff --git a/Sit/src/main/resources/plugin.yml b/Sit/src/main/resources/plugin.yml index 99f5922..41bace8 100644 --- a/Sit/src/main/resources/plugin.yml +++ b/Sit/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: Sit -version: 2.1.0 +version: 2.2.0 description: "You can sit here!" author: beito3 api: ["1.0.9"] diff --git a/settings.gradle b/settings.gradle index 79daab8..bb25331 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,4 @@ -rootProject.name = 'nukkitx-plugins' -include "Sit" \ No newline at end of file +include "Sit" +include 'Roto' +