Skip to content
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.webmc</groupId>
<artifactId>wlib</artifactId>
<version>1.2.9-SNAPSHOT</version>
<version>1.3.1-SNAPSHOT</version>
<distributionManagement>
<repository>
<id>github</id>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xyz/webmc/wlib/api/WLIB.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.bukkit.plugin.Plugin;
import org.semver4j.Semver;

@SuppressWarnings({ "NonConstantLogger" })
@SuppressWarnings({ "NonConstantLogger" , "null"})
public final class WLIB {
private static final StackWalker stackWalker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE);
private static final List<Plugin> plugins = new ArrayList<>();
Expand Down
108 changes: 40 additions & 68 deletions src/main/java/xyz/webmc/wlib/api/structure/AbstractBaseStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,114 +13,86 @@

package xyz.webmc.wlib.api.structure;

import xyz.webmc.wlib.api.util.SchemUtil;
import xyz.webmc.wlib.api.WLIB;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cryptomorin.xseries.XMaterial;
import dev.colbster937.reflect.MirrorSafe;
import net.sandrohc.schematic4j.exception.ParsingException;
import net.sandrohc.schematic4j.schematic.Schematic;
import net.sandrohc.schematic4j.schematic.types.SchematicBlock;
import net.sandrohc.schematic4j.schematic.types.SchematicBlockPos;
import org.bukkit.Location;


@SuppressWarnings({ "unchecked" })
public abstract class AbstractBaseStructure {
private static final Map<Class<? extends AbstractBaseStructure>, AbstractBaseStructure> INSTANCES = new HashMap<>();

private final List<BlockRelative> blocks = new ArrayList<>();
private static final List<Class<AbstractBaseStructure>> STRUCTURES = new ArrayList<>();
private final String name;

protected AbstractBaseStructure(final String name) {
this.name = name;
}

public final void place(final Location loc) {
final Location offset = loc.clone().add(this.getOffsetX(), this.getOffsetY(), this.getOffsetZ());
for (final BlockRelative blk : blocks) {
blk.place(offset);
public final String getName() {
return this.name;
}

public abstract PlaceableStructure build();

public static final List<Class<AbstractBaseStructure>> getStructures() {
return STRUCTURES;
}

public static final void registerStructure(final Class<AbstractBaseStructure> clazz) {
if (clazz != null && !STRUCTURES.contains(clazz)) {
STRUCTURES.add(clazz);
}
}

public final String getName() {
return this.name;
@Deprecated(forRemoval = true)
public void place(final Location loc) {
WLIB.warnDeprecatedUsage();
this.build().place(loc);
}
Comment thread
noone-075 marked this conversation as resolved.

@Deprecated(forRemoval = true)
public int getOffsetX() {
return 0;
WLIB.warnDeprecatedUsage();
throw new UnsupportedOperationException();
}

@Deprecated(forRemoval = true)
public int getOffsetY() {
return 0;
WLIB.warnDeprecatedUsage();
throw new UnsupportedOperationException();
}

@Deprecated(forRemoval = true)
public int getOffsetZ() {
return 0;
WLIB.warnDeprecatedUsage();
throw new UnsupportedOperationException();
}

@Deprecated(forRemoval = true)
protected final void addBlock(final BlockRelative blk) {
this.blocks.add(blk);
WLIB.warnDeprecatedUsage();
throw new UnsupportedOperationException();
}

@Deprecated(forRemoval = true)
protected final void loadSchematic(final InputStream is) throws IOException, ParsingException {
try (is) {
final Schematic schematic = SchemUtil.readSchematic(is);

final int width = schematic.width();
final int height = schematic.height();
final int length = schematic.length();

final SchematicBlockPos offset = schematic.offset();

for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) {
for (int x = 0; x < width; x++) {
final SchematicBlock block = schematic.block(x, y, z);
if (block != SchematicBlock.AIR) {
final XMaterial mat = XMaterial.matchXMaterial(block.block().replace("minecraft:", "")).orElse(XMaterial.AIR);
if (mat != null) {
final StringBuilder sb = new StringBuilder();

for (final Map.Entry<String, String> entry : block.states().entrySet()) {
if (sb.length() > 0) {
sb.append(",");
}

sb.append(entry.getKey());
sb.append("=");
sb.append(entry.getValue());
}

this.addBlock(new BlockRelative(x - offset.x, y - offset.y, z - offset.z, mat, "[" + sb.toString() + "]"));
}
}
}
}
}
}
WLIB.warnDeprecatedUsage();
throw new UnsupportedOperationException();
}

@Deprecated(forRemoval = true)
protected final void loadSchematic(final File file) throws IOException, ParsingException {
loadSchematic(new FileInputStream(file));
WLIB.warnDeprecatedUsage();
throw new UnsupportedOperationException();
}

@Deprecated(forRemoval = true)
public static final <T extends AbstractBaseStructure> T getInstance(final Class<T> clazz, final Object... params) {
AbstractBaseStructure structure = INSTANCES.get(clazz);

if (structure == null) {
structure = MirrorSafe.invokeConstructor(clazz, params);
INSTANCES.put(clazz, structure);
}

return (T) structure;
WLIB.warnDeprecatedUsage();
throw new UnsupportedOperationException();
}
Comment thread
noone-075 marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2026 Colbster937
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* See the LICENSE file for details.
*/

package xyz.webmc.wlib.api.structure;

import xyz.webmc.wlib.api.WLIB;

import org.bukkit.Location;
import org.bukkit.World;

public abstract class AbstractGenerableStructure extends AbstractBaseStructure {
static final int SEARCH_RADIUS = 100;

protected AbstractGenerableStructure(final String name) {
super(name);
}

public abstract boolean canGenerateAt(final int chunkX, final int chunkZ, final long worldSeed);

public abstract long getGenerationSeed(final int chunkX, final int chunkZ, final long worldSeed);

public abstract PlaceableStructure build(final long seed);

@Override
public final PlaceableStructure build() {
return this.build(0);
}

public final Location locateNearest(final Location pos, final long worldSeed) {
Location ret = null;

if (pos != null && pos.getWorld() != null) {
final int centerChunkX = pos.getChunk().getX();
final int centerChunkZ = pos.getChunk().getZ();
final World world = pos.getWorld();

search:
for (int radius = 0; radius <= SEARCH_RADIUS; radius++) {
final int minX = centerChunkX - radius;
final int maxX = centerChunkX + radius;
final int minZ = centerChunkZ - radius;
final int maxZ = centerChunkZ + radius;

for (int chunkX = minX; chunkX <= maxX; chunkX++) {
for (int chunkZ : new int[] { minZ, maxZ }) {
if (this.canGenerateAt(chunkX, chunkZ, worldSeed)) {
ret = this.createLocation(world, chunkX, chunkZ);
break search;
}
}
}

for (int chunkZ = minZ + 1; chunkZ < maxZ; chunkZ++) {
for (int chunkX : new int[] { minX, maxX }) {
if (this.canGenerateAt(chunkX, chunkZ, worldSeed)) {
ret = this.createLocation(world, chunkX, chunkZ);
break search;
}
}
}
}
}

return ret;
}

@Deprecated(forRemoval = true)
public final void place(final Location loc) {
WLIB.warnDeprecatedUsage();
this.build(getGenerationSeed(loc.getChunk().getX(), loc.getChunk().getZ(), loc.getWorld().getSeed())).place(loc);
}
Comment thread
noone-075 marked this conversation as resolved.

private Location createLocation(final World world, final int chunkX, final int chunkZ) {
return new Location(world, chunkX * 16 + 8, 65, chunkZ * 16 + 8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void place(final Location loc) {
final Material _mat = this.mat.parseMaterial();

if (mat != null) {
if (WLIB.getIsModernServer() && this.dataModern != null) {
if (WLIB.getIsModernServer() && this.dataModern != null && _mat != null) {
final Object data = MirrorSafe.invokeMethod(Bukkit.class, "createBlockData", new Object[] { "minecraft:" + _mat.name().toLowerCase() + this.dataModern });
MirrorSafe.invokeMethod(Block.class, blk, "setBlockData", data, false);
} else {
Expand Down Expand Up @@ -110,4 +110,8 @@ public final String getDataModern() {
public final byte getDataLegacy() {
return dataLegacy;
}

public final BlockRelative offset(final int x, final int y, final int z) {
return new BlockRelative(this.x + x, this.y + y, this.z + z, this.mat, this.dataModern, this.dataLegacy);
}
}
Loading