Skip to content

Commit

Permalink
Updated BlockData Api and ChestStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Nov 17, 2021
1 parent d0f9929 commit c66c44f
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 37 deletions.
4 changes: 2 additions & 2 deletions legacy-1_17-compat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourcewriters.spigot.rwg</groupId>
<artifactId>legacy-1_17-compat</artifactId>
<version>2.0.2</version>
<version>3.0.0</version>

<properties>
<api.version>2.1.0</api.version>
<api.version>3.0.0</api.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataLoaderManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataParserManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataPlacerManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.BlockAccessImpl;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftLoader;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftParser;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftPlacer;
Expand All @@ -26,7 +25,6 @@ public class VersionCompatibilityProviderImpl extends VersionCompatibilityProvid
public void provide(final ILogger logger, final IArgumentMap map) {
final IDataFixHandler dataFixHandler = new DataFixHandlerImpl();
final IVersionAccess versionAccess = new VersionAccessImpl(logger);
set(map, IBlockAccess.class, new BlockAccessImpl(logger, versionAccess.getConversionAccess(), dataFixHandler));
set(map, IVersionAccess.class, versionAccess);
set(map, IDataFixHandler.class, dataFixHandler);
}
Expand Down
6 changes: 3 additions & 3 deletions legacy-1_17-compat/src/main/resources/api.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"api": 2,
"version": "${project.version}",
"api": 3,
"version": "${project.version}",
"minecraft": [
"1.17"
"1.17"
]
}
4 changes: 2 additions & 2 deletions legacy-1_17_1-compat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourcewriters.spigot.rwg</groupId>
<artifactId>legacy-1_17_1-compat</artifactId>
<version>2.0.2</version>
<version>3.0.0</version>

<properties>
<api.version>2.1.0</api.version>
<api.version>3.0.0</api.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataLoaderManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataParserManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataPlacerManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.BlockAccessImpl;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftLoader;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftParser;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftPlacer;
Expand All @@ -26,7 +25,6 @@ public class VersionCompatibilityProviderImpl extends VersionCompatibilityProvid
public void provide(final ILogger logger, final IArgumentMap map) {
final IDataFixHandler dataFixHandler = new DataFixHandlerImpl();
final IVersionAccess versionAccess = new VersionAccessImpl(logger);
set(map, IBlockAccess.class, new BlockAccessImpl(logger, versionAccess.getConversionAccess(), dataFixHandler));
set(map, IVersionAccess.class, versionAccess);
set(map, IDataFixHandler.class, dataFixHandler);
}
Expand Down
6 changes: 3 additions & 3 deletions legacy-1_17_1-compat/src/main/resources/api.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"api": 2,
"version": "${project.version}",
"api": 3,
"version": "${project.version}",
"minecraft": [
"1.17.1"
"1.17.1"
]
}
2 changes: 1 addition & 1 deletion legacy-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourcewriters.spigot.rwg</groupId>
<artifactId>legacy-api</artifactId>
<version>2.5.1</version>
<version>3.0.0</version>

<distributionManagement>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.plugin.Plugin;

Expand Down Expand Up @@ -36,6 +36,6 @@ public final String getName() {
return name;
}

public abstract IBlockData load(@NonNull IBlockAccess access, @NonNull Block block, @NonNull BlockData blockData);
public abstract IBlockData load(@NonNull IBlockAccess access, @NonNull BlockState blockState, @NonNull BlockData blockData);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;

import com.syntaxphoenix.syntaxapi.nbt.NbtCompound;
Expand All @@ -23,6 +24,9 @@ public interface IBlockAccess {

@CallerSensitive
IBlockData dataOf(@NonNull Block block);

@CallerSensitive
IBlockData dataOf(@NonNull BlockState blockState);

@CallerSensitive
IBlockData dataOf(@NonNull String rawData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;

import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull;

Expand All @@ -29,5 +30,7 @@ default boolean register(final boolean expression, @NonNull final Supplier<Block
IBlockData load(@NonNull Location location);

IBlockData load(@NonNull Block block);

IBlockData load(@NonNull BlockState state);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.plugin.Plugin;

Expand All @@ -22,14 +22,14 @@ public final boolean isWhitelisted(final Material material) {
return materials.contains(material) == whitelist;
}

protected abstract IBlockData internalLoad(@NonNull IBlockAccess access, @NonNull Block block, @NonNull BlockData blockData);
protected abstract IBlockData internalLoad(@NonNull IBlockAccess access, @NonNull BlockState blockState, @NonNull BlockData blockData);

@Override
public final IBlockData load(@NonNull final IBlockAccess access, @NonNull final Block block, @NonNull final BlockData blockData) {
public final IBlockData load(@NonNull final IBlockAccess access, @NonNull final BlockState blockState, @NonNull final BlockData blockData) {
if (!isWhitelisted(blockData.getMaterial())) {
return null;
}
return internalLoad(access, block, blockData);
return internalLoad(access, blockState, blockData);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;

import com.syntaxphoenix.syntaxapi.json.utils.Tracker;
Expand Down Expand Up @@ -77,6 +78,17 @@ public IBlockData dataOf(@NonNull final Block block) {
return loaderManager.load(block);
}

@Override
public IBlockData dataOf(BlockState blockState) {
Objects.requireNonNull(blockState, "BlockState can't be null!");
final Optional<Class<?>> clazz = Tracker.getCallerClass();
if (clazz.isPresent() && BlockDataLoader.class.isAssignableFrom(clazz.get())) {
logger.log(LogTypeId.WARNING,
"BlockDataLoader (" + clazz.get().getName() + ") is calling dataOf(Block), this could cause an infinite loop!");
}
return loaderManager.load(blockState);
}

@CallerSensitive
@Override
public IBlockData dataOf(@NonNull final String rawData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;

import com.syntaxphoenix.syntaxapi.logging.ILogger;
Expand Down Expand Up @@ -68,15 +69,15 @@ public IBlockData load(@NonNull final Location location) {
Objects.requireNonNull(location, "Location can't be null!");
if (Bukkit.isPrimaryThread()) {
try {
return load(location.getBlock());
return load(location.getBlock().getState());
} catch (final Exception exp) {
logger.log(LogTypeId.WARNING, "Failed to load block");
logger.log(LogTypeId.WARNING, exp);
return null;
}
}
try {
return load(location.getBlock());
return load(location.getBlock().getState());
} catch (final Exception exp) {
logger.log(LogTypeId.WARNING, "Failed to load block asynchronously to main thread");
logger.log(LogTypeId.WARNING, exp);
Expand All @@ -87,14 +88,35 @@ public IBlockData load(@NonNull final Location location) {
@Override
public IBlockData load(@NonNull final Block block) {
Objects.requireNonNull(block, "Block can't be null!");
if (Bukkit.isPrimaryThread()) {
try {
return load(block.getState());
} catch (final Exception exp) {
logger.log(LogTypeId.WARNING, "Failed to load block");
logger.log(LogTypeId.WARNING, exp);
return null;
}
}
try {
return load(block.getState());
} catch (final Exception exp) {
logger.log(LogTypeId.WARNING, "Failed to load block asynchronously to main thread");
logger.log(LogTypeId.WARNING, exp);
return null;
}
}

@Override
public IBlockData load(@NonNull final BlockState blockState) {
Objects.requireNonNull(blockState, "BlockState can't be null!");
if (loaders.isEmpty()) {
logger.log(LogTypeId.ERROR, "Can't load blocks without BlockDataLoader's!");
return null;
}
final BlockData data = block.getBlockData();
final BlockData data = blockState.getBlockData();
final int size = order.size();
for (int index = size - 1; index >= 0; index--) {
final IBlockData output = loaders.get(order.get(index)).load(access, block, data);
final IBlockData output = loaders.get(order.get(index)).load(access, blockState, data);
if (output != null) {
return output;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.sourcewriters.spigot.rwg.legacy.api.block.impl;

import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.plugin.Plugin;

Expand All @@ -15,7 +15,7 @@ public DefaultMinecraftLoader(final Plugin plugin) {
}

@Override
public IBlockData load(final IBlockAccess access, final Block block, final BlockData blockData) {
public IBlockData load(final IBlockAccess access, final BlockState block, final BlockData blockData) {
return access.dataOf(blockData).setConversionPossible(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

@Unsafe(status = UnsafeStatus.WORK_IN_PROGRESS, useable = true)
public interface IChestStorage {

String[] getNames();

boolean has(String chest);

void fillInventory(String chest, Inventory inventory);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

class Properties implements IProperties {

private static final ConcurrentHashMap<String, VoidProperty> VOIDS = new ConcurrentHashMap<>();
private static final Function<String, VoidProperty> VOID_PROPERTY_BUILD = VoidProperty::new;

private final List<IProperty<?>> properties = Collections.synchronizedList(new ArrayList<>());

Expand Down Expand Up @@ -99,7 +105,8 @@ public List<IProperty<?>> find(final String... keys) {

@Override
public IProperty<?> find(final String key) {
return properties.stream().filter(property -> property.getKey().equals(key)).findFirst().orElse(new VoidProperty(key));
Optional<IProperty<?>> option = properties.stream().filter(property -> property.getKey().equals(key)).findFirst();
return option.isPresent() ? option.get() : VOIDS.computeIfAbsent(key, VOID_PROPERTY_BUILD);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.function.Supplier;

import net.sourcewriters.spigot.rwg.legacy.api.data.asset.IAsset;
import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull;

public interface ISchematicLoader {
Expand All @@ -22,7 +23,7 @@ public interface ISchematicLoader {
* format
*/
@NonNull
ISchematic load(@NonNull File file) throws Exception;
IAsset<ISchematic> load(@NonNull File file) throws Exception;

boolean register(@NonNull SchematicConverter converter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.syntaxphoenix.syntaxapi.random.RandomNumberGenerator;

import net.sourcewriters.spigot.rwg.legacy.api.data.asset.IAsset;
import net.sourcewriters.spigot.rwg.legacy.api.schematic.update.ISchematicUpdater;
import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull;
import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.unsafe.Unsafe;
Expand All @@ -14,7 +15,7 @@
@Unsafe(status = UnsafeStatus.SUBJECT_TO_CHANGE, useable = true)
public interface ISchematicStorage {

ISchematic get(String name);
IAsset<ISchematic> get(String name);

boolean has(String name);

Expand All @@ -24,7 +25,7 @@ public interface ISchematicStorage {
String[] getNames();

@NonNull
ISchematic[] getSchematics();
IAsset<ISchematic>[] getSchematics();

@NonNull
Future<?> paste(ISchematic schmeatic, Location location);
Expand Down
4 changes: 2 additions & 2 deletions legacy-pre1_17-compat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourcewriters.spigot.rwg</groupId>
<artifactId>legacy-pre1_17-compat</artifactId>
<version>2.0.1</version>
<version>3.0.0</version>

<properties>
<api.version>2.1.0</api.version>
<api.version>3.0.0</api.version>
</properties>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataLoaderManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataParserManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockDataPlacerManager;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.BlockAccessImpl;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftLoader;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftParser;
import net.sourcewriters.spigot.rwg.legacy.api.block.impl.DefaultMinecraftPlacer;
Expand All @@ -26,7 +25,6 @@ public class VersionCompatibilityProviderImpl extends VersionCompatibilityProvid
public void provide(final ILogger logger, final IArgumentMap map) {
final IDataFixHandler dataFixHandler = new DataFixHandlerImpl();
final IVersionAccess versionAccess = new VersionAccessImpl(logger);
set(map, IBlockAccess.class, new BlockAccessImpl(logger, versionAccess.getConversionAccess(), dataFixHandler));
set(map, IVersionAccess.class, versionAccess);
set(map, IDataFixHandler.class, dataFixHandler);
}
Expand Down
4 changes: 2 additions & 2 deletions legacy-pre1_17-compat/src/main/resources/api.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"api": 2,
"version": "${project.version}",
"api": 3,
"version": "${project.version}",
"minecraft": [
"1.13",
"1.13.1",
Expand Down

0 comments on commit c66c44f

Please sign in to comment.