SteamWar/FightSystem
Archiviert
13
1

Schematic pasting refactoring #286

Manuell gemergt
Lixfel hat 5 Commits von weRefactoring nach master 2021-09-11 17:57:23 +02:00 zusammengeführt
5 geänderte Dateien mit 59 neuen und 87 gelöschten Zeilen
Nur Änderungen aus Commit 453dea774e werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -39,12 +39,12 @@ import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.FightSystem;
import de.steamwar.sql.Schematic; import de.steamwar.sql.Schematic;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.util.Vector;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -88,45 +88,24 @@ public class WorldeditWrapper14 implements WorldeditWrapper.IWorldeditWrapper {
} }
@Override @Override
public void pasteSchematic(Clipboard clipboard, Region region, boolean rotate) { public int getWaterDepth(Clipboard clipboard) {
AffineTransform aT = new AffineTransform().rotateY(rotate ? 180 : 0);
BlockVector3 dimensions = clipboard.getDimensions();
BlockVector3 v = BlockVector3.at(region.centerX(), region.getMinY(), region.centerZ());
if(rotate){
v = v.add(dimensions.getX()/2 + dimensions.getX()%2, 0, dimensions.getZ()/2 + dimensions.getZ()%2).subtract(1, 0, 1);
}else{
v = v.subtract(dimensions.getX()/2, 0, dimensions.getZ()/2);
}
v = v.subtract(aT.apply(clipboardOffset(clipboard).toVector3()).toBlockPoint());
if(Config.WaterDepth != 0){
BlockVector3 it = clipboard.getMinimumPoint().add(0, 0, 1); BlockVector3 it = clipboard.getMinimumPoint().add(0, 0, 1);
int depth = 0; int depth = 0;
while(!clipboard.getBlock(it).getBlockType().getMaterial().isAir()){ while(!clipboard.getBlock(it).getBlockType().getMaterial().isAir()){
depth++; depth++;
it = it.add(0, 1, 0); it = it.add(0, 1, 0);
} }
v = v.add(0, Config.WaterDepth - depth, 0); return depth;
}
pasteClipboard(Bukkit.getWorlds().get(0), clipboard, aT, v, false);
} }
@Override @Override
public void pasteChar(Clipboard character, Location pos, int xOffset, AffineTransform aT) { public void pasteClipboard(Clipboard clipboard, Location position, Vector offset, AffineTransform aT) {
BlockVector3 v = aT.apply(BlockVector3.at(xOffset, 0, 0).subtract(clipboardOffset(character)).toVector3()).toBlockPoint(); EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(position.getWorld()), -1);
pasteClipboard(pos.getWorld(), character, aT, v.add(BukkitAdapter.asBlockVector(pos)), true);
}
private BlockVector3 clipboardOffset(Clipboard clipboard) {
return clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
}
private void pasteClipboard(org.bukkit.World world, Clipboard clipboard, AffineTransform aT, BlockVector3 pos, boolean ignoreAir) {
EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(world), -1);
ClipboardHolder ch = new ClipboardHolder(clipboard); ClipboardHolder ch = new ClipboardHolder(clipboard);
ch.setTransform(aT); ch.setTransform(aT);
Operations.completeBlindly(ch.createPaste(e).to(pos).ignoreAirBlocks(ignoreAir).build()); Operations.completeBlindly(ch.createPaste(e).to(BukkitAdapter.asBlockVector(position).add(
aT.apply(BlockVector3.at(offset.getBlockX(), offset.getBlockY(), offset.getBlockZ()).add(clipboard.getOrigin()).subtract(clipboard.getMinimumPoint()).toVector3()).toBlockPoint()
)).build());
e.flushSession(); e.flushSession();
} }

Datei anzeigen

@ -37,7 +37,6 @@ import com.sk89q.worldedit.math.transform.AffineTransform;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.FightSystem;
import de.steamwar.sql.Schematic; import de.steamwar.sql.Schematic;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -88,46 +87,25 @@ public class WorldeditWrapper8 implements WorldeditWrapper.IWorldeditWrapper {
} }
@Override @Override
public void pasteSchematic(Clipboard clipboard, Region region, boolean rotate) { public int getWaterDepth(Clipboard clipboard) {
AffineTransform aT = new AffineTransform().rotateY(rotate ? 180 : 0);
Vector dimensions = clipboard.getDimensions();
Vector v = new Vector(region.centerX(), region.getMinY(), region.centerZ());
if(rotate){
v = v.add(dimensions.getX()/2 + dimensions.getX()%2, 0, dimensions.getZ()/2 + dimensions.getZ()%2).subtract(1, 0, 1);
}else{
v = v.subtract(dimensions.getX()/2 - dimensions.getX()%2, 0, dimensions.getZ()/2 - dimensions.getZ()%2);
}
v = v.subtract(aT.apply(clipboardOffset(clipboard)).toBlockPoint());
if(Config.WaterDepth != 0){
Vector it = clipboard.getMinimumPoint().add(0, 0, 1); Vector it = clipboard.getMinimumPoint().add(0, 0, 1);
int depth = 0; int depth = 0;
while(!clipboard.getBlock(it).isAir()) { while(!clipboard.getBlock(it).isAir()) {
depth++; depth++;
it = it.add(0, 1, 0); it = it.add(0, 1, 0);
} }
v = v.add(0, Config.WaterDepth - depth, 0); return depth;
}
pasteClipboard(Bukkit.getWorlds().get(0), clipboard, aT, v, false);
} }
@Override @Override
public void pasteChar(Clipboard character, Location pos, int xOffset, AffineTransform aT) { public void pasteClipboard(Clipboard clipboard, Location position, org.bukkit.util.Vector offset, AffineTransform aT) {
Vector v = aT.apply(new Vector(xOffset, 0, 0).subtract(clipboardOffset(character))).toBlockPoint(); World w = new BukkitWorld(position.getWorld());
pasteClipboard(pos.getWorld(), character, aT, v.add(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()), true);
}
private Vector clipboardOffset(Clipboard clipboard) {
return clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
}
private void pasteClipboard(org.bukkit.World world, Clipboard clipboard, AffineTransform aT, Vector pos, boolean ignoreAir) {
World w = new BukkitWorld(world);
EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(w, -1); EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(w, -1);
ClipboardHolder ch = new ClipboardHolder(clipboard, w.getWorldData()); ClipboardHolder ch = new ClipboardHolder(clipboard, w.getWorldData());
ch.setTransform(aT); ch.setTransform(aT);
Operations.completeBlindly(ch.createPaste(e, w.getWorldData()).to(pos).ignoreAirBlocks(ignoreAir).build()); Operations.completeBlindly(ch.createPaste(e, w.getWorldData()).to(new Vector(position.getBlockX(), position.getBlockY(), position.getBlockZ()).add(
aT.apply(new Vector(offset.getBlockX(), offset.getBlockY(), offset.getBlockZ()).add(clipboard.getOrigin()).subtract(clipboard.getMinimumPoint())).toBlockPoint()
)).build());
e.flushQueue(); e.flushQueue();
} }

Datei anzeigen

@ -35,6 +35,7 @@ import de.steamwar.sql.Schematic;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.util.Vector;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -122,7 +123,16 @@ public class FightSchematic extends StateDependent {
try { try {
replaceTeamColor(clipboard); replaceTeamColor(clipboard);
WorldeditWrapper.impl.pasteSchematic(clipboard, region, rotate); WorldeditWrapper.impl.pasteClipboard(
clipboard,
new Location(Bukkit.getWorlds().get(0), region.centerX(), region.getMinY(), region.centerZ()),
new Vector(
-clipboard.getDimensions().getBlockX()/2,
Config.WaterDepth != 0 ? Config.WaterDepth - WorldeditWrapper.impl.getWaterDepth(clipboard) : 0,
-clipboard.getDimensions().getBlockZ()/2
),
new AffineTransform().rotateY(rotate ? 180 : 0)
);
} catch (WorldEditException e) { } catch (WorldEditException e) {
FightSystem.broadcast("§cFehler beim Pasten der Schematic"); FightSystem.broadcast("§cFehler beim Pasten der Schematic");
Bukkit.getScheduler().runTask(FightSystem.getPlugin(), FightSystem::setPreSchemState); Bukkit.getScheduler().runTask(FightSystem.getPlugin(), FightSystem::setPreSchemState);
@ -182,9 +192,9 @@ public class FightSchematic extends StateDependent {
length -= 1; length -= 1;
AffineTransform aT = new AffineTransform().rotateY(rotate ? 180 : 0); AffineTransform aT = new AffineTransform().rotateY(rotate ? 180 : 0);
Location base = new Location(Bukkit.getWorlds().get(0), team.getSchemRegion().centerX(), team.getExtendRegion().getMaxY(), team.getSchemRegion().centerZ() + (rotate ? 1 : -1) * team.getSchemRegion().getSizeZ() / 2); Location base = new Location(Bukkit.getWorlds().get(0), region.centerX(), team.getExtendRegion().getMaxY(), region.centerZ());
for(int i = 0; i < characters.length; i++){ for(int i = 0; i < characters.length; i++){
WorldeditWrapper.impl.pasteChar(characters[i], base, offsets[i] - length/2, aT); WorldeditWrapper.impl.pasteClipboard(characters[i], base, new Vector(offsets[i] - length/2, 0, region.getSizeZ()/2), aT);
} }
} }

Datei anzeigen

@ -38,10 +38,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.io.DataOutputStream; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level; import java.util.logging.Level;
public interface Recorder { public interface Recorder {
@ -249,16 +246,23 @@ public interface Recorder {
} }
default void schem(int embedId, int noEmbedId, int schemId){ default void schem(int embedId, int noEmbedId, int schemId){
try { if(schemId == 0) {
if(schemId == 0)
throw new IOException();
write(embedId, schemId, Schematic.getSchemFromDB(schemId).schemData());
} catch (IOException e) {
if(schemId != 0)
Bukkit.getLogger().log(Level.SEVERE, "Could not embed schematic", e);
write(noEmbedId, schemId); write(noEmbedId, schemId);
return;
} }
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try{
copy(Schematic.getSchemFromDB(schemId).schemData(), buffer);
}catch (EOFException e) {
Bukkit.getLogger().log(Level.INFO, "EOFException ignored");
} catch (IOException e) {
Bukkit.getLogger().log(Level.SEVERE, "Could not embed schematic", e);
disable();
return;
}
write(embedId, schemId, buffer.toByteArray());
} }
default void scoreboardTitle(String title){ default void scoreboardTitle(String title){
@ -292,8 +296,8 @@ public interface Recorder {
stream.writeDouble((Double)o); stream.writeDouble((Double)o);
else if(o instanceof String) else if(o instanceof String)
stream.writeUTF((String)o); stream.writeUTF((String)o);
else if(o instanceof InputStream) else if(o instanceof byte[])
copy((InputStream) o, stream); stream.write((byte[])o);
else else
throw new SecurityException("Undefined write for: " + o.getClass().getName()); throw new SecurityException("Undefined write for: " + o.getClass().getName());
} }

Datei anzeigen

@ -26,6 +26,7 @@ import de.steamwar.fightsystem.VersionDependent;
import de.steamwar.sql.Schematic; import de.steamwar.sql.Schematic;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.util.Vector;
import java.io.IOException; import java.io.IOException;
@ -36,8 +37,8 @@ public class WorldeditWrapper {
public interface IWorldeditWrapper { public interface IWorldeditWrapper {
void replaceTeamColor(Clipboard clipboard, DyeColor c) throws WorldEditException; void replaceTeamColor(Clipboard clipboard, DyeColor c) throws WorldEditException;
void pasteSchematic(Clipboard clipboard, Region region, boolean rotate); int getWaterDepth(Clipboard clipboard);
void pasteChar(Clipboard character, Location pos, int xOffset, AffineTransform aT); void pasteClipboard(Clipboard clipboard, Location position, Vector offset, AffineTransform aT);
Clipboard loadChar(String charName) throws IOException; Clipboard loadChar(String charName) throws IOException;
void saveSchem(Schematic schem, Region region, int minY) throws WorldEditException; void saveSchem(Schematic schem, Region region, int minY) throws WorldEditException;
} }