Skip to content

Commit

Permalink
use built in registries for block states
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiao-MoMi committed Jan 2, 2025
1 parent 7160792 commit b0c1b1b
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -92,6 +93,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -219,6 +221,15 @@ private synchronized boolean init() {
return true;
}

@Override
public Collection<String> getRegisteredDefaultBlockStates() {
ArrayList<String> states = new ArrayList<>();
for (final Block block : BuiltInRegistries.BLOCK) {
states.add(CraftBlockData.createData(block.defaultBlockState()).getAsString());
}
return states;
}

@Override
public BlockMaterial getMaterial(BlockType blockType) {
Block block = getBlock(blockType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -91,6 +92,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -218,6 +220,15 @@ private synchronized boolean init() {
return true;
}

@Override
public Collection<String> getRegisteredDefaultBlockStates() {
ArrayList<String> states = new ArrayList<>();
for (final Block block : BuiltInRegistries.BLOCK) {
states.add(CraftBlockData.createData(block.defaultBlockState()).getAsString());
}
return states;
}

@Override
public BlockMaterial getMaterial(BlockType blockType) {
Block block = getBlock(blockType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
Expand Down Expand Up @@ -98,6 +99,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -228,6 +230,15 @@ private synchronized boolean init() {
return true;
}

@Override
public Collection<String> getRegisteredDefaultBlockStates() {
ArrayList<String> states = new ArrayList<>();
for (final Block block : BuiltInRegistries.BLOCK) {
states.add(CraftBlockData.createData(block.defaultBlockState()).getAsString());
}
return states;
}

@Override
public BlockMaterial getMaterial(BlockType blockType) {
Block block = getBlock(blockType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
Expand Down Expand Up @@ -98,6 +99,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -228,6 +230,15 @@ private synchronized boolean init() {
return true;
}

@Override
public Collection<String> getRegisteredDefaultBlockStates() {
ArrayList<String> states = new ArrayList<>();
for (final Block block : BuiltInRegistries.BLOCK) {
states.add(CraftBlockData.createData(block.defaultBlockState()).getAsString());
}
return states;
}

@Override
public BlockMaterial getMaterial(BlockType blockType) {
Block block = getBlock(blockType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
Expand Down Expand Up @@ -97,6 +98,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -214,6 +216,15 @@ private synchronized boolean init() {
return true;
}

@Override
public Collection<String> getRegisteredDefaultBlockStates() {
ArrayList<String> states = new ArrayList<>();
for (final Block block : BuiltInRegistries.BLOCK) {
states.add(CraftBlockData.createData(block.defaultBlockState()).getAsString());
}
return states;
}

@Override
public BlockMaterial getMaterial(BlockType blockType) {
Block block = getBlock(blockType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
Expand Down Expand Up @@ -97,6 +98,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -214,6 +216,15 @@ private synchronized boolean init() {
return true;
}

@Override
public Collection<String> getRegisteredDefaultBlockStates() {
ArrayList<String> states = new ArrayList<>();
for (final Block block : BuiltInRegistries.BLOCK) {
states.add(CraftBlockData.createData(block.defaultBlockState()).getAsString());
}
return states;
}

@Override
public BlockMaterial getMaterial(BlockType blockType) {
Block block = getBlock(blockType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.World;
import org.bukkit.block.BlockState;

import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.enginehub.linbus.tree.LinTag;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Map;
import java.util.OptionalInt;

Expand Down Expand Up @@ -135,6 +136,11 @@ default void sendBiomeUpdates(World world, Iterable<BlockVector2> chunks) {
getParent().sendBiomeUpdates(world, chunks);
}

@Override
default Collection<String> getRegisteredDefaultBlockStates() {
return getParent().getRegisteredDefaultBlockStates();
}

@Override
default BlockMaterial getMaterial(BlockType blockType) {
return getParent().getMaterial(blockType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,9 @@ public boolean isTranslucent() {

//FAWE start
@Override
public Collection<String> values() {
ArrayList<String> blocks = new ArrayList<>();
for (Material m : Material.values()) {
if (!m.isLegacy() && m.isBlock()) {
BlockData blockData = m.createBlockData();
blocks.add(blockData.getAsString());
}
}
return blocks;
public Collection<String> getAllDefaultBlockStates() {
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
return adapter.getRegisteredDefaultBlockStates();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.sk89q.worldedit.world.registry.BlockMaterial;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.WorldCreator;
Expand All @@ -66,7 +67,9 @@
import org.enginehub.linbus.tree.LinTag;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -332,6 +335,22 @@ default BlockMaterial getMaterial(BlockState blockState) {
return null;
}

/**
* Returns an iterable of all blocks in their default state as string representations known to the server.
*
* @return an iterable containing the default state strings of all valid blocks
*/
default Collection<String> getRegisteredDefaultBlockStates() {
ArrayList<String> blocks = new ArrayList<>();
for (Material m : Material.values()) {
if (!m.isLegacy() && m.isBlock()) {
BlockData blockData = m.createBlockData();
blocks.add(blockData.getAsString());
}
}
return blocks;
}

@Deprecated
default Tag toNative(T foreign) {
return LinBusConverter.toJnbtTag(toNativeLin(foreign));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static int[] generateStateOrdinals(int internalId, int ordinal, int maxS
Platform platform = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS);
Registries registries = platform.getRegistries();
BlockRegistry blockReg = registries.getBlockRegistry();
Collection<String> blocks = blockReg.values();
Collection<String> blocks = blockReg.getAllDefaultBlockStates();
Map<String, String> blockMap = blocks.stream().collect(Collectors.toMap(item -> item.charAt(item.length() - 1) == ']'
? item.substring(0, item.indexOf('['))
: item, item -> item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ default BlockMaterial getMaterial(BlockState state) {
/**
* Register all blocks.
*/
default Collection<String> values() {
default Collection<String> getAllDefaultBlockStates() {
return Collections.emptyList();
}

Expand Down

0 comments on commit b0c1b1b

Please sign in to comment.