Created
December 14, 2012 14:57
-
-
Save aadnk/4286005 to your computer and use it in GitHub Desktop.
Spawn a fake mob using ProtocolLib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.comphenix.example; | |
import java.lang.reflect.InvocationTargetException; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Entity; | |
import org.bukkit.entity.EntityType; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.Listener; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.comphenix.protocol.Packets; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.ProtocolManager; | |
import com.comphenix.protocol.events.PacketContainer; | |
import com.comphenix.protocol.wrappers.WrappedDataWatcher; | |
import com.comphenix.protocol.wrappers.WrappedWatchableObject; | |
public class MobSpawner extends JavaPlugin implements Listener { | |
private ProtocolManager protocolManager; | |
private WrappedDataWatcher ghastWatcher; | |
public void onLoad() { | |
protocolManager = ProtocolLibrary.getProtocolManager(); | |
} | |
@Override | |
public void onEnable() { | |
ghastWatcher = getDefaultWatcher(getServer().getWorlds().get(0), EntityType.GHAST); | |
for (WrappedWatchableObject object : ghastWatcher) | |
System.out.println(object); | |
} | |
public void sendPacket(Player p) { | |
PacketContainer newPacket = new PacketContainer(24); | |
newPacket.getIntegers(). | |
write(0, 500). | |
write(1, (int) EntityType.GHAST.getTypeId()). | |
write(2, (int) (p.getLocation().getX() * 32)). | |
write(3, (int) (p.getLocation().getY() * 32)). | |
write(4, (int) (p.getLocation().getZ() * 32)); | |
newPacket.getDataWatcherModifier(). | |
write(0, ghastWatcher); | |
try { | |
ProtocolLibrary.getProtocolManager().sendServerPacket(p, newPacket); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} | |
} | |
public WrappedDataWatcher getDefaultWatcher(World world, EntityType type) { | |
Entity entity = world.spawnEntity(new Location(world, 0, 256, 0), type); | |
WrappedDataWatcher watcher = WrappedDataWatcher.getEntityWatcher(entity).deepClone(); | |
entity.remove(); | |
return watcher; | |
} | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | |
Player target = null; | |
// Get the target argument | |
if (args.length > 0) { | |
target = getServer().getPlayerExact(args[0]); | |
} else if (sender instanceof Player) { | |
target = (Player) sender; | |
} else { | |
sender.sendMessage(ChatColor.RED + "This command requires at least one argument."); | |
return true; | |
} | |
if (target != null) { | |
sendPacket(target); | |
} else { | |
sender.sendMessage(ChatColor.RED + "Cannot find player " + args[0]); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can i make more mobs and use the playerinteractintityevent?