Removed potion packet util and added MCoreEntityPotionColorEvent.

This commit is contained in:
Olof Larsson 2013-02-28 12:48:09 +01:00
parent bd1e2e29de
commit f1acaf8143
5 changed files with 156 additions and 74 deletions

View File

@ -15,6 +15,7 @@ import com.massivecraft.mcore.adapter.ItemStackAdapter;
import com.massivecraft.mcore.adapter.MongoURIAdapter; import com.massivecraft.mcore.adapter.MongoURIAdapter;
import com.massivecraft.mcore.adapter.PSAdapter; import com.massivecraft.mcore.adapter.PSAdapter;
import com.massivecraft.mcore.cmd.CmdMcore; import com.massivecraft.mcore.cmd.CmdMcore;
import com.massivecraft.mcore.integration.protocollib.ProtocolLibFeatures;
import com.massivecraft.mcore.mixin.ScheduledTeleportEngine; import com.massivecraft.mcore.mixin.ScheduledTeleportEngine;
import com.massivecraft.mcore.mixin.SenderIdMixinDefault; import com.massivecraft.mcore.mixin.SenderIdMixinDefault;
import com.massivecraft.mcore.store.Coll; import com.massivecraft.mcore.store.Coll;
@ -129,6 +130,9 @@ public class MCore extends MPlugin
this.cmdMcore = new CmdMcore(); this.cmdMcore = new CmdMcore();
this.cmdMcore.register(this, true); this.cmdMcore.register(this, true);
// Integration
this.integrate(ProtocolLibFeatures.get());
/* /*
test(""); test("");
test("+1day"); test("+1day");

View File

@ -0,0 +1,72 @@
package com.massivecraft.mcore.integration.protocollib;
import java.util.List;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
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.WrappedWatchableObject;
import com.massivecraft.mcore.MCore;
public class EntityPotionColorPacketAdapter extends PacketAdapter
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private EntityPotionColorPacketAdapter() { super(MCore.get(), ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Server.ENTITY_METADATA); }
private static EntityPotionColorPacketAdapter i = new EntityPotionColorPacketAdapter();
public static EntityPotionColorPacketAdapter get() { return i; }
// -------------------------------------------- //
// SETUP
// -------------------------------------------- //
public void setup()
{
ProtocolLibrary.getProtocolManager().addPacketListener(this);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void onPacketSending(PacketEvent event)
{
// If the server is sending a meta-data packet to a sendee player...
// NOTE: That must be the case. We are listening to no other situation.
final PacketContainer packet = event.getPacket();
final Player sendee = event.getPlayer();
// ... fetch the entity ...
// NOTE: MetaData packets are only sent to players in the same world.
final Entity entity = packet.getEntityModifier(sendee.getWorld()).read(0);
// ... fetch the metadata ...
final List<WrappedWatchableObject> metadata = packet.getWatchableCollectionModifier().read(0);
// ... for each watchable in the metadata ...
for (WrappedWatchableObject watchable : metadata)
{
// If the watchable is about potion effect color ...
if (watchable.getIndex() != 8) continue;
// ... run our custom async event to allow changing it ...
int oldColor = (Integer) watchable.getValue();
MCoreEntityPotionColorEvent colorEvent = new MCoreEntityPotionColorEvent(sendee, entity, oldColor);
colorEvent.run();
int newColor = colorEvent.getColor();
// ... alter if changed.
if (newColor != oldColor) watchable.setValue(newColor, false);
}
}
}

View File

@ -0,0 +1,55 @@
package com.massivecraft.mcore.integration.protocollib;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class MCoreEntityPotionColorEvent extends Event implements Runnable
{
// -------------------------------------------- //
// REQUIRED EVENT CODE
// -------------------------------------------- //
private static final HandlerList handlers = new HandlerList();
@Override public HandlerList getHandlers() { return handlers; }
public static HandlerList getHandlerList() { return handlers; }
// -------------------------------------------- //
// FIELDS & RAWDATA GET/SET
// -------------------------------------------- //
private final Player sendee;
public Player getSendee() { return this.sendee; }
private final Entity entity;
public Entity getEntity() { return this.entity; }
// http://www.wiki.vg/Entities#Index_8.2C_int:_Potion_effects
private int color;
public int getColor() { return this.color; }
public void setColor(int color) { this.color = color; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public MCoreEntityPotionColorEvent(Player sendee, Entity entity, int color)
{
this.sendee = sendee;
this.entity = entity;
this.color = color;
}
// -------------------------------------------- //
// RUN
// -------------------------------------------- //
@Override
public void run()
{
Bukkit.getPluginManager().callEvent(this);
}
}

View File

@ -0,0 +1,25 @@
package com.massivecraft.mcore.integration.protocollib;
import com.massivecraft.mcore.integration.IntegrationFeaturesAbstract;
public class ProtocolLibFeatures extends IntegrationFeaturesAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private ProtocolLibFeatures() { super("ProtocolLib"); }
private static ProtocolLibFeatures i = new ProtocolLibFeatures();
public static ProtocolLibFeatures get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void activate()
{
EntityPotionColorPacketAdapter.get().setup();
}
}

View File

@ -1,74 +0,0 @@
package com.massivecraft.mcore.util;
import net.minecraft.server.v1_4_R1.Packet41MobEffect;
import net.minecraft.server.v1_4_R1.Packet42RemoveMobEffect;
import org.bukkit.craftbukkit.v1_4_R1.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class PotionPaketUtil
{
// -------------------------------------------- //
// ASSUMING THE PLAYER SELF
// -------------------------------------------- //
public static void add(Player player, PotionEffectType potionEffectType, int amplifier, int duration)
{
add(player, player, potionEffectType, amplifier, duration);
}
public static void add(Player player, PotionEffect potionEffect)
{
add(player, player, potionEffect);
}
public static void remove(Player player, PotionEffectType potionEffectType)
{
remove(player, player, potionEffectType);
}
// -------------------------------------------- //
// USING BUKKIT STUFF
// -------------------------------------------- //
public static void add(Player player, Entity entity, PotionEffectType potionEffectType, int amplifier, int duration)
{
add(player, entity.getEntityId(), (byte)potionEffectType.getId(), (byte)amplifier, (short)duration);
}
public static void add(Player player, Entity entity, PotionEffect potionEffect)
{
add(player, entity.getEntityId(), (byte)potionEffect.getType().getId(), (byte)potionEffect.getAmplifier(), (short)potionEffect.getDuration());
}
public static void remove(Player player, Entity entity, PotionEffectType potionEffectType)
{
remove(player, entity.getEntityId(), (byte)potionEffectType.getId());
}
// -------------------------------------------- //
// CORE
// -------------------------------------------- //
public static void add(Player player, int entityId, byte effectId, byte amplifier, short duration)
{
Packet41MobEffect pm = new Packet41MobEffect();
pm.a = entityId;
pm.b = effectId;
pm.c = amplifier;
pm.d = duration;
((CraftPlayer)player).getHandle().playerConnection.sendPacket(pm);
}
public static void remove(Player player, int entityId, byte effectId)
{
Packet42RemoveMobEffect pr = new Packet42RemoveMobEffect();
pr.a = entityId;
pr.b = effectId;
((CraftPlayer)player).getHandle().playerConnection.sendPacket(pr);
}
}