Created
April 30, 2013 01:53
-
-
Save aadnk/5486168 to your computer and use it in GitHub Desktop.
Change the appearance of a falling anvil to a flaming block.
This file contains hidden or 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.util.HashSet; | |
import java.util.Set; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.FallingBlock; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.entity.EntityDeathEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.comphenix.protocol.Packets; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.events.ConnectionSide; | |
import com.comphenix.protocol.events.PacketAdapter; | |
import com.comphenix.protocol.events.PacketEvent; | |
public class FlamingAnvil extends JavaPlugin implements Listener { | |
private Set<Integer> flamingBlocks = new HashSet<Integer>(); | |
private boolean spawningFlamingBlocks; | |
@Override | |
public void onEnable() { | |
getServer().getPluginManager().registerEvents(this, this); | |
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(this, ConnectionSide.SERVER_SIDE, Packets.Server.VEHICLE_SPAWN) { | |
@Override | |
public void onPacketSending(PacketEvent event) { | |
int entityID = event.getPacket().getIntegers().read(0); | |
// See if we just spawned it | |
if (spawningFlamingBlocks) { | |
flamingBlocks.add(entityID); | |
} | |
if (flamingBlocks.contains(entityID)) { | |
// Set the material for the client. Look at PacketWrapper's Packet17SpawnObjectVehicle for more information. | |
event.getPacket().getIntegers().write(10, Material.FIRE.getId()); | |
} | |
} | |
}); | |
} | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | |
if (sender instanceof Player) { | |
spawnFireBlock((Player) sender); | |
} else { | |
sender.sendMessage(ChatColor.RED + "This command is only supported for players."); | |
} | |
return true; | |
} | |
@EventHandler | |
public void onEntityDeathEvent(EntityDeathEvent e) { | |
// Clean up | |
flamingBlocks.remove(e.getEntity().getEntityId()); | |
} | |
private void spawnFireBlock(Player player) { | |
// Indicate that we're currently spawning fire blocks | |
spawningFlamingBlocks = true; | |
Location above = player.getLocation().add(0, 10, 0); | |
FallingBlock block = player.getWorld().spawnFallingBlock(above, Material.ANVIL, (byte) 0); | |
// We're done - add the entity ID just in case | |
spawningFlamingBlocks = false; | |
flamingBlocks.add(block.getEntityId()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment