Skip to content

Commit

Permalink
Allow - switch on a fence_gate
Browse files Browse the repository at this point in the history
Fence Gate objects (and who knows what other blocks) require a Player
link to flip, this commit will do everything possible to link a player
for these calculations (It doesn't particularly matter which player, or
even that it's a real player)

Also add some missing constructors to the dObject meta.
  • Loading branch information
mcmonkey4eva committed Dec 11, 2013
1 parent 5d01e6f commit 94f52b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dObject.java
Expand Up @@ -56,12 +56,14 @@
// | object notation: l@ can reference unique objects: no can be notable: yes
// | constructors: ( <>'s represent non-static information and are not literal)
// | l@<x>,<y>,<z>,<world_name> - fetches a specific location
// | l@<x>,<y>,<z>,<pitch>,<yaw>,<world_name> - fetches a specific location and direction
// | l@<notable_location_name> - fetches the location that has been 'noted' with the specified ID
//
// + ----- dEntity ------+
// | object notation: e@ can reference unique objects: yes can be notable: no
// | constructors: ( <>'s represent non-static information and are not literal)
// | e@<entity_type> - fetches a new entity with the specified type as implemented by Bukkit's entity type enumeration
// | e@<entity_type>,<setting> - fetches a new entity of the specified type with a custom setting unique to the type
// | e@<entity_script_name> - fetches a new custom entity as specified by the referenced entity script (soon)
// | e@<entity_id> - fetches the entity that has the (temporary) entity ID set by Bukkit
// | e@random - fetches a new, random entity
Expand Down
Expand Up @@ -9,10 +9,15 @@
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.minecraft.server.v1_7_R1.Block;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_7_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -82,8 +87,9 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
int duration = ((Duration)scriptEntry.getObject("duration")).getSecondsAsInt();
final SwitchState switchState = SwitchState.valueOf(scriptEntry.getElement("switchstate").asString());

final Player player = scriptEntry.hasPlayer() ? scriptEntry.getPlayer().getPlayerEntity(): null;
// Switch the Block
switchBlock(interactLocation, switchState);
switchBlock(interactLocation, switchState, player);

// TODO: Rewrite the below code to not use freakin' NMS!
// If duration set, schedule a delayed task.
Expand All @@ -99,22 +105,40 @@ public void run() {
// Check to see if the state of the block is what is expected. If switched during
// the duration, the switchback is cancelled.
if (switchState == SwitchState.OFF && !((interactLocation.getBlock().getData() & 0x8) > 0))
switchBlock(interactLocation, SwitchState.ON);
switchBlock(interactLocation, SwitchState.ON, player);
else if (switchState == SwitchState.ON && ((interactLocation.getBlock().getData() & 0x8) > 0))
switchBlock(interactLocation, SwitchState.OFF);
else if (switchState == SwitchState.TOGGLE) switchBlock(interactLocation, SwitchState.TOGGLE);
switchBlock(interactLocation, SwitchState.OFF, player);
else if (switchState == SwitchState.TOGGLE) switchBlock(interactLocation, SwitchState.TOGGLE, player);
}
}, duration * 20));
}

}

// Break off this portion of the code from execute() so it can be used in both execute and the delayed runnable
public void switchBlock(Location interactLocation, SwitchState switchState) {
public void switchBlock(Location interactLocation, SwitchState switchState, Player player) {
World world = interactLocation.getWorld();
boolean currentState = (interactLocation.getBlock().getData() & 0x8) > 0;
String state = switchState.toString();

// Try for a linked player
CraftPlayer craftPlayer = (CraftPlayer) player;
if (craftPlayer == null && Bukkit.getOnlinePlayers().length > 0) {
// If there's none, link any player
if (Bukkit.getOnlinePlayers().length > 0) {
craftPlayer = (CraftPlayer) Bukkit.getOnlinePlayers()[0];
}
else {
// If there are no players, link any Human NPC
for (NPC npc: CitizensAPI.getNPCRegistry()) {
if (npc.isSpawned() && npc.getEntity() instanceof Player) {
craftPlayer = (CraftPlayer) npc.getEntity();
break;
}
}
}
}

if ((state.equals("ON") && !currentState) ||
(state.equals("OFF") && currentState) ||
state.equals("TOGGLE")) {
Expand All @@ -126,13 +150,12 @@ public void switchBlock(Location interactLocation, SwitchState switchState) {
interactLocation.getBlockX(),
interactLocation.getBlockY(),
interactLocation.getBlockZ(),
null, 0, 0f, 0f, 0f);
craftPlayer != null ? craftPlayer.getHandle(): null, 0, 0f, 0f, 0f);

dB.log("Switched " + interactLocation.getBlock().getType().toString() + "! Current state now: " +
((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));

} catch (NullPointerException e) {

dB.echoError("Cannot switch " + interactLocation.getBlock().getType().toString() + "!");
}
}
Expand Down

0 comments on commit 94f52b4

Please sign in to comment.