Added Sign Interact

This commit is contained in:
Sn0wStorm 2013-08-03 01:43:05 +02:00
parent 5704852bd4
commit 565413c555
5 changed files with 218 additions and 8 deletions

View File

@ -34,6 +34,7 @@ import com.dre.dungeonsxl.EditWorld;
import com.dre.dungeonsxl.LeaveSign; import com.dre.dungeonsxl.LeaveSign;
import com.dre.dungeonsxl.game.GameChest; import com.dre.dungeonsxl.game.GameChest;
import com.dre.dungeonsxl.game.GameWorld; import com.dre.dungeonsxl.game.GameWorld;
import com.dre.dungeonsxl.trigger.InteractTrigger;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
public P p = P.p; public P p = P.p;
@ -127,6 +128,12 @@ public class PlayerListener implements Listener {
GameWorld gworld = GameWorld.get(player.getWorld()); GameWorld gworld = GameWorld.get(player.getWorld());
if (gworld != null) { if (gworld != null) {
// Trigger InteractTrigger
InteractTrigger trigger = InteractTrigger.get(clickedBlock, gworld);
if (trigger != null) {
trigger.onTrigger(player);
}
// Ready Sign // Ready Sign
for (Block blockReady : gworld.blocksReady) { for (Block blockReady : gworld.blocksReady) {
if (blockReady.getLocation().distance(clickedBlock.getLocation()) < 1) { if (blockReady.getLocation().distance(clickedBlock.getLocation()) < 1) {

View File

@ -17,7 +17,7 @@ public abstract class DSign {
protected GameWorld gworld; protected GameWorld gworld;
// List of Triggers // List of Triggers
private Set<Trigger> triggers = new HashSet<Trigger>(); protected Set<Trigger> triggers = new HashSet<Trigger>();
public abstract boolean check(); public abstract boolean check();
@ -30,13 +30,15 @@ public abstract class DSign {
this.gworld = gworld; this.gworld = gworld;
// Check Trigger // Check Trigger
String[] typeSplit = sign.getLine(3).split(","); if (gworld != null) {
for (String typeSplitPart : typeSplit) { String[] typeSplit = sign.getLine(3).split(",");
String[] splitted = typeSplitPart.split(" "); for (String typeSplitPart : typeSplit) {
Trigger trigger = Trigger.getOrCreate(splitted, this); String[] splitted = typeSplitPart.split(" ");
if (trigger != null) { Trigger trigger = Trigger.getOrCreate(splitted, this);
trigger.addListener(this); if (trigger != null) {
this.triggers.add(trigger); trigger.addListener(this);
this.triggers.add(trigger);
}
} }
} }
} }
@ -118,6 +120,8 @@ public abstract class DSign {
dSign = new SIGNStart(sign, gworld); dSign = new SIGNStart(sign, gworld);
} else if (lines[0].equalsIgnoreCase("[" + SIGNTrigger.name + "]")) { } else if (lines[0].equalsIgnoreCase("[" + SIGNTrigger.name + "]")) {
dSign = new SIGNTrigger(sign, gworld); dSign = new SIGNTrigger(sign, gworld);
} else if (lines[0].equalsIgnoreCase("[" + SIGNInteract.name + "]")) {
dSign = new SIGNInteract(sign, gworld);
} else if (lines[0].equalsIgnoreCase("[" + SIGNRedstone.name + "]")) { } else if (lines[0].equalsIgnoreCase("[" + SIGNRedstone.name + "]")) {
dSign = new SIGNRedstone(sign, gworld); dSign = new SIGNRedstone(sign, gworld);
} else if (lines[0].equalsIgnoreCase("[" + SIGNBlock.name + "]")) { } else if (lines[0].equalsIgnoreCase("[" + SIGNBlock.name + "]")) {

View File

@ -0,0 +1,102 @@
package com.dre.dungeonsxl.signs;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.block.Sign;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;
import com.dre.dungeonsxl.game.GameWorld;
import com.dre.dungeonsxl.EditWorld;
import com.dre.dungeonsxl.trigger.InteractTrigger;
public class SIGNInteract extends DSign {
public static String name = "Interact";
public String buildPermissions = "dxl.sign.trigger";
public boolean onDungeonInit = false;
public SIGNInteract(Sign sign, GameWorld gworld) {
super(sign, gworld);
}
@Override
public boolean check() {
Set<Integer> used = new HashSet<Integer>();
for (Block block : EditWorld.get(sign.getLocation().getWorld()).sign) {
if (block != null) {
if (!block.getChunk().isLoaded()) {
block.getChunk().load();
}
if (block.getState() instanceof Sign) {
Sign rsign = (Sign) block.getState();
if (rsign.getLine(0).equalsIgnoreCase("[" + name + "]")) {
used.add(p.parseInt(rsign.getLine(1)));
}
}
}
}
int id = 1;
if (sign.getLine(1).equalsIgnoreCase("")) {
if (used.size() != 0) {
while (used.contains(id)) {
id++;
}
}
} else {
id = p.parseInt(sign.getLine(1));
if (used.contains(id)) {
return false;
} else {
return true;
}
}
sign.setLine(1, id + "");
p.getServer().getScheduler().scheduleSyncDelayedTask(p, new UpdateTask(), 2);
return true;
}
@Override
public void onInit() {
InteractTrigger trigger = InteractTrigger.getOrCreate(p.parseInt(sign.getLine(1)), sign.getBlock(), gworld);
if (trigger != null) {
trigger.addListener(this);
this.triggers.add(trigger);
}
sign.setLine(0, ChatColor.DARK_BLUE + "############");
sign.setLine(1, ChatColor.GREEN + sign.getLine(2));
sign.setLine(2, ChatColor.GREEN + sign.getLine(3));
sign.setLine(3, ChatColor.DARK_BLUE + "############");
sign.update();
}
@Override
public boolean onPlayerTrigger(Player player) {
return true;
}
@Override
public String getPermissions() {
return buildPermissions;
}
@Override
public boolean isOnDungeonInit() {
return onDungeonInit;
}
public class UpdateTask implements Runnable {
public UpdateTask() {
}
@Override
public void run() {
sign.update();
}
}
}

View File

@ -0,0 +1,91 @@
package com.dre.dungeonsxl.trigger;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import com.dre.dungeonsxl.game.GameWorld;
public class InteractTrigger extends Trigger {
private static Map<GameWorld, ArrayList<InteractTrigger>> triggers = new HashMap<GameWorld, ArrayList<InteractTrigger>>();
private int interactId;
private Block interactBlock;
public InteractTrigger(int id, Block block) {
this.interactId = id;
this.interactBlock = block;
}
public void onTrigger(Player player) {
triggered = true;
this.player = player;
updateDSigns();
}
public void register(GameWorld gworld) {
if (!hasTriggers(gworld)) {
ArrayList<InteractTrigger> list = new ArrayList<InteractTrigger>();
list.add(this);
triggers.put(gworld, list);
} else {
triggers.get(gworld).add(this);
}
}
public void unregister(GameWorld gworld) {
if (hasTriggers(gworld)) {
triggers.get(gworld).remove(this);
}
}
public static InteractTrigger getOrCreate(int id, GameWorld gworld) {
InteractTrigger trigger = get(id, gworld);
if (trigger != null) {
return trigger;
}
return new InteractTrigger(id, null);
}
public static InteractTrigger getOrCreate(int id, Block block, GameWorld gworld) {
InteractTrigger trigger = get(id, gworld);
if (trigger != null) {
trigger.interactBlock = block;
return trigger;
}
return new InteractTrigger(id, block);
}
public static InteractTrigger get(Block block, GameWorld gworld) {
if (hasTriggers(gworld)) {
for (InteractTrigger trigger : triggers.get(gworld)) {
if (trigger.interactBlock != null) {
if (trigger.interactBlock.equals(block)) {
return trigger;
}
}
}
}
return null;
}
public static InteractTrigger get(int id, GameWorld gworld) {
if (hasTriggers(gworld)) {
for (InteractTrigger trigger : triggers.get(gworld)) {
if (trigger.interactId == id) {
return trigger;
}
}
}
return null;
}
public static boolean hasTriggers(GameWorld gworld) {
return !triggers.isEmpty() && triggers.containsKey(gworld);
}
}

View File

@ -45,6 +45,12 @@ public abstract class Trigger {
trigger = SignTrigger.getOrCreate(P.p.parseInt(splitted[1]), dsign.getGameWorld()); trigger = SignTrigger.getOrCreate(P.p.parseInt(splitted[1]), dsign.getGameWorld());
} }
} else if (splitted[0].equalsIgnoreCase("I")) {
if (splitted.length > 1) {
trigger = InteractTrigger.getOrCreate(P.p.parseInt(splitted[1]), dsign.getGameWorld());
}
} }
} }
return trigger; return trigger;