#1182: Consolidate Preconditions use and minor cleanup

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot
2023-06-12 19:41:02 +10:00
parent 5ff68bfbcb
commit ff78bf30f6
72 changed files with 695 additions and 855 deletions

View File

@@ -1,5 +1,6 @@
package org.bukkit.craftbukkit.structure;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -14,7 +15,6 @@ import net.minecraft.world.level.block.EnumBlockRotation;
import net.minecraft.world.level.levelgen.structure.templatesystem.DefinedStructure;
import net.minecraft.world.level.levelgen.structure.templatesystem.DefinedStructureInfo;
import net.minecraft.world.level.levelgen.structure.templatesystem.DefinedStructureProcessorRotation;
import org.apache.commons.lang3.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.RegionAccessor;
@@ -42,9 +42,10 @@ public class CraftStructure implements Structure {
@Override
public void place(Location location, boolean includeEntities, StructureRotation structureRotation, Mirror mirror, int palette, float integrity, Random random) {
Preconditions.checkArgument(location != null, "Location cannot be null");
location.checkFinite();
World world = location.getWorld();
Validate.notNull(world, "location#getWorld() cannot be null");
Preconditions.checkArgument(world != null, "The World of Location cannot be null");
BlockVector blockVector = new BlockVector(location.getBlockX(), location.getBlockY(), location.getBlockZ());
place(world, blockVector, includeEntities, structureRotation, mirror, palette, integrity, random);
@@ -52,12 +53,11 @@ public class CraftStructure implements Structure {
@Override
public void place(RegionAccessor regionAccessor, BlockVector location, boolean includeEntities, StructureRotation structureRotation, Mirror mirror, int palette, float integrity, Random random) {
Validate.notNull(regionAccessor, "regionAccessor can not be null");
Preconditions.checkArgument(location != null, "Location cannot be null");
Preconditions.checkArgument(regionAccessor != null, "RegionAccessor cannot be null");
location.checkFinite();
if (integrity < 0F || integrity > 1F) {
throw new IllegalArgumentException("Integrity must be between 0 and 1 inclusive. Was \"" + integrity + "\"");
}
Preconditions.checkArgument(integrity >= 0F && integrity <= 1F, "Integrity value (%S) must be between 0 and 1 inclusive", integrity);
RandomSource randomSource = new RandomSourceWrapper(random);
DefinedStructureInfo definedstructureinfo = new DefinedStructureInfo()
@@ -74,10 +74,10 @@ public class CraftStructure implements Structure {
@Override
public void fill(Location corner1, Location corner2, boolean includeEntities) {
Validate.notNull(corner1, "corner1 cannot be null");
Validate.notNull(corner2, "corner2 cannot be null");
Preconditions.checkArgument(corner1 != null, "Location corner1 cannot be null");
Preconditions.checkArgument(corner2 != null, "Location corner2 cannot be null");
World world = corner1.getWorld();
Validate.notNull(world, "corner1#getWorld() cannot be null");
Preconditions.checkArgument(world != null, "World of corner1 Location cannot be null");
Location origin = new Location(world, Math.min(corner1.getBlockX(), corner2.getBlockX()), Math.min(corner1.getBlockY(), corner2.getBlockY()), Math.min(corner1.getBlockZ(), corner2.getBlockZ()));
BlockVector size = new BlockVector(Math.abs(corner1.getBlockX() - corner2.getBlockX()), Math.abs(corner1.getBlockY() - corner2.getBlockY()), Math.abs(corner1.getBlockZ() - corner2.getBlockZ()));
@@ -86,13 +86,11 @@ public class CraftStructure implements Structure {
@Override
public void fill(Location origin, BlockVector size, boolean includeEntities) {
Validate.notNull(origin, "origin cannot be null");
Preconditions.checkArgument(origin != null, "Location origin cannot be null");
World world = origin.getWorld();
Validate.notNull(world, "origin#getWorld() cannot be null");
Validate.notNull(size, "size cannot be null");
if (size.getBlockX() < 1 || size.getBlockY() < 1 || size.getBlockZ() < 1) {
throw new IllegalArgumentException("Size must be at least 1x1x1 but was " + size.getBlockX() + "x" + size.getBlockY() + "x" + size.getBlockZ());
}
Preconditions.checkArgument(world != null, "World of Location origin cannot be null");
Preconditions.checkArgument(size != null, "BlockVector size cannot be null");
Preconditions.checkArgument(size.getBlockX() >= 1 && size.getBlockY() >= 1 && size.getBlockZ() >= 1, "Size must be at least 1x1x1 but was %sx%sx%s", size.getBlockX(), size.getBlockY(), size.getBlockZ());
structure.fillFromWorld(((CraftWorld) world).getHandle(), CraftLocation.toBlockPosition(origin), CraftBlockVector.toBlockPosition(size), includeEntities, Blocks.STRUCTURE_VOID);
}

View File

@@ -1,5 +1,6 @@
package org.bukkit.craftbukkit.structure;
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -17,7 +18,6 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.world.level.levelgen.structure.templatesystem.DefinedStructure;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
import org.apache.commons.lang3.Validate;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.structure.Structure;
@@ -35,16 +35,14 @@ public class CraftStructureManager implements StructureManager {
public Map<NamespacedKey, Structure> getStructures() {
Map<NamespacedKey, Structure> cachedStructures = new HashMap<>();
for (Map.Entry<MinecraftKey, Optional<DefinedStructure>> entry : structureManager.structureRepository.entrySet()) {
entry.getValue().ifPresent(definedStructure -> {
cachedStructures.put(CraftNamespacedKey.fromMinecraft(entry.getKey()), new CraftStructure(definedStructure));
});
entry.getValue().ifPresent(definedStructure -> cachedStructures.put(CraftNamespacedKey.fromMinecraft(entry.getKey()), new CraftStructure(definedStructure)));
}
return Collections.unmodifiableMap(cachedStructures);
}
@Override
public Structure getStructure(NamespacedKey structureKey) {
Validate.notNull(structureKey, "structureKey cannot be null");
Preconditions.checkArgument(structureKey != null, "NamespacedKey structureKey cannot be null");
final Optional<DefinedStructure> definedStructure = structureManager.structureRepository.get(CraftNamespacedKey.toMinecraft(structureKey));
if (definedStructure == null) {
@@ -83,7 +81,8 @@ public class CraftStructureManager implements StructureManager {
@Override
public void saveStructure(NamespacedKey structureKey, Structure structure) throws IOException {
Validate.notNull(structure, "structure cannot be null");
Preconditions.checkArgument(structureKey != null, "NamespacedKey structure cannot be null");
Preconditions.checkArgument(structure != null, "Structure cannot be null");
File structureFile = getStructureFile(structureKey);
Files.createDirectories(structureFile.toPath().getParent());
@@ -92,7 +91,8 @@ public class CraftStructureManager implements StructureManager {
@Override
public Structure registerStructure(NamespacedKey structureKey, Structure structure) {
Validate.notNull(structure, "structure cannot be null");
Preconditions.checkArgument(structureKey != null, "NamespacedKey structureKey cannot be null");
Preconditions.checkArgument(structure != null, "Structure cannot be null");
MinecraftKey minecraftKey = createAndValidateMinecraftStructureKey(structureKey);
final Optional<DefinedStructure> optionalDefinedStructure = Optional.of(((CraftStructure) structure).getHandle());
@@ -102,6 +102,7 @@ public class CraftStructureManager implements StructureManager {
@Override
public Structure unregisterStructure(NamespacedKey structureKey) {
Preconditions.checkArgument(structureKey != null, "NamespacedKey structureKey cannot be null");
MinecraftKey minecraftKey = createAndValidateMinecraftStructureKey(structureKey);
final Optional<DefinedStructure> previousStructure = structureManager.structureRepository.remove(minecraftKey);
@@ -132,7 +133,7 @@ public class CraftStructureManager implements StructureManager {
@Override
public Structure loadStructure(File file) throws IOException {
Validate.notNull(file, "file cannot be null");
Preconditions.checkArgument(file != null, "File cannot be null");
FileInputStream fileinputstream = new FileInputStream(file);
return loadStructure(fileinputstream);
@@ -140,15 +141,15 @@ public class CraftStructureManager implements StructureManager {
@Override
public Structure loadStructure(InputStream inputStream) throws IOException {
Validate.notNull(inputStream, "inputStream cannot be null");
Preconditions.checkArgument(inputStream != null, "inputStream cannot be null");
return new CraftStructure(structureManager.readStructure(inputStream));
}
@Override
public void saveStructure(File file, Structure structure) throws IOException {
Validate.notNull(file, "file cannot be null");
Validate.notNull(structure, "structure cannot be null");
Preconditions.checkArgument(file != null, "file cannot be null");
Preconditions.checkArgument(structure != null, "structure cannot be null");
FileOutputStream fileoutputstream = new FileOutputStream(file);
saveStructure(fileoutputstream, structure);
@@ -156,8 +157,8 @@ public class CraftStructureManager implements StructureManager {
@Override
public void saveStructure(OutputStream outputStream, Structure structure) throws IOException {
Validate.notNull(outputStream, "outputStream cannot be null");
Validate.notNull(structure, "structure cannot be null");
Preconditions.checkArgument(outputStream != null, "outputStream cannot be null");
Preconditions.checkArgument(structure != null, "structure cannot be null");
NBTTagCompound nbttagcompound = ((CraftStructure) structure).getHandle().save(new NBTTagCompound());
NBTCompressedStreamTools.writeCompressed(nbttagcompound, outputStream);
@@ -169,17 +170,16 @@ public class CraftStructureManager implements StructureManager {
}
private MinecraftKey createAndValidateMinecraftStructureKey(NamespacedKey structureKey) {
Validate.notNull(structureKey, "structureKey cannot be null");
Preconditions.checkArgument(structureKey != null, "NamespacedKey structureKey cannot be null");
MinecraftKey minecraftkey = CraftNamespacedKey.toMinecraft(structureKey);
if (minecraftkey.getPath().contains("//")) {
throw new IllegalArgumentException("Resource key for Structures can not contain \"//\"");
}
Preconditions.checkArgument(!minecraftkey.getPath().contains("//"), "Resource key for Structures can not contain \"//\"");
return minecraftkey;
}
@Override
public Structure copy(Structure structure) {
Preconditions.checkArgument(structure != null, "Structure cannot be null");
return new CraftStructure(structureManager.readStructure(((CraftStructure) structure).getHandle().save(new NBTTagCompound())));
}
}