use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.
the class TeleportSign method onSignClick.
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
BlockState state = event.getClickedBlock().getState();
if (!(state instanceof Sign)) {
return;
}
Sign sign = (Sign) state;
if (!ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(HEADER)) {
return;
}
if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) {
return;
}
int tX = sign.getX();
int tY = sign.getY();
int tZ = sign.getZ();
String[] numbers = ChatColor.stripColor(sign.getLine(1)).replaceAll(" ", "").split(",");
if (numbers.length >= 3) {
try {
tX = Integer.parseInt(numbers[0]);
tY = Integer.parseInt(numbers[1]);
tZ = Integer.parseInt(numbers[2]);
} catch (NumberFormatException e) {
event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Invalid Coordinates"));
return;
}
}
String w = ChatColor.stripColor(sign.getLine(2));
World world = Bukkit.getWorld(w);
if (world == null)
world = sign.getWorld();
if (!event.getPlayer().hasPermission("movecraft." + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) {
event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
return;
}
final Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer());
if (c == null || !c.getType().getBoolProperty(CraftType.CAN_TELEPORT)) {
return;
}
long timeSinceLastTeleport = System.currentTimeMillis() - c.getLastTeleportTime();
if (c.getType().getIntProperty(CraftType.TELEPORTATION_COOLDOWN) > 0 && timeSinceLastTeleport < c.getType().getIntProperty(CraftType.TELEPORTATION_COOLDOWN)) {
event.getPlayer().sendMessage(String.format(I18nSupport.getInternationalisedString("Teleportation - Cooldown active"), timeSinceLastTeleport));
return;
}
int dx = tX - sign.getX();
int dy = tY - sign.getY();
int dz = tZ - sign.getZ();
c.translate(world, dx, dy, dz);
c.setLastTeleportTime(System.currentTimeMillis());
}
use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.
the class AsyncManager method processAlgorithmQueue.
private void processAlgorithmQueue() {
int runLength = 10;
int queueLength = finishedAlgorithms.size();
runLength = Math.min(runLength, queueLength);
for (int i = 0; i < runLength; i++) {
boolean sentMapUpdate = false;
AsyncTask poll = finishedAlgorithms.poll();
Craft c = ownershipMap.get(poll);
if (poll instanceof TranslationTask) {
// Process translation task
TranslationTask task = (TranslationTask) poll;
sentMapUpdate = processTranslation(task, c);
} else if (poll instanceof RotationTask) {
// Process rotation task
RotationTask task = (RotationTask) poll;
sentMapUpdate = processRotation(task, c);
}
ownershipMap.remove(poll);
// will mark the crafts once it is done with them.
if (!sentMapUpdate) {
clear(c);
}
}
}
use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.
the class RotationTask method execute.
@Override
protected void execute() {
if (oldHitBox.isEmpty())
return;
if (getCraft().getDisabled() && !(craft instanceof SinkingCraft)) {
failed = true;
failMessage = I18nSupport.getInternationalisedString("Translation - Failed Craft Is Disabled");
}
// check for fuel, burn some from a furnace if needed. Blocks of coal are supported, in addition to coal and charcoal
if (!checkFuel()) {
failMessage = I18nSupport.getInternationalisedString("Translation - Failed Craft out of fuel");
failed = true;
return;
}
// if a subcraft, find the parent craft. If not a subcraft, it is it's own parent
Set<Craft> craftsInWorld = CraftManager.getInstance().getCraftsInWorld(getCraft().getWorld());
Craft parentCraft = getCraft();
for (Craft craft : craftsInWorld) {
if (craft != getCraft() && !craft.getHitBox().intersection(oldHitBox).isEmpty()) {
parentCraft = craft;
break;
}
}
for (MovecraftLocation originalLocation : oldHitBox) {
MovecraftLocation newLocation = MathUtils.rotateVec(rotation, originalLocation.subtract(originPoint)).add(originPoint);
newHitBox.add(newLocation);
Material oldMaterial = originalLocation.toBukkit(w).getBlock().getType();
// prevent chests collision
if (Tags.CHESTS.contains(oldMaterial) && !checkChests(oldMaterial, newLocation)) {
failed = true;
failMessage = String.format(I18nSupport.getInternationalisedString("Rotation - Craft is obstructed") + " @ %d,%d,%d", newLocation.getX(), newLocation.getY(), newLocation.getZ());
break;
}
if (!withinWorldBorder(craft.getWorld(), newLocation)) {
failMessage = I18nSupport.getInternationalisedString("Rotation - Failed Craft cannot pass world border") + String.format(" @ %d,%d,%d", newLocation.getX(), newLocation.getY(), newLocation.getZ());
failed = true;
return;
}
Material newMaterial = newLocation.toBukkit(w).getBlock().getType();
if (newMaterial.isAir() || (newMaterial == Material.PISTON_HEAD) || craft.getType().getMaterialSetProperty(CraftType.PASSTHROUGH_BLOCKS).contains(newMaterial))
continue;
if (!oldHitBox.contains(newLocation)) {
failed = true;
failMessage = String.format(I18nSupport.getInternationalisedString("Rotation - Craft is obstructed") + " @ %d,%d,%d", newLocation.getX(), newLocation.getY(), newLocation.getZ());
break;
}
}
if (!oldFluidList.isEmpty()) {
for (MovecraftLocation fluidLoc : oldFluidList) {
newFluidList.add(MathUtils.rotateVec(rotation, fluidLoc.subtract(originPoint)).add(originPoint));
}
}
if (failed) {
if (this.isSubCraft && parentCraft != getCraft()) {
parentCraft.setProcessing(false);
}
return;
}
// call event
CraftRotateEvent event = new CraftRotateEvent(craft, rotation, originPoint, oldHitBox, newHitBox);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
failed = true;
failMessage = event.getFailMessage();
return;
}
if (parentCraft != craft) {
parentCraft.getFluidLocations().removeAll(oldFluidList);
parentCraft.getFluidLocations().addAll(newFluidList);
}
updates.add(new CraftRotateCommand(getCraft(), originPoint, rotation));
// rotate entities in the craft
Location tOP = new Location(getCraft().getWorld(), originPoint.getX(), originPoint.getY(), originPoint.getZ());
tOP.setX(tOP.getBlockX() + 0.5);
tOP.setZ(tOP.getBlockZ() + 0.5);
if (!(craft instanceof SinkingCraft && craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS)) && craft.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) {
Location midpoint = new Location(craft.getWorld(), (oldHitBox.getMaxX() + oldHitBox.getMinX()) / 2.0, (oldHitBox.getMaxY() + oldHitBox.getMinY()) / 2.0, (oldHitBox.getMaxZ() + oldHitBox.getMinZ()) / 2.0);
for (Entity entity : craft.getWorld().getNearbyEntities(midpoint, oldHitBox.getXLength() / 2.0 + 1, oldHitBox.getYLength() / 2.0 + 2, oldHitBox.getZLength() / 2.0 + 1)) {
if (!craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) || ((entity.getType() == EntityType.PLAYER || entity.getType() == EntityType.PRIMED_TNT) && !(craft instanceof SinkingCraft))) {
// Player is onboard this craft
Location adjustedPLoc = entity.getLocation().subtract(tOP);
double[] rotatedCoords = MathUtils.rotateVecNoRound(rotation, adjustedPLoc.getX(), adjustedPLoc.getZ());
float newYaw = rotation == MovecraftRotation.CLOCKWISE ? 90F : -90F;
EntityUpdateCommand eUp = new EntityUpdateCommand(entity, rotatedCoords[0] + tOP.getX() - entity.getLocation().getX(), 0, rotatedCoords[1] + tOP.getZ() - entity.getLocation().getZ(), newYaw, 0);
updates.add(eUp);
}
}
}
if (getCraft().getCruising()) {
if (rotation == MovecraftRotation.ANTICLOCKWISE) {
// ship faces west
switch(getCraft().getCruiseDirection()) {
case WEST:
getCraft().setCruiseDirection(CruiseDirection.SOUTH);
break;
// ship faces east
case EAST:
getCraft().setCruiseDirection(CruiseDirection.NORTH);
break;
// ship faces north
case SOUTH:
getCraft().setCruiseDirection(CruiseDirection.EAST);
break;
// ship faces south
case NORTH:
getCraft().setCruiseDirection(CruiseDirection.WEST);
break;
}
} else if (rotation == MovecraftRotation.CLOCKWISE) {
// ship faces west
switch(getCraft().getCruiseDirection()) {
case WEST:
getCraft().setCruiseDirection(CruiseDirection.NORTH);
break;
// ship faces east
case EAST:
getCraft().setCruiseDirection(CruiseDirection.SOUTH);
break;
// ship faces north
case SOUTH:
getCraft().setCruiseDirection(CruiseDirection.WEST);
break;
// ship faces south
case NORTH:
getCraft().setCruiseDirection(CruiseDirection.EAST);
break;
}
}
}
// if you rotated a subcraft, update the parent with the new blocks
if (this.isSubCraft) {
// also find the furthest extent from center and notify the player of the new direction
int farthestX = 0;
int farthestZ = 0;
for (MovecraftLocation loc : newHitBox) {
if (Math.abs(loc.getX() - originPoint.getX()) > Math.abs(farthestX))
farthestX = loc.getX() - originPoint.getX();
if (Math.abs(loc.getZ() - originPoint.getZ()) > Math.abs(farthestZ))
farthestZ = loc.getZ() - originPoint.getZ();
}
Component faceMessage = I18nSupport.getInternationalisedComponent("Rotation - Farthest Extent Facing").append(Component.text(" "));
if (Math.abs(farthestX) > Math.abs(farthestZ)) {
if (farthestX > 0) {
faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - East"));
} else {
faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - West"));
}
} else {
if (farthestZ > 0) {
faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - South"));
} else {
faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - North"));
}
}
getCraft().getAudience().sendMessage(faceMessage);
craftsInWorld = CraftManager.getInstance().getCraftsInWorld(getCraft().getWorld());
for (Craft craft : craftsInWorld) {
if (!newHitBox.intersection(craft.getHitBox()).isEmpty() && craft != getCraft()) {
// craft.setHitBox(newHitBox);
if (Settings.Debug) {
Bukkit.broadcastMessage(String.format("Size of %s hitbox: %d, Size of %s hitbox: %d", this.craft.getType().getStringProperty(CraftType.NAME), newHitBox.size(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size()));
}
craft.setHitBox(craft.getHitBox().difference(oldHitBox).union(newHitBox));
if (Settings.Debug) {
Bukkit.broadcastMessage(String.format("Hitbox of craft %s intersects hitbox of craft %s", this.craft.getType().getStringProperty(CraftType.NAME), craft.getType().getStringProperty(CraftType.NAME)));
Bukkit.broadcastMessage(String.format("Size of %s hitbox: %d, Size of %s hitbox: %d", this.craft.getType().getStringProperty(CraftType.NAME), newHitBox.size(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size()));
}
break;
}
}
}
}
use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.
the class ScuttleCommand method onCommand.
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
if (!command.getName().equalsIgnoreCase("scuttle")) {
return false;
}
Craft craft = null;
// Scuttle other player
if (commandSender.hasPermission("movecraft.commands.scuttle.others") && strings.length >= 1) {
Player player = Bukkit.getPlayer(strings[0]);
if (player == null) {
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Must Be Online"));
return true;
}
craft = CraftManager.getInstance().getCraftByPlayer(player);
} else if (commandSender.hasPermission("movecraft.commands.scuttle.self") && strings.length == 0) {
if (!(commandSender instanceof Player)) {
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Must Be Player"));
return true;
}
craft = CraftManager.getInstance().getCraftByPlayer(Bukkit.getPlayer(commandSender.getName()));
}
if (craft == null) {
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft"));
return true;
}
if (craft instanceof SinkingCraft) {
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Craft Already Sinking"));
return true;
}
if (!commandSender.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".scuttle")) {
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions"));
return true;
}
CraftScuttleEvent e = new CraftScuttleEvent(craft, (Player) commandSender);
Bukkit.getServer().getPluginManager().callEvent(e);
if (e.isCancelled())
return true;
craft.setCruising(false);
CraftManager.getInstance().sink(craft);
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Scuttle Activated"));
return true;
}
use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.
the class CraftTranslateCommand method doUpdate.
@Override
public void doUpdate() {
final Logger logger = Movecraft.getInstance().getLogger();
if (craft.getHitBox().isEmpty()) {
logger.warning("Attempted to move craft with empty HashHitBox!");
CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.EMPTY, false);
return;
}
long time = System.nanoTime();
World oldWorld = craft.getWorld();
final Set<Material> passthroughBlocks = new HashSet<>(craft.getType().getMaterialSetProperty(CraftType.PASSTHROUGH_BLOCKS));
if (craft instanceof SinkingCraft) {
passthroughBlocks.addAll(Tags.FLUID);
passthroughBlocks.addAll(Tag.LEAVES.getValues());
passthroughBlocks.addAll(Tags.SINKING_PASSTHROUGH);
}
if (passthroughBlocks.isEmpty()) {
// translate the craft
Movecraft.getInstance().getWorldHandler().translateCraft(craft, displacement, world);
craft.setWorld(world);
// trigger sign events
sendSignEvents();
} else {
SetHitBox originalLocations = new SetHitBox();
for (MovecraftLocation movecraftLocation : craft.getHitBox()) {
originalLocations.add(movecraftLocation.subtract(displacement));
}
final Set<MovecraftLocation> to = Sets.difference(craft.getHitBox().asSet(), originalLocations.asSet());
// place phased blocks
for (MovecraftLocation location : to) {
var data = location.toBukkit(world).getBlock().getBlockData();
if (passthroughBlocks.contains(data.getMaterial())) {
craft.getPhaseBlocks().put(location.toBukkit(world), data);
}
}
// The subtraction of the set of coordinates in the HitBox cube and the HitBox itself
final var invertedHitBox = Sets.difference(craft.getHitBox().boundingHitBox().asSet(), craft.getHitBox().asSet());
// place phased blocks
final Set<Location> overlap = new HashSet<>(craft.getPhaseBlocks().keySet());
overlap.removeIf((location -> !craft.getHitBox().contains(MathUtils.bukkit2MovecraftLoc(location))));
final int minX = craft.getHitBox().getMinX();
final int maxX = craft.getHitBox().getMaxX();
final int minY = craft.getHitBox().getMinY();
final int maxY = overlap.isEmpty() ? craft.getHitBox().getMaxY() : Collections.max(overlap, Comparator.comparingInt(Location::getBlockY)).getBlockY();
final int minZ = craft.getHitBox().getMinZ();
final int maxZ = craft.getHitBox().getMaxZ();
final HitBox[] surfaces = { new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(minX, maxY, maxZ)), new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(maxX, maxY, minZ)), new SolidHitBox(new MovecraftLocation(maxX, minY, maxZ), new MovecraftLocation(minX, maxY, maxZ)), new SolidHitBox(new MovecraftLocation(maxX, minY, maxZ), new MovecraftLocation(maxX, maxY, minZ)), new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(maxX, minY, maxZ)) };
final SetHitBox validExterior = new SetHitBox();
for (HitBox hitBox : surfaces) {
validExterior.addAll(Sets.difference(hitBox.asSet(), craft.getHitBox().asSet()));
}
// Check to see which locations in the from set are actually outside of the craft
final Set<MovecraftLocation> confirmed = craft instanceof SinkingCraft ? invertedHitBox.copyInto(new LinkedHashSet<>()) : verifyExterior(invertedHitBox, validExterior);
// A set of locations that are confirmed to be "exterior" locations
final Set<MovecraftLocation> failed = Sets.difference(invertedHitBox, confirmed).copyInto(new LinkedHashSet<>());
final WorldHandler handler = Movecraft.getInstance().getWorldHandler();
for (MovecraftLocation location : failed) {
var data = location.toBukkit(world).getBlock().getBlockData();
if (!passthroughBlocks.contains(data.getMaterial()))
continue;
craft.getPhaseBlocks().put(location.toBukkit(world), data);
}
// translate the craft
handler.translateCraft(craft, displacement, world);
craft.setWorld(world);
// trigger sign events
sendSignEvents();
for (MovecraftLocation l : failed) {
MovecraftLocation orig = l.subtract(displacement);
if (craft.getHitBox().contains(orig) || failed.contains(orig)) {
continue;
}
confirmed.add(orig);
}
// place confirmed blocks if they have been un-phased
for (MovecraftLocation location : confirmed) {
Location bukkit = location.toBukkit(craft.getWorld());
if (!craft.getPhaseBlocks().containsKey(bukkit))
continue;
// Do not place if it is at a collapsed HitBox location
if (!craft.getCollapsedHitBox().isEmpty() && craft.getCollapsedHitBox().contains(location))
continue;
var phaseBlock = craft.getPhaseBlocks().remove(bukkit);
handler.setBlockFast(bukkit, phaseBlock);
craft.getPhaseBlocks().remove(bukkit);
}
for (MovecraftLocation location : originalLocations) {
Location bukkit = location.toBukkit(oldWorld);
if (!craft.getHitBox().contains(location) && craft.getPhaseBlocks().containsKey(bukkit)) {
var phaseBlock = craft.getPhaseBlocks().remove(bukkit);
handler.setBlockFast(bukkit, phaseBlock);
}
}
for (MovecraftLocation location : failed) {
Location bukkit = location.toBukkit(oldWorld);
var data = bukkit.getBlock().getBlockData();
if (passthroughBlocks.contains(data.getMaterial())) {
craft.getPhaseBlocks().put(bukkit, data);
handler.setBlockFast(bukkit, Material.AIR.createBlockData());
}
}
// waterlog();
}
if (!craft.isNotProcessing())
craft.setProcessing(false);
time = System.nanoTime() - time;
if (Settings.Debug)
logger.info("Total time: " + (time / 1e6) + " milliseconds. Moving with cooldown of " + craft.getTickCooldown() + ". Speed of: " + String.format("%.2f", craft.getSpeed()) + ". Displacement of: " + displacement);
// Only add cruise time if cruising
if (craft.getCruising() && displacement.getY() == 0 && (displacement.getX() == 0 || displacement.getZ() == 0))
craft.addCruiseTime(time / 1e9f);
}
Aggregations