use of org.cubeengine.module.elevator.data.ElevatorData in project modules-extra by CubeEngine.
the class ElevatorListener method onInteractBlock.
@Listener
public void onInteractBlock(InteractBlockEvent event, @Root Player player) {
if (!(event instanceof InteractBlockEvent.Primary.MainHand) && !(event instanceof InteractBlockEvent.Secondary.MainHand)) {
return;
}
BlockType type = event.getTargetBlock().getState().getType();
if (type != BlockTypes.STANDING_SIGN && type != BlockTypes.WALL_SIGN) {
return;
}
Location<World> loc = event.getTargetBlock().getLocation().get();
ElevatorData data = loc.get(ElevatorData.class).orElse(null);
Boolean sneak = player.get(Keys.IS_SNEAKING).orElse(false);
if (sneak) {
Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);
if (data == null) {
if (!(event instanceof InteractBlockEvent.Primary)) {
// Only Punch to activate
return;
}
if (itemInHand.isPresent()) {
if (player.hasPermission(module.getPerm().CREATE.getId()) && itemInHand.get().getType().equals(module.getConfig().creationItem)) {
data = new ElevatorData();
data.setOwner(player.getUniqueId());
loc.offer(data);
ItemStack item = itemInHand.get();
item.setQuantity(item.getQuantity() - 1);
player.setItemInHand(HandTypes.MAIN_HAND, item);
List<Text> list = loc.get(Keys.SIGN_LINES).get();
// Set First Line with name of renamed Item
list.set(0, itemInHand.get().get(Keys.DISPLAY_NAME).orElse(list.get(0)));
loc.offer(Keys.SIGN_LINES, list);
i18n.send(ACTION_BAR, player, POSITIVE, "Elevator created!");
updateSign(loc, data);
event.setCancelled(true);
}
}
} else if (// Sign has Elevator Data and hand is empty
!itemInHand.isPresent()) {
if (player.hasPermission(module.getPerm().ADJUST.getId())) {
// Search order dependent on click
Vector3i target = data.getTarget();
target = findNextSign(loc, target, loc.getBlockPosition(), event instanceof InteractBlockEvent.Primary);
data.setTarget(target);
updateSign(loc, data);
event.setCancelled(true);
}
} else if (itemInHand.get().getType() == ItemTypes.PAPER && event instanceof InteractBlockEvent.Primary) {
if (player.hasPermission(module.getPerm().RENAME.getId())) {
List<Text> list = loc.get(Keys.SIGN_LINES).get();
// Set First Line with name of renamed Item
list.set(0, itemInHand.get().get(Keys.DISPLAY_NAME).orElse(list.get(0)));
loc.offer(Keys.SIGN_LINES, list);
i18n.send(ACTION_BAR, player, POSITIVE, "Elevator name changed!");
event.setCancelled(true);
}
}
return;
}
if (event instanceof InteractBlockEvent.Secondary && player.hasPermission(module.getPerm().USE.getId())) {
Optional<Vector3i> target = event.getTargetBlock().get(IElevatorData.TARGET);
if (target.isPresent()) {
if (loc.getExtent().get(target.get(), ElevatorData.class).isPresent()) {
Vector3i sign = target.get();
Vector3d pPos = player.getLocation().getPosition();
Location<World> targetLoc = new Location<>(player.getWorld(), pPos.getX(), sign.getY() - 1, pPos.getZ());
if (!player.setLocationSafely(targetLoc)) {
i18n.send(ACTION_BAR, player, NEGATIVE, "Target obstructed");
}
event.setCancelled(true);
} else {
i18n.send(ACTION_BAR, player, NEGATIVE, "Target sign was destroyed!");
event.setCancelled(true);
}
}
}
if (event instanceof InteractBlockEvent.Secondary) {
Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);
if (itemInHand.isPresent()) {
if (player.hasPermission(module.getPerm().CREATE.getId()) && itemInHand.get().getType().equals(module.getConfig().creationItem)) {
event.setCancelled(true);
}
}
}
}
use of org.cubeengine.module.elevator.data.ElevatorData in project modules-extra by CubeEngine.
the class ElevatorListener method findNextSign.
private Vector3i findNextSign(Location<World> loc, Vector3i previous, Vector3i startPos, boolean up) {
startPos = previous == null ? startPos : previous;
// Search for next Elevator sign
BlockRay<World> ray = BlockRay.from(loc.getExtent(), startPos.toDouble()).direction(new Vector3d(0, up ? 1 : -1, 0)).narrowPhase(false).stopFilter(b -> b.getBlockY() <= loc.getExtent().getBlockMax().getY()).stopFilter(b -> b.getBlockY() >= loc.getExtent().getBlockMin().getY()).build();
while (ray.hasNext()) {
BlockRayHit<World> next = ray.next();
if (next.getBlockPosition().equals(startPos)) {
continue;
}
Optional<ElevatorData> targetData = next.getLocation().get(ElevatorData.class);
if (targetData.isPresent() && !next.getBlockPosition().equals(loc.getBlockPosition())) {
return next.getBlockPosition();
}
}
// nothing found? Return same location as before when it is valid
Optional<ElevatorData> targetData = loc.getExtent().get(startPos, ElevatorData.class);
return targetData.isPresent() ? previous : null;
}
Aggregations