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
Raw Normal View History

2020-04-28 00:15:24 +00:00
package com.thebrokenrail.scriptcraft.api.bridge;
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import net.minecraft.block.BlockState;
2020-08-26 21:52:17 +00:00
import net.minecraft.state.property.Property;
2020-04-28 00:15:24 +00:00
2020-08-26 21:52:17 +00:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@SuppressWarnings({"rawtypes", "unchecked"})
2020-04-28 00:15:24 +00:00
class BlockStateBridges {
static void register() {
2020-08-26 21:52:17 +00:00
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();
});
2020-06-02 17:24:32 +00:00
ScriptCraftCore.addBridge("BlockState.isValid", args -> args[0] instanceof BlockState);
2020-04-28 00:15:24 +00:00
}
}