Skip to content

Instantly share code, notes, and snippets.

@joserobjr
Last active December 27, 2019 04:35
Show Gist options
  • Save joserobjr/702a87fba0f9ffc8166f215065d51f2b to your computer and use it in GitHub Desktop.
Save joserobjr/702a87fba0f9ffc8166f215065d51f2b to your computer and use it in GitHub Desktop.
Example plugin (PR for CloudburstMC/Nukkit#1028)
name: SkellyMovePlugin
main: sample.SkellyMovePlugin
version: "1.0.0"
api: ["1.0.9"]
package sample;
import cn.nukkit.block.Block;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.mob.EntitySkeleton;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.Listener;
import cn.nukkit.event.player.PlayerInteractEntityEvent;
import cn.nukkit.event.player.PlayerInteractEvent;
import cn.nukkit.item.Item;
import cn.nukkit.level.Level;
import cn.nukkit.level.Sound;
import cn.nukkit.math.BlockFace;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.TextFormat;
public class SkellyMovePlugin extends PluginBase implements Listener {
@EventHandler
public void onInteractEntity(PlayerInteractEntityEvent event) {
Entity entity = event.getEntity();
if (entity instanceof EntitySkeleton) {
entity.saveNBT();
Item playerItem = event.getItem().clone();
playerItem.setCount(playerItem.getCount() -1);
event.getPlayer().getInventory().setItemInHand(playerItem);
Item drop = Item.get(Item.BONE);
drop.setCompoundTag(new CompoundTag("").putCompound("SkellyData", entity.namedTag.copy()));
drop.setCustomName(TextFormat.YELLOW+"Captured Skeleton");
Level level = entity.getLevel();
entity.close();
level.dropItem(entity, drop, null, true, 20);
level.addSound(entity, Sound.MOB_GHAST_FIREBALL);
event.setCancelled();
}
}
@EventHandler
public void onInteract(PlayerInteractEvent event) {
Item item = event.getItem();
Block block = event.getBlock();
if (item.hasCompoundTag() && item.getId() == Item.BONE && item.getNamedTag().contains("SkellyData") && block.getId() != 0) {
CompoundTag entityData = item.getNamedTag().getCompound("SkellyData");
if (!entityData.isEmpty()) {
BlockFace face = event.getFace();
Item playerItem = event.getItem().clone();
playerItem.setCount(playerItem.getCount() -1);
event.getPlayer().getInventory().setItemInHand(playerItem);
Entity entity = Entity.createEntity("Skeleton", block.getChunk(),
entityData.putList(new ListTag<>("Pos")
.add(new DoubleTag("", block.x + face.getXOffset() * 0.2 + face.getZOffset() * 0.5 + 0.25))
.add(new DoubleTag("", block.y + face.getYOffset()))
.add(new DoubleTag("", block.z + face.getZOffset() * 0.2 + face.getXOffset() * 0.5 + 0.25))
)
);
entity.spawnToAll();
block.getLevel().addSound(entity, Sound.BLOCK_BEEHIVE_EXIT);
event.setCancelled();
}
}
}
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment