This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/BlockStateBridges.java

55 lines
2.1 KiB
Java

package com.thebrokenrail.scriptcraft.api.bridge;
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import net.minecraft.block.BlockState;
import net.minecraft.state.property.Property;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@SuppressWarnings({"rawtypes", "unchecked"})
class BlockStateBridges {
static void register() {
ScriptCraftCore.addBridge("BlockState.getBlock", args -> ((BlockState) args[0]).getBlock());
ScriptCraftCore.addBridge("BlockState.get", args -> {
BlockState state = (BlockState) args[0];
String name = (String) args[1];
Collection<Property<?>> properties = state.getProperties();
for (Property property : properties) {
if (property.getName().equals(name)) {
return property.name(state.get(property));
}
}
return null;
});
ScriptCraftCore.addBridge("BlockState.with", args -> {
BlockState state = (BlockState) args[0];
String name = (String) args[1];
Comparable value = (Comparable) args[2];
Collection<Property<?>> properties = state.getProperties();
for (Property property : properties) {
if (property.getName().equals(name)) {
if (value instanceof String) {
value = (Comparable) property.parse((String) value).orElse(null);
}
return state.with(property, value);
}
}
return null;
});
ScriptCraftCore.addBridge("BlockState.properties", args -> {
BlockState state = (BlockState) args[0];
List<String> names = new ArrayList<>();
Collection<Property<?>> properties = state.getProperties();
for (Property<?> property : properties) {
names.add(property.getName());
}
return names.toArray();
});
ScriptCraftCore.addBridge("BlockState.isValid", args -> args[0] instanceof BlockState);
}
}