Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created July 12, 2013 18:22
Show Gist options
  • Save aadnk/5986632 to your computer and use it in GitHub Desktop.
Save aadnk/5986632 to your computer and use it in GitHub Desktop.
Create an explosion with no particles.
package com.comphenix.example;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
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.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.ChunkPosition;
import com.google.common.base.Preconditions;
public class NoParticleExplosion extends JavaPlugin {
// Whether or not there's a pending fake explosion
private boolean pendingFakeExplosion;
@Override
public void onEnable() {
ProtocolLibrary.getProtocolManager().addPacketListener(
new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL,
Packets.Server.EXPLOSION) {
@Override
public void onPacketSending(PacketEvent event) {
if (pendingFakeExplosion) {
PacketContainer packet = event.getPacket();
World world = event.getPlayer().getWorld();
// Just set these blocks to air
for (ChunkPosition position :
packet.getPositionCollectionModifier().read(0)) {
world.getBlockAt(position.getX(), position.getY(), position.getZ()).
setTypeIdAndData(0, (byte)0, true);
}
// Don't send the usual packet
event.setCancelled(true);
}
}
});
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
sendNoParticleExplosion(((Player) sender).getLocation(), 4, false);
}
return true;
}
/**
* Create an explosion with no particles.
* @param location - the origin or center of the explosion.
* @param power - the power of explosion, where 4F is TNT.
* @param setFire - whether or not to set blocks on fire.
* @return FALSE if explosion was canceled, otherwise TRUE.
*/
private boolean sendNoParticleExplosion(Location location, float power, boolean setFire) {
Preconditions.checkNotNull(location, "location cannot be NULL.");
try {
pendingFakeExplosion = true;
return location.getWorld().createExplosion(location, power, setFire);
} finally {
pendingFakeExplosion = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment