ソースを参照

Refactoring schematic Paster

RedstoneFuture 11 ヶ月 前
コミット
7930c1c361

+ 51 - 32
1_16_FAWE/src/main/java/de/butzlabben/missilewars/missile/paste/r1_16/fawe/R1_16Paster.java

@@ -44,53 +44,72 @@ import java.util.logging.Level;
  */
 public class R1_16Paster {
 
-    public void pasteMissile(File schematic, Vector pos, int rotation, org.bukkit.World world,
-                             Material glassBlockReplace, int radius, Material replaceType, JavaPlugin plugin, int replaceTicks) {
+    public void pasteMissile(File schematic, Vector locationVec, int rotation, org.bukkit.World world,
+                             Material glassBlockReplace, int replaceRadius, Material replaceMaterial, int replaceTicks, JavaPlugin plugin) {
+        
+        pasteSchematic(schematic, locationVec, world, rotation, plugin);
+        
+        // Remove "Replacer-Block" after a short time to update the pasted schematic structure via (normal) WorldEdit:
+        new BukkitRunnable() {
+            @Override
+            public void run() {
+                removeTempBlock(locationVec, world, replaceRadius, replaceMaterial);
+            }
+        }.runTaskLater(plugin, replaceTicks);
+    }
+
+    /**
+     * This method executes the paste command via FAWE.
+     * 
+     * @param schematic (File) the target WorldEdit schematic file
+     * @param locationVec (Vector) 
+     * @param world (World) the target world for the WorldEdit action
+     * @param rotation (int) the target schematic rotation
+     * @param plugin (JavaPlugin) the basis plugin
+     */
+    public void pasteSchematic(File schematic, Vector locationVec, org.bukkit.World world, int rotation, JavaPlugin plugin) {
         World weWorld = new BukkitWorld(world);
+        BlockVector3 blockVec = fromBukkitVector(locationVec);
 
-        try (Clipboard clipboard = ClipboardFormats.findByFile(schematic).load(schematic);
+        try (Clipboard clipboard = ClipboardFormats.findByFile(schematic).load(schematic); 
              var session = WorldEdit.getInstance().newEditSession(weWorld)) {
+            
             ClipboardHolder clipboardHolder = new ClipboardHolder(clipboard);
             clipboardHolder.setTransform(new AffineTransform().rotateY(rotation));
-            BlockVector3 vec = fromBukkitVector(pos);
+            
             Operation pasteBuilder = clipboardHolder
                     .createPaste(session)
-                    .to(vec)
+                    .to(blockVec)
                     .ignoreAirBlocks(true)
                     .build();
-
             Operations.completeBlindly(pasteBuilder);
-
-            new BukkitRunnable() {
-                @Override
-                public void run() {
-                    var rad = BlockVector3.at(radius, radius, radius);
-                    weWorld.replaceBlocks(new CuboidRegion(vec.subtract(rad), vec.add(rad)), 
-                            Set.of(BukkitAdapter.adapt(replaceType.createBlockData()).toBaseBlock()), 
-                            BukkitAdapter.adapt(Material.AIR.createBlockData()));
-                }
-            }.runTaskLater(plugin, replaceTicks);
+            
         } catch (Exception e) {
-            plugin.getLogger().log(Level.SEVERE, "Could not paste schematic", e);
+            plugin.getLogger().log(Level.SEVERE, "Could not paste schematic '" + schematic.getName() 
+                    + "' with FAWE (" + WorldEdit.getVersion() + ")", e);
         }
     }
-
-    public void pasteSchematic(File schematic, Vector pos, org.bukkit.World world) {
+    
+    /**
+     * This method removes the temporary "Replacer-Block", so that the (asynchronously) 
+     * paste structure via FAWE gets an update.
+     * 
+     * @param locationVec (Vector) 
+     * @param world (World) the target world for the WorldEdit action
+     * @param radius (int) the configured update-radius
+     * @param replaceMaterial (Material) the target material for the replacement
+     */
+    private void removeTempBlock(Vector locationVec, org.bukkit.World world, int radius, Material replaceMaterial) {
         World weWorld = new BukkitWorld(world);
-
-        try (var clipboard = ClipboardFormats.findByFile(schematic).load(schematic);
-             var session = WorldEdit.getInstance().newEditSession(weWorld)) {
-            Operation paste = new ClipboardHolder(clipboard)
-                    .createPaste(session)
-                    .to(fromBukkitVector(pos))
-                    .ignoreAirBlocks(true)
-                    .build();
-            Operations.completeBlindly(paste);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+        BlockVector3 blockVec = fromBukkitVector(locationVec);
+        
+        var radiusVec = BlockVector3.at(radius, radius, radius);
+        
+        weWorld.replaceBlocks(new CuboidRegion(blockVec.subtract(radiusVec), blockVec.add(radiusVec)), 
+                Set.of(BukkitAdapter.adapt(replaceMaterial.createBlockData()).toBaseBlock()), 
+                BukkitAdapter.adapt(Material.AIR.createBlockData()));
     }
-
+    
     private BlockVector3 fromBukkitVector(org.bukkit.util.Vector pos) {
         return BlockVector3.at(pos.getX(), pos.getY(), pos.getZ());
     }

+ 1 - 1
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/schematics/objects/Shield.java

@@ -29,7 +29,7 @@ public class Shield extends SchematicObject {
             Location loc = ball.getLocation();
             Vector pastePos = new Vector(loc.getX(), loc.getY(), loc.getZ());
             
-            PasteProvider.getPaster().pasteSchematic(getSchematic(), pastePos, loc.getWorld());
+            PasteProvider.getPaster().pasteSchematic(getSchematic(), pastePos, 0, loc.getWorld());
         } catch (Exception e) {
             Logger.ERROR.log("Could not load " + getDisplayName());
             e.printStackTrace();

+ 2 - 2
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/schematics/paste/Paster.java

@@ -29,7 +29,7 @@ import java.io.File;
  */
 public interface Paster {
 
-    void pasteSchematic(File schematic, Vector position, org.bukkit.World world);
-
     void pasteMissile(File schematic, Vector position, int rotation, org.bukkit.World world, Team team);
+
+    void pasteSchematic(File schematic, Vector position, int rotation, org.bukkit.World world);
 }

+ 1 - 1
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/schematics/paste/R1_13FawePasteProvider.java

@@ -43,7 +43,7 @@ public class R1_13FawePasteProvider implements Paster {
     }
 
     @Override
-    public void pasteSchematic(File schematic, Vector position, World world) {
+    public void pasteSchematic(File schematic, Vector position, int rotation, World world) {
         paster.pasteSchematic(schematic, position, world);
     }
 }

+ 1 - 1
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/schematics/paste/R1_13WEPasteProvider.java

@@ -43,7 +43,7 @@ public class R1_13WEPasteProvider implements Paster {
     }
 
     @Override
-    public void pasteSchematic(File schematic, Vector position, World world) {
+    public void pasteSchematic(File schematic, Vector position, int rotation, World world) {
         paster.pasteSchematic(schematic, position, world);
     }
 }

+ 3 - 3
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/schematics/paste/R1_16FawePasteProvider.java

@@ -39,11 +39,11 @@ public class R1_16FawePasteProvider implements Paster {
     @Override
     public void pasteMissile(File schematic, Vector position, int rotation, World world, Team team) {
         paster.pasteMissile(schematic, position, rotation, world, ColorConverter.getGlassFromColorCode(team.getColorCode()),
-                Config.getReplaceRadius(), Config.getStartReplace(), MissileWars.getInstance(), Config.getReplaceTicks());
+                Config.getReplaceRadius(), Config.getStartReplace(), Config.getReplaceTicks(), MissileWars.getInstance());
     }
 
     @Override
-    public void pasteSchematic(File schematic, Vector position, World world) {
-        paster.pasteSchematic(schematic, position, world);
+    public void pasteSchematic(File schematic, Vector position, int rotation, World world) {
+        paster.pasteSchematic(schematic, position, world, rotation, MissileWars.getInstance());
     }
 }