use of net.countercraft.movecraft.MovecraftRotation in project Movecraft by APDevTeam.
the class IWorldHandler method rotateCraft.
@Override
public void rotateCraft(@NotNull Craft craft, @NotNull MovecraftLocation originPoint, @NotNull MovecraftRotation rotation) {
// *******************************************
// * Step one: Convert to Positions *
// *******************************************
HashMap<BlockPosition, BlockPosition> rotatedPositions = new HashMap<>();
MovecraftRotation counterRotation = rotation == MovecraftRotation.CLOCKWISE ? MovecraftRotation.ANTICLOCKWISE : MovecraftRotation.CLOCKWISE;
for (MovecraftLocation newLocation : craft.getHitBox()) {
rotatedPositions.put(locationToPosition(MathUtils.rotateVec(counterRotation, newLocation.subtract(originPoint)).add(originPoint)), locationToPosition(newLocation));
}
// *******************************************
// * Step two: Get the tiles *
// *******************************************
WorldServer nativeWorld = ((CraftWorld) craft.getWorld()).getHandle();
List<TileHolder> tiles = new ArrayList<>();
// get the tiles
for (BlockPosition position : rotatedPositions.keySet()) {
// TileEntity tile = nativeWorld.removeTileEntity(position);
TileEntity tile = removeTileEntity(nativeWorld, position);
if (tile == null)
continue;
tile.a(ROTATION[rotation.ordinal()]);
// get the nextTick to move with the tile
tiles.add(new TileHolder(tile, tickProvider.getNextTick(nativeWorld, position), position));
}
// *******************************************
// * Step three: Translate all the blocks *
// *******************************************
// blockedByWater=false means an ocean-going vessel
// TODO: Simplify
// TODO: go by chunks
// TODO: Don't move unnecessary blocks
// get the blocks and rotate them
HashMap<BlockPosition, IBlockData> blockData = new HashMap<>();
for (BlockPosition position : rotatedPositions.keySet()) {
blockData.put(position, nativeWorld.getType(position).a(ROTATION[rotation.ordinal()]));
}
// create the new block
for (Map.Entry<BlockPosition, IBlockData> entry : blockData.entrySet()) {
setBlockFast(nativeWorld, rotatedPositions.get(entry.getKey()), entry.getValue());
}
// TODO: go by chunks
for (TileHolder tileHolder : tiles) {
moveTileEntity(nativeWorld, rotatedPositions.get(tileHolder.getTilePosition()), tileHolder.getTile());
if (tileHolder.getNextTick() == null)
continue;
final long currentTime = nativeWorld.worldData.getTime();
nativeWorld.getBlockTickList().a(rotatedPositions.get(tileHolder.getNextTick().a), (Block) tileHolder.getNextTick().b(), (int) (tileHolder.getNextTick().b - currentTime), tileHolder.getNextTick().c);
}
// *******************************************
// * Step five: Destroy the leftovers *
// *******************************************
// TODO: add support for pass-through
Collection<BlockPosition> deletePositions = CollectionUtils.filter(rotatedPositions.keySet(), rotatedPositions.values());
for (BlockPosition position : deletePositions) {
setBlockFast(nativeWorld, position, Blocks.AIR.getBlockData());
}
}
use of net.countercraft.movecraft.MovecraftRotation in project Movecraft by APDevTeam.
the class SubcraftRotateSign method onSignClick.
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
MovecraftRotation rotation;
if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
rotation = MovecraftRotation.CLOCKWISE;
else if (event.getAction() == Action.LEFT_CLICK_BLOCK)
rotation = MovecraftRotation.ANTICLOCKWISE;
else
return;
BlockState state = event.getClickedBlock().getState();
if (!(state instanceof Sign))
return;
Sign sign = (Sign) state;
if (!ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(HEADER))
return;
Location loc = event.getClickedBlock().getLocation();
MovecraftLocation startPoint = new MovecraftLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
if (rotating.contains(startPoint)) {
event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Rotation - Already Rotating"));
event.setCancelled(true);
return;
}
// rotate subcraft
String craftTypeStr = ChatColor.stripColor(sign.getLine(1));
CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(craftTypeStr);
if (craftType == null)
return;
if (ChatColor.stripColor(sign.getLine(2)).equals("") && ChatColor.stripColor(sign.getLine(3)).equals("")) {
sign.setLine(2, "_\\ /_");
sign.setLine(3, "/ \\");
sign.update(false, false);
}
if (!event.getPlayer().hasPermission("movecraft." + craftTypeStr + ".pilot") || !event.getPlayer().hasPermission("movecraft." + craftTypeStr + ".rotate")) {
event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
return;
}
Craft playerCraft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer());
if (playerCraft != null) {
if (!playerCraft.isNotProcessing()) {
event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Detection - Parent Craft is busy"));
return;
}
// prevent the parent craft from moving or updating until the subcraft is done
playerCraft.setProcessing(true);
new BukkitRunnable() {
@Override
public void run() {
playerCraft.setProcessing(false);
}
}.runTaskLater(Movecraft.getInstance(), (10));
}
rotating.add(startPoint);
Player player = event.getPlayer();
World world = event.getClickedBlock().getWorld();
CraftManager.getInstance().detect(startPoint, craftType, (type, w, p, parents) -> {
if (parents.size() > 1)
return new Pair<>(Result.failWithMessage(I18nSupport.getInternationalisedString("Detection - Failed - Already commanding a craft")), null);
if (parents.size() < 1)
return new Pair<>(Result.succeed(), new SubcraftRotateCraft(type, w, p));
Craft parent = parents.iterator().next();
return new Pair<>(Result.succeed(), new SubCraftImpl(type, w, parent));
}, world, player, Movecraft.getAdventure().player(player), craft -> () -> {
Bukkit.getServer().getPluginManager().callEvent(new CraftPilotEvent(craft, CraftPilotEvent.Reason.SUB_CRAFT));
if (craft instanceof SubCraft) {
// Subtract craft from the parent
Craft parent = ((SubCraft) craft).getParent();
var newHitbox = parent.getHitBox().difference(craft.getHitBox());
;
parent.setHitBox(newHitbox);
}
new BukkitRunnable() {
@Override
public void run() {
craft.rotate(rotation, startPoint, true);
if (craft instanceof SubCraft) {
Craft parent = ((SubCraft) craft).getParent();
var newHitbox = parent.getHitBox().union(craft.getHitBox());
parent.setHitBox(newHitbox);
}
CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.SUB_CRAFT, false);
}
}.runTaskLater(Movecraft.getInstance(), 3);
});
event.setCancelled(true);
new BukkitRunnable() {
@Override
public void run() {
rotating.remove(startPoint);
}
}.runTaskLater(Movecraft.getInstance(), 4);
}
use of net.countercraft.movecraft.MovecraftRotation in project Movecraft by APDevTeam.
the class HelmSign method onSignClick.
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
MovecraftRotation rotation;
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
rotation = MovecraftRotation.CLOCKWISE;
} else if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
rotation = MovecraftRotation.ANTICLOCKWISE;
} else {
return;
}
BlockState state = event.getClickedBlock().getState();
if (!(state instanceof Sign)) {
return;
}
Sign sign = (Sign) state;
if (!(ChatColor.stripColor(sign.getLine(0)).equals("\\ || /") && ChatColor.stripColor(sign.getLine(1)).equals("== ==") && ChatColor.stripColor(sign.getLine(2)).equals("/ || \\"))) {
return;
}
Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer());
if (craft == null) {
return;
}
if (!event.getPlayer().hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) {
event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
return;
}
if (!MathUtils.locIsNearCraftFast(craft, MathUtils.bukkit2MovecraftLoc(event.getPlayer().getLocation())))
return;
if (craft.getType().getBoolProperty(CraftType.ROTATE_AT_MIDPOINT)) {
CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).rotate(rotation, craft.getHitBox().getMidPoint());
} else {
CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).rotate(rotation, MathUtils.bukkit2MovecraftLoc(sign.getLocation()));
}
// timeMap.put(event.getPlayer(), System.currentTimeMillis());
event.setCancelled(true);
// TODO: Lower speed while turning
/*int curTickCooldown = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getCurTickCooldown();
int baseTickCooldown = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getCruiseTickCooldown();
if (curTickCooldown * 2 > baseTickCooldown)
curTickCooldown = baseTickCooldown;
else
curTickCooldown = curTickCooldown * 2;*/
// CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).setCurTickCooldown(curTickCooldown); // lose half your speed when turning
}
use of net.countercraft.movecraft.MovecraftRotation in project Movecraft by APDevTeam.
the class CraftRotateCommand 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();
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()) {
SetHitBox originalLocations = new SetHitBox();
final MovecraftRotation counterRotation = rotation == MovecraftRotation.CLOCKWISE ? MovecraftRotation.ANTICLOCKWISE : MovecraftRotation.CLOCKWISE;
for (MovecraftLocation movecraftLocation : craft.getHitBox()) {
originalLocations.add(MathUtils.rotateVec(counterRotation, movecraftLocation.subtract(originLocation)).add(originLocation));
}
final HitBox to = craft.getHitBox().difference(originalLocations);
for (MovecraftLocation location : to) {
var data = location.toBukkit(craft.getWorld()).getBlock().getBlockData();
if (passthroughBlocks.contains(data.getMaterial())) {
craft.getPhaseBlocks().put(location.toBukkit(craft.getWorld()), data);
}
}
// The subtraction of the set of coordinates in the HitBox cube and the HitBox itself
final HitBox invertedHitBox = new SetHitBox(craft.getHitBox().boundingHitBox()).difference(craft.getHitBox());
// A set of locations that are confirmed to be "exterior" locations
final SetHitBox exterior = new SetHitBox();
final SetHitBox interior = new SetHitBox();
// place phased blocks
final Set<Location> overlap = new HashSet<>(craft.getPhaseBlocks().keySet());
overlap.retainAll(craft.getHitBox().asSet().stream().map(l -> l.toBukkit(craft.getWorld())).collect(Collectors.toSet()));
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)) };
// Valid exterior starts as the 6 surface planes of the HitBox with the locations that lie in the HitBox removed
final SetHitBox validExterior = new SetHitBox();
for (HitBox hitBox : surfaces) {
validExterior.addAll(hitBox.difference(craft.getHitBox()));
}
// Check to see which locations in the from set are actually outside of the craft
for (MovecraftLocation location : validExterior) {
if (craft.getHitBox().contains(location) || exterior.contains(location)) {
continue;
}
// use a modified BFS for multiple origin elements
SetHitBox visited = new SetHitBox();
Queue<MovecraftLocation> queue = new LinkedList<>();
queue.add(location);
while (!queue.isEmpty()) {
MovecraftLocation node = queue.poll();
// If the node is already a valid member of the exterior of the HitBox, continued search is unitary.
for (MovecraftLocation neighbor : CollectionUtils.neighbors(invertedHitBox, node)) {
if (visited.contains(neighbor)) {
continue;
}
visited.add(neighbor);
queue.add(neighbor);
}
}
exterior.addAll(visited);
}
interior.addAll(invertedHitBox.difference(exterior));
final WorldHandler handler = Movecraft.getInstance().getWorldHandler();
for (MovecraftLocation location : invertedHitBox.difference(exterior)) {
var data = location.toBukkit(craft.getWorld()).getBlock().getBlockData();
if (!passthroughBlocks.contains(data.getMaterial())) {
continue;
}
craft.getPhaseBlocks().put(location.toBukkit(craft.getWorld()), data);
}
// translate the craft
handler.rotateCraft(craft, originLocation, rotation);
// trigger sign events
sendSignEvents();
// place confirmed blocks if they have been un-phased
for (MovecraftLocation location : exterior) {
Location bukkit = location.toBukkit(craft.getWorld());
if (!craft.getPhaseBlocks().containsKey(bukkit)) {
continue;
}
var phaseBlock = craft.getPhaseBlocks().remove(bukkit);
handler.setBlockFast(bukkit, phaseBlock);
craft.getPhaseBlocks().remove(bukkit);
}
for (MovecraftLocation location : originalLocations.boundingHitBox()) {
Location bukkit = location.toBukkit(craft.getWorld());
if (!craft.getHitBox().inBounds(location) && craft.getPhaseBlocks().containsKey(bukkit)) {
var phaseBlock = craft.getPhaseBlocks().remove(bukkit);
handler.setBlockFast(bukkit, phaseBlock);
}
}
for (MovecraftLocation location : interior) {
Location bukkit = location.toBukkit(craft.getWorld());
var data = bukkit.getBlock().getBlockData();
if (passthroughBlocks.contains(data.getMaterial())) {
craft.getPhaseBlocks().put(bukkit, data);
handler.setBlockFast(bukkit, Material.AIR.createBlockData());
}
}
} else {
// translate the craft
Movecraft.getInstance().getWorldHandler().rotateCraft(craft, originLocation, rotation);
// trigger sign events
sendSignEvents();
}
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()));
}
use of net.countercraft.movecraft.MovecraftRotation in project Movecraft by APDevTeam.
the class IWorldHandler method rotateCraft.
@Override
public void rotateCraft(@NotNull Craft craft, @NotNull MovecraftLocation originPoint, @NotNull MovecraftRotation rotation) {
// *******************************************
// * Step one: Convert to Positions *
// *******************************************
HashMap<BlockPosition, BlockPosition> rotatedPositions = new HashMap<>();
MovecraftRotation counterRotation = rotation == MovecraftRotation.CLOCKWISE ? MovecraftRotation.ANTICLOCKWISE : MovecraftRotation.CLOCKWISE;
for (MovecraftLocation newLocation : craft.getHitBox()) {
rotatedPositions.put(locationToPosition(MathUtils.rotateVec(counterRotation, newLocation.subtract(originPoint)).add(originPoint)), locationToPosition(newLocation));
}
// *******************************************
// * Step two: Get the tiles *
// *******************************************
WorldServer nativeWorld = ((CraftWorld) craft.getWorld()).getHandle();
List<TileHolder> tiles = new ArrayList<>();
// get the tiles
for (BlockPosition position : rotatedPositions.keySet()) {
// TileEntity tile = nativeWorld.removeTileEntity(position);
TileEntity tile = removeTileEntity(nativeWorld, position);
if (tile == null)
continue;
tile.a(ROTATION[rotation.ordinal()]);
// get the nextTick to move with the tile
tiles.add(new TileHolder(tile, tickProvider.getNextTick(nativeWorld, position), position));
}
// *******************************************
// * Step three: Translate all the blocks *
// *******************************************
// blockedByWater=false means an ocean-going vessel
// TODO: Simplify
// TODO: go by chunks
// TODO: Don't move unnecessary blocks
// get the blocks and rotate them
HashMap<BlockPosition, IBlockData> blockData = new HashMap<>();
for (BlockPosition position : rotatedPositions.keySet()) {
blockData.put(position, nativeWorld.getType(position).a(ROTATION[rotation.ordinal()]));
}
// create the new block
for (Map.Entry<BlockPosition, IBlockData> entry : blockData.entrySet()) {
setBlockFast(nativeWorld, rotatedPositions.get(entry.getKey()), entry.getValue());
}
// TODO: go by chunks
for (TileHolder tileHolder : tiles) {
moveTileEntity(nativeWorld, rotatedPositions.get(tileHolder.getTilePosition()), tileHolder.getTile());
if (tileHolder.getNextTick() == null)
continue;
final long currentTime = nativeWorld.worldData.getTime();
nativeWorld.getBlockTickList().a(rotatedPositions.get(tileHolder.getNextTick().a), (Block) tileHolder.getNextTick().b(), (int) (tileHolder.getNextTick().b - currentTime), tileHolder.getNextTick().c);
}
// *******************************************
// * Step five: Destroy the leftovers *
// *******************************************
// TODO: add support for pass-through
Collection<BlockPosition> deletePositions = CollectionUtils.filter(rotatedPositions.keySet(), rotatedPositions.values());
for (BlockPosition position : deletePositions) {
setBlockFast(nativeWorld, position, Blocks.AIR.getBlockData());
}
}
Aggregations