From d241a57d3860b27f4bff20f0c155c27d350ac2a5 Mon Sep 17 00:00:00 2001 From: CocoTheOwner Date: Tue, 17 Aug 2021 18:17:13 +0200 Subject: [PATCH] Object --- .../volmit/iris/core/decrees/DecObject.java | 119 ++++++++++++++++-- 1 file changed, 109 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/volmit/iris/core/decrees/DecObject.java b/src/main/java/com/volmit/iris/core/decrees/DecObject.java index f31da5ece..d428238eb 100644 --- a/src/main/java/com/volmit/iris/core/decrees/DecObject.java +++ b/src/main/java/com/volmit/iris/core/decrees/DecObject.java @@ -1,10 +1,14 @@ package com.volmit.iris.core.decrees; +import com.volmit.iris.core.command.object.CommandIrisObjectUndo; +import com.volmit.iris.core.project.loader.IrisData; import com.volmit.iris.core.service.WandSVC; +import com.volmit.iris.engine.object.common.IObjectPlacer; import com.volmit.iris.engine.object.objects.IrisObject; import com.volmit.iris.engine.object.objects.IrisObjectPlacement; import com.volmit.iris.engine.object.objects.IrisObjectPlacementScaleInterpolator; import com.volmit.iris.engine.object.objects.IrisObjectRotation; +import com.volmit.iris.engine.object.tile.TileData; import com.volmit.iris.util.data.Cuboid; import com.volmit.iris.util.decree.DecreeExecutor; import com.volmit.iris.util.decree.DecreeOrigin; @@ -12,12 +16,14 @@ import com.volmit.iris.util.decree.annotations.Decree; import com.volmit.iris.util.decree.annotations.Param; import com.volmit.iris.util.format.C; import com.volmit.iris.util.math.Direction; +import com.volmit.iris.util.math.RNG; import com.volmit.iris.util.scheduling.Queue; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.Sound; +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.TileState; import org.bukkit.block.data.BlockData; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import java.text.NumberFormat; @@ -174,7 +180,8 @@ public class DecObject implements DecreeExecutor { player().setItemInHand(WandSVC.createWand(g[0], g[1])); } } - + private static final Set skipBlocks = Set.of(Material.GRASS, Material.SNOW, Material.VINE, Material.TORCH, Material.DEAD_BUSH, + Material.POPPY, Material.DANDELION); @Decree(description = "Paste an object") public void paste( @Param(description = "The object to paste") @@ -188,19 +195,111 @@ public class DecObject implements DecreeExecutor { @Param(description = "The scale interpolator to use", defaultValue = "none") IrisObjectPlacementScaleInterpolator interpolator ){ - double maxScale = getMaxScale(object); + double maxScale = Double.max(10 - object.getBlocks().size() / 10000d, 1); if (scale < maxScale){ sender().sendMessage(C.YELLOW + "Indicated scale exceeds maximum. Downscaled to maximum: " + maxScale); scale = maxScale; } - IrisObjectPlacement placement = new IrisObjectPlacement(); - placement.setRotation(IrisObjectRotation.of(0, rotate, 0)); sender().playSound(Sound.BLOCK_ENCHANTMENT_TABLE_USE, 1f, 1.5f); + IrisObjectPlacement placement = new IrisObjectPlacement(); + placement.setRotation(IrisObjectRotation.of(0, rotate, 0)); + + ItemStack wand = player().getInventory().getItemInMainHand(); + Location block = player().getTargetBlock(skipBlocks, 256).getLocation().clone().add(0, 1, 0); + + Map futureChanges = new HashMap<>(); + + object = object.scaled(scale, interpolator); + object.place(block.getBlockX(), block.getBlockY() + (int) object.getCenter().getY(), block.getBlockZ(), createPlacer(block.getWorld(), futureChanges), placement, new RNG(), null); + + CommandIrisObjectUndo.addChanges(player(), futureChanges); + + if (edit) { + ItemStack newWand = WandSVC.createWand(block.clone().subtract(object.getCenter()).add(object.getW() - 1, + object.getH() + object.getCenter().clone().getY() - 1, object.getD() - 1), block.clone().subtract(object.getCenter().clone().setY(0))); + if (WandSVC.isWand(wand)) { + wand = newWand; + player().getInventory().setItemInMainHand(wand); + sender().sendMessage("Updated wand for " + "objects/" + object.getLoadKey() + ".iob"); + } else { + int slot = WandSVC.findWand(player().getInventory()); + if (slot == -1) { + player().getInventory().addItem(newWand); + sender().sendMessage("Given new wand for " + "objects/" + object.getLoadKey() + ".iob"); + } else { + player().getInventory().setItem(slot, newWand); + sender().sendMessage("Updated wand for " + "objects/" + object.getLoadKey() + ".iob"); + } + } + } else { + sender().sendMessage("Placed " + "objects/" + object.getLoadKey() + ".iob"); + } } - private double getMaxScale(IrisObject object){ - return Double.max(10 - object.getBlocks().size() / 10000d, 1); + public static IObjectPlacer createPlacer(World world, Map futureBlockChanges) { + + return new IObjectPlacer() { + @Override + public int getHighest(int x, int z, IrisData data) { + return world.getHighestBlockYAt(x, z); + } + + @Override + public int getHighest(int x, int z, IrisData data, boolean ignoreFluid) { + return world.getHighestBlockYAt(x, z, ignoreFluid ? HeightMap.OCEAN_FLOOR : HeightMap.MOTION_BLOCKING); + } + + @Override + public void set(int x, int y, int z, BlockData d) { + Block block = world.getBlockAt(x, y, z); + + //Prevent blocks being set in or bellow bedrock + if (y <= world.getMinHeight() || block.getType() == Material.BEDROCK) return; + + futureBlockChanges.put(block, block.getBlockData()); + + block.setBlockData(d); + } + + @Override + public BlockData get(int x, int y, int z) { + return world.getBlockAt(x, y, z).getBlockData(); + } + + @Override + public boolean isPreventingDecay() { + return false; + } + + @Override + public boolean isSolid(int x, int y, int z) { + return world.getBlockAt(x, y, z).getType().isSolid(); + } + + @Override + public boolean isUnderwater(int x, int z) { + return false; + } + + @Override + public int getFluidHeight() { + return 63; + } + + @Override + public boolean isDebugSmartBore() { + return false; + } + + @Override + public void setTile(int xx, int yy, int zz, TileData tile) { + BlockState state = world.getBlockAt(xx, yy, zz).getState(); + tile.toBukkitTry(state); + state.update(); + } + }; } + }