Skip to content

Commit

Permalink
Add dPlayer.open_book mech
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Jan 4, 2015
1 parent d342ad3 commit 50c4227
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -2208,6 +2208,19 @@ else if (split.length > 1) {
PlayerSpectateEntity.setSpectating(getPlayerEntity(), value.asType(dEntity.class).getBukkitEntity());
}

// <--[mechanism]
// @object dPlayer
// @name open_book
// @input None
// @description
// Forces the player to open the written book in their hand.
// The book can safely be removed from the player's hand
// without the player closing the book.
// -->
if (mechanism.matches("open_book")) {
OpenBook.openBook(getPlayerEntity());
}

// Iterate through this object's properties' mechanisms
for (Property property : PropertyParser.getProperties(this)) {
property.adjust(mechanism);
Expand Down
@@ -0,0 +1,38 @@
package net.aufdemrand.denizen.utilities.packets;

import io.netty.buffer.Unpooled;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.minecraft.server.v1_8_R1.PacketDataSerializer;
import net.minecraft.server.v1_8_R1.PacketPlayOutCustomPayload;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.util.Map;

public class OpenBook {

private static final Field channel, packet_data;

static {
Map<String, Field> fields = PacketHelper.registerFields(PacketPlayOutCustomPayload.class);
channel = fields.get("a");
packet_data = fields.get("b");
}

public static PacketPlayOutCustomPayload getOpenBookPacket() {
PacketPlayOutCustomPayload customPayloadPacket = new PacketPlayOutCustomPayload();
try {
channel.set(customPayloadPacket, "MC|BOpen");
packet_data.set(customPayloadPacket, new PacketDataSerializer(Unpooled.buffer()));
} catch (Exception e) {
dB.echoError(e);
}
return customPayloadPacket;
}

public static void openBook(Player player) {
PacketPlayOutCustomPayload customPayloadPacket = getOpenBookPacket();
PacketHelper.sendPacket(player, customPayloadPacket);
}

}

0 comments on commit 50c4227

Please sign in to comment.