use of org.bukkit.block.TileState in project MissileWarfare by koiboi-dev.
the class AntiMissileLauncher method preRegister.
@Override
public void preRegister() {
// cancel thing on place
BlockPlaceHandler blockPlaceHandler = new BlockPlaceHandler(false) {
@Override
public void onPlayerPlace(BlockPlaceEvent event) {
BlockData data = event.getBlockPlaced().getBlockData();
((Directional) data).setFacing(BlockFace.UP);
event.getBlockPlaced().setBlockData(data);
Block block = event.getBlockPlaced();
// Block bottom = world.getBlockAt(event.getBlock().getLocation().subtract(new Vector(0, 2, 0)));
if (correctlyBuilt(block)) {
event.getPlayer().sendMessage(Translations.get("messages.launchers.createantiair.success"));
} else {
event.getPlayer().sendMessage(Translations.get("messages.launchers.createantiair.failure"));
}
}
};
addItemHandler(blockPlaceHandler);
BlockDispenseHandler blockDispenseHandler = this::blockDispense;
addItemHandler(blockDispenseHandler);
addItemHandler(new BlockTicker() {
@Override
public boolean isSynchronized() {
return true;
}
@Override
public void tick(Block block, SlimefunItem slimefunItem, Config config) {
TileState state = (TileState) block.getState();
PersistentDataContainer cont = state.getPersistentDataContainer();
if (!block.isBlockPowered()) {
List<MissileController> missiles = MissileWarfare.activemissiles;
MissileController locked = null;
if (!missiles.isEmpty()) {
for (MissileController missile : missiles) {
if (block.getLocation().distanceSquared(missile.pos.toLocation(missile.world)) < range && missile.isgroundmissile) {
locked = missile;
break;
}
}
}
state.update();
try {
if (locked != null && cont.get(new NamespacedKey(MissileWarfare.getInstance(), "timesincelastshot"), PersistentDataType.INTEGER) <= System.currentTimeMillis()) {
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "timesincelastshot"), PersistentDataType.INTEGER, (int) System.currentTimeMillis() + 1000);
fireMissile((Dispenser) block.getState(), locked);
}
} catch (NullPointerException e) {
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "timesincelastshot"), PersistentDataType.INTEGER, Integer.MIN_VALUE);
state.update();
}
}
}
});
}
use of org.bukkit.block.TileState in project MissileWarfare by koiboi-dev.
the class GroundMissileLauncher method fireMissile.
public boolean fireMissile(Dispenser disp, MissileClass missile) {
TileState state = (TileState) disp.getBlock().getState();
PersistentDataContainer cont = state.getPersistentDataContainer();
int[] coords = cont.get(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY);
Integer alt = cont.get(new NamespacedKey(MissileWarfare.getInstance(), "alt"), PersistentDataType.INTEGER);
if (coords == null) {
MissileWarfare.getInstance().getServer().broadcastMessage("Missile cannot fire at : " + new Vector(disp.getBlock().getLocation().getX(), disp.getBlock().getLocation().getY(), disp.getBlock().getLocation().getZ()) + " Invalid Coordinates!");
return false;
} else if (VariantsAPI.isInRange((int) disp.getLocation().distanceSquared(new Vector(coords[0], 0, coords[1]).toLocation(disp.getWorld())), missile.type)) {
MissileWarfare.getInstance().getServer().broadcastMessage("Missile cannot fire at : " + disp.getBlock().getLocation() + " Target out of distance!");
return false;
}
if (alt == null) {
alt = 120;
}
if (MissileWarfare.plugin.getConfig().getBoolean("logging.logMissileShots")) {
Player result = null;
double lastDistance = Double.MAX_VALUE;
for (Player p : disp.getWorld().getPlayers()) {
double distance = disp.getLocation().distanceSquared(p.getLocation());
if (distance < lastDistance) {
lastDistance = distance;
result = p;
}
}
MissileWarfare.getInstance().getLogger().info("Missile Shot || Location: " + disp.getBlock().getLocation() + " Target: " + new Vector(coords[0], 0, coords[1]) + " Nearest Player: " + result.getName());
if (MissileWarfare.getInstance().getConfig().getBoolean("logging.broadcastMissileShots")) {
final String playername = result.getName();
new BukkitRunnable() {
@Override
public void run() {
MissileWarfare.getInstance().getServer().broadcastMessage("Missile Shot! Launcher Coords: " + disp.getBlock().getLocation().toVector() + " Nearest Player: " + playername);
}
}.runTaskLater(MissileWarfare.getInstance(), 20L * MissileWarfare.getInstance().getConfig().getLong("logging.waitTimeBeforeBroadcast"));
}
}
MissileController _missile = new MissileController(true, disp.getBlock().getLocation().add(new Vector(0.5, 1.35, 0.5)).toVector(), new Vector(coords[0], 0, coords[1]), (float) missile.speed, disp.getBlock().getWorld(), missile.power, missile.accuracy, missile.type, alt);
_missile.FireMissile();
return true;
}
use of org.bukkit.block.TileState in project MissileWarfare by koiboi-dev.
the class GroundMissileLauncher method onBlockRightClick.
private void onBlockRightClick(PlayerRightClickEvent event) {
// Stick/Blaze Rod Method
if (event.getItem().getType() == Material.STICK) {
event.cancel();
TileState state = (TileState) Objects.requireNonNull(event.getInteractEvent().getClickedBlock()).getState();
PersistentDataContainer cont = state.getPersistentDataContainer();
try {
if (event.getPlayer().isSneaking()) {
int[] coords = cont.get(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY);
float dist = (float) new Vector(coords[0], 0, coords[1]).distance(new Vector(event.getInteractEvent().getClickedBlock().getX(), 0, event.getInteractEvent().getClickedBlock().getY()));
event.getPlayer().sendMessage(Translations.get("messages.launchers.ground.coords").replace("{xcoord}", String.valueOf(coords[0])).replace("{ycoord}", String.valueOf(coords[1])).replace("{dist}", String.valueOf(dist)));
return;
}
} catch (NullPointerException e) {
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY, new int[] { 0, 0 });
state.update();
}
try {
if (cont.get(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER) == 0) {
Prompt askCoordY = new StringPrompt() {
@Override
public String getPromptText(ConversationContext conversationContext) {
return "Input Coordinates Z, Input exit to cancel";
}
@Override
public Prompt acceptInput(ConversationContext conversationContext, String s) {
try {
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY, new int[] { cont.get(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY)[0], Integer.parseInt(s) });
} catch (NumberFormatException e) {
conversationContext.getForWhom().sendRawMessage(Translations.get("messages.launchers.ground.setting.notanumber"));
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
state.update();
return END_OF_CONVERSATION;
}
conversationContext.getForWhom().sendRawMessage("Z: " + Integer.parseInt(s));
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
state.update();
return END_OF_CONVERSATION;
}
};
Prompt askCoordX = new StringPrompt() {
@Override
public String getPromptText(ConversationContext conversationContext) {
return "Input Coordinates X, Input exit to cancel";
}
@Override
public Prompt acceptInput(ConversationContext conversationContext, String s) {
try {
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY, new int[] { Integer.parseInt(s), 0 });
} catch (NumberFormatException e) {
conversationContext.getForWhom().sendRawMessage(Translations.get("messages.launchers.ground.setting.notacoord"));
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
state.update();
return END_OF_CONVERSATION;
}
conversationContext.getForWhom().sendRawMessage("X: " + Integer.parseInt(s));
return askCoordY;
}
};
ConversationFactory cf = new ConversationFactory(MissileWarfare.getInstance());
Conversation conversation = cf.withFirstPrompt(askCoordX).withLocalEcho(false).withEscapeSequence("exit").withTimeout(20).buildConversation(event.getPlayer());
conversation.begin();
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 1);
}
} catch (NullPointerException e) {
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
state.update();
}
} else if (event.getItem().getType() == Material.BLAZE_ROD) {
event.cancel();
TileState state = (TileState) Objects.requireNonNull(event.getInteractEvent().getClickedBlock()).getState();
PersistentDataContainer cont = state.getPersistentDataContainer();
try {
if (cont.get(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER) == 0) {
Prompt askCruiseAlt = new StringPrompt() {
@Override
public String getPromptText(ConversationContext conversationContext) {
return "Input Cruise Altitude, Input exit to cancel";
}
@Override
public Prompt acceptInput(ConversationContext conversationContext, String s) {
try {
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "alt"), PersistentDataType.INTEGER, Integer.valueOf(s));
} catch (NumberFormatException e) {
conversationContext.getForWhom().sendRawMessage(Translations.get("messages.launchers.ground.setting.notanumber"));
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
state.update();
return END_OF_CONVERSATION;
}
conversationContext.getForWhom().sendRawMessage(Translations.get("messages.launchers.ground.setting.cruisealt") + Integer.parseInt(s));
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
state.update();
return END_OF_CONVERSATION;
}
};
ConversationFactory cf = new ConversationFactory(MissileWarfare.getInstance());
Conversation conversation = cf.withFirstPrompt(askCruiseAlt).withLocalEcho(false).withEscapeSequence("exit").withTimeout(20).buildConversation(event.getPlayer());
conversation.begin();
}
} catch (NullPointerException e) {
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
state.update();
}
} else if (SlimefunItem.getByItem(event.getItem()) == SlimefunItem.getById("PLAYERLIST")) {
event.cancel();
TileState state = (TileState) event.getClickedBlock().get().getBlockData();
PersistentDataContainer cont = state.getPersistentDataContainer();
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "groupid"), PersistentDataType.STRING, event.getItem().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(MissileWarfare.getInstance(), "id"), PersistentDataType.STRING));
state.update();
}
}
use of org.bukkit.block.TileState in project MissileWarfare by koiboi-dev.
the class GroundMissileLauncher method blockDispense.
private void blockDispense(BlockDispenseEvent blockDispenseEvent, Dispenser dispenser, Block block, SlimefunItem slimefunItem) {
blockDispenseEvent.setCancelled(true);
TileState state = (TileState) dispenser.getBlock().getState();
PersistentDataContainer cont = state.getPersistentDataContainer();
if (cont.get(new NamespacedKey(MissileWarfare.getInstance(), "canfire"), PersistentDataType.INTEGER) != 1) {
MissileWarfare.getInstance().getServer().broadcastMessage("Missile at : " + dispenser.getBlock().getLocation().toVector() + " Is unable to fire: Missing GREEN_CONCRETE Below");
}
new BukkitRunnable() {
@Override
public void run() {
if (dispenser.getBlock().getRelative(BlockFace.DOWN).getType() == Material.GREEN_CONCRETE) {
fireMissile(dispenser);
}
}
}.runTaskLater(MissileWarfare.getInstance(), 1);
}
use of org.bukkit.block.TileState in project MissileWarfare by koiboi-dev.
the class GroundMissileLauncher method preRegister.
@Override
public void preRegister() {
// cancel thing on place
BlockPlaceHandler blockPlaceHandler = new BlockPlaceHandler(false) {
@Override
public void onPlayerPlace(BlockPlaceEvent event) {
World world = event.getBlock().getWorld();
Block below = world.getBlockAt(event.getBlock().getLocation().subtract(new Vector(0, 1, 0)));
BlockData data = event.getBlockPlaced().getBlockData();
((Directional) data).setFacing(BlockFace.UP);
event.getBlockPlaced().setBlockData(data);
// Block bottom = world.getBlockAt(event.getBlock().getLocation().subtract(new Vector(0, 2, 0)));
if (below.getType() == Material.GREEN_CONCRETE) {
event.getPlayer().sendMessage(Translations.get("messages.launchers.ground.create.success"));
TileState state = (TileState) event.getBlockPlaced().getState();
PersistentDataContainer cont = state.getPersistentDataContainer();
cont.set(new NamespacedKey(MissileWarfare.getInstance(), "canfire"), PersistentDataType.INTEGER, 1);
state.update();
/*if (bottom.getType() == Material.GREEN_CONCRETE){
event.getPlayer().sendMessage("Created Small Launcher!");
}else{
event.getPlayer().sendMessage("Bottom Block is type: " + bottom.getType() + " It needs Type GREEN_CONCRETE");
event.setCancelled(true);
}*/
} else {
event.getPlayer().sendMessage(Translations.get("messages.launchers.ground.create.failure").replace("{type}", below.getType().name()));
}
}
};
addItemHandler(blockPlaceHandler);
BlockUseHandler blockUseHandler = this::onBlockRightClick;
addItemHandler(blockUseHandler);
BlockDispenseHandler blockDispenseHandler = this::blockDispense;
addItemHandler(blockDispenseHandler);
}
Aggregations