Skip to content

Commit

Permalink
Add dPlayer.edit_sign mech
Browse files Browse the repository at this point in the history
I love packets
  • Loading branch information
Morphan1 committed Jan 4, 2015
1 parent 50c4227 commit d231df7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -2221,6 +2221,18 @@ else if (split.length > 1) {
OpenBook.openBook(getPlayerEntity());
}

// <--[mechanism]
// @object dPlayer
// @name edit_sign
// @input dLocation
// @description
// Allows the player to edit an existing sign. To create a
// sign, see <@link command Sign>.
// -->
if (mechanism.matches("edit_sign") && mechanism.requireObject(dLocation.class)) {
SignEditor.editSign(getPlayerEntity(), value.asType(dLocation.class));
}

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

import net.aufdemrand.denizen.utilities.debugging.dB;
import net.minecraft.server.v1_8_R1.BlockPosition;
import net.minecraft.server.v1_8_R1.PacketPlayOutOpenSignEditor;
import net.minecraft.server.v1_8_R1.TileEntity;
import net.minecraft.server.v1_8_R1.TileEntitySign;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
import org.bukkit.entity.Player;

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

public class SignEditor {

private static final Field sign_location;

static {
Map<String, Field> fields = PacketHelper.registerFields(PacketPlayOutOpenSignEditor.class);
sign_location = fields.get("a");
}

public static PacketPlayOutOpenSignEditor getSignEditorPacket(Location location) {
PacketPlayOutOpenSignEditor signEditorPacket = new PacketPlayOutOpenSignEditor();
try {
sign_location.set(signEditorPacket, new BlockPosition(location.getX(), location.getY(), location.getZ()));
} catch (Exception e) {
dB.echoError(e);
}
return signEditorPacket;
}

public static void editSign(Player player, Location location) {
PacketPlayOutOpenSignEditor signEditorPacket = getSignEditorPacket(location);
TileEntity sign = ((CraftWorld) location.getWorld()).getTileEntityAt(location.getBlockX(),
location.getBlockY(), location.getBlockZ());
if (sign instanceof TileEntitySign) {
((TileEntitySign) sign).isEditable = true;
PacketHelper.sendPacket(player, signEditorPacket);
}
else {
dB.echoError("Can't edit non-sign materials!");
}
}

}

0 comments on commit d231df7

Please sign in to comment.