Skip to content

Commit ab49825

Browse files
Revert "Adjust code to auto assign blockstates based on requested type"
This reverts commit 80ffd45.
1 parent 80ffd45 commit ab49825

2 files changed

Lines changed: 25 additions & 55 deletions

File tree

parallelworlds/src/main/java/parallelmc/parallelworlds/ParallelWorldsBootstrapper.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.minecraft.world.level.material.MapColor;
2020
import org.bukkit.plugin.java.JavaPlugin;
2121
import parallelmc.parallelworlds.blocks.QuicksandBlock;
22+
import parallelmc.parallelworlds.blocks.TestBlock;
2223
import parallelmc.parallelworlds.registry.ParallelBlockRegistry;
2324

2425
import java.util.function.Function;
@@ -34,17 +35,14 @@ public void bootstrap(BootstrapContext context) {
3435
if (registry == null) throw new RuntimeException("ParallelBlockRegistry is null!");
3536

3637
register(registry, "polished_sandstone", Block::new,
37-
BlockBehaviour.Properties.of().mapColor(MapColor.SAND).requiresCorrectToolForDrops().strength(0.8F)
38-
.sound(SoundType.STONE),
39-
ParallelBlockRegistry.BlockType.FULL_BLOCK,
38+
BlockBehaviour.Properties.of().mapColor(MapColor.SAND).requiresCorrectToolForDrops().strength(0.8F).sound(SoundType.STONE),
39+
Blocks.NOTE_BLOCK.getStateDefinition().any().setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.BANJO).setValue(NoteBlock.NOTE, 0),
4040
Blocks.SANDSTONE.defaultBlockState(),
4141
Component.literal("Polished Sandstone").setStyle(Style.EMPTY));
4242

4343
register(registry, "quicksand", QuicksandBlock::new,
44-
BlockBehaviour.Properties.of().mapColor(MapColor.SAND).strength(0.25F)
45-
.sound(SoundType.SAND).dynamicShape().noOcclusion()
46-
.isRedstoneConductor((blockState, blockGetter, blockPos) -> false),
47-
ParallelBlockRegistry.BlockType.FULL_BLOCK,
44+
BlockBehaviour.Properties.of().mapColor(MapColor.SAND).strength(0.25F).sound(SoundType.SAND).dynamicShape().noOcclusion().isRedstoneConductor((blockState, blockGetter, blockPos) -> false),
45+
Blocks.NOTE_BLOCK.getStateDefinition().any().setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.BANJO).setValue(NoteBlock.NOTE, 1),
4846
Blocks.SAND.defaultBlockState(),
4947
Component.literal("Quicksand").setStyle(Style.EMPTY));
5048

@@ -58,10 +56,10 @@ public JavaPlugin createPlugin(PluginProviderContext context) {
5856

5957
private static void register(ParallelBlockRegistry registry, String name,
6058
Function<BlockBehaviour.Properties, Block> factory, BlockBehaviour.Properties properties,
61-
ParallelBlockRegistry.BlockType targetType, BlockState particleState, Component itemName) {
59+
BlockState targetBlockstate, BlockState particleState, Component itemName) {
6260
ResourceKey<Block> blockKey = ResourceKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath("parallelutils", name));
6361
Block block = factory.apply(properties.setId(blockKey));
64-
registry.registerBlock(blockKey, block, targetType, particleState, itemName);
62+
registry.registerBlock(blockKey, block, targetBlockstate, particleState, itemName);
6563
}
6664

6765

parallelworlds/src/main/java/parallelmc/parallelworlds/registry/ParallelBlockRegistry.java

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public class ParallelBlockRegistry {
4141
private final WritableRegistry<Block> blockRegistry;
4242
private final HashMap<Integer, Integer> stateMap;
4343

44-
// By type of custom block, a queue of each available ID for the register functions to consume
45-
private final HashMap<BlockType, Queue<Integer>> availableStates;
44+
// NOTE: If the resource pack can be automatically generated and served
45+
// this can be converted into map of BlockType -> Queue<Integer> since we can choose values entirely server side
46+
private final HashMap<BlockState, Integer> availableStates;
4647

4748
private final HashMap<Integer, ParallelBlockData> blockDataMap;
4849
private final HashMap<Integer, BlockState> placeMap;
@@ -72,32 +73,20 @@ private ParallelBlockRegistry() throws RuntimeException, NoSuchMethodException {
7273
placeMap = new HashMap<>();
7374

7475
// TODO: Fill available states
75-
addFullBlock(Blocks.NOTE_BLOCK, BlockType.FULL_BLOCK);
76-
addFullBlock(Blocks.SCULK_SENSOR, BlockType.TRANSPARENT_SLAB);
77-
addFullBlock(Blocks.CALIBRATED_SCULK_SENSOR, BlockType.TRANSPARENT_SLAB);
76+
addFullBlock(Blocks.NOTE_BLOCK);
77+
addFullBlock(Blocks.SCULK_SENSOR);
78+
addFullBlock(Blocks.CALIBRATED_SCULK_SENSOR);
7879

7980
}
8081

81-
private void addFullBlock(Block block, BlockType type) {
82-
83-
Queue<Integer> queue;
84-
85-
if (!availableStates.containsKey(type)) {
86-
// Create new queue if it doesn't exist, and set its initial size for a slight efficiency boost
87-
queue = new ArrayDeque<>(block.getStateDefinition().getPossibleStates().size());
88-
availableStates.put(type, queue);
89-
} else {
90-
queue = availableStates.get(type);
91-
}
82+
private void addFullBlock(Block block) {
9283

9384
for (BlockState state : block.getStateDefinition().getPossibleStates()) {
9485
if (state.equals(block.defaultBlockState())) continue; // This is the one actually used for rendering
9586

96-
int stateId = Block.BLOCK_STATE_REGISTRY.getId(state);
87+
if (availableStates.containsKey(state)) throw new IllegalStateException("Cannot add state that already exists");
9788

98-
if (queue.contains(stateId)) throw new IllegalStateException("Cannot add state that already exists");
99-
100-
queue.add(stateId);
89+
availableStates.put(state, Block.BLOCK_STATE_REGISTRY.getId(state));
10190
}
10291
}
10392

@@ -128,8 +117,7 @@ public void freeze() {
128117
MappedRegistry<@NotNull WritableRegistry<?>> writable_registry = (MappedRegistry<WritableRegistry<?>>) getPrivateField("WRITABLE_REGISTRY", BuiltInRegistries.class, null, WritableRegistry.class);
129118

130119
// Search registry byValue (since byKey doesn't work for whatever reason...)
131-
Map<WritableRegistry<?>, Holder.Reference<@NotNull WritableRegistry<?>>> byValue =
132-
getPrivateField("byValue", MappedRegistry.class, writable_registry, Map.class);
120+
Map<WritableRegistry<?>, Holder.Reference<@NotNull WritableRegistry<?>>> byValue = getPrivateField("byValue", MappedRegistry.class, writable_registry, Map.class);
133121

134122
for (WritableRegistry i : byValue.keySet()) {
135123
if (String.valueOf(i.key().identifier()).equals(String.valueOf(registryKey.identifier()))) {
@@ -140,30 +128,21 @@ public void freeze() {
140128
return null;
141129
}
142130

143-
public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, BlockType targetType,
144-
BlockState particleState, Component name) {
131+
public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, BlockState targetBlockstate, BlockState particleState, Component name) {
145132
ItemStack stack = Items.BARRIER.getDefaultInstance();
146133

147134
stack.applyComponentsAndValidate(
148135
DataComponentPatch.builder()
149136
.set(TypedDataComponent.createUnchecked(DataComponents.ITEM_MODEL, key.identifier()))
150137
.set(TypedDataComponent.createUnchecked(DataComponents.ITEM_NAME, name)).build());
151138

152-
return registerBlock(key, block, targetType, List.of(stack), new BlockParticleOption(ParticleTypes.BLOCK, particleState),stack);
139+
return registerBlock(key, block, targetBlockstate, List.of(stack), new BlockParticleOption(ParticleTypes.BLOCK, particleState),stack);
153140
}
154141

155-
public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, BlockType targetType, List<ItemStack> drops, ParticleOptions particle, ItemStack placeBlock) {
142+
public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, BlockState targetBlockstate, List<ItemStack> drops, ParticleOptions particle, ItemStack placeBlock) {
156143
if (frozen) return false;
157144

158-
Queue<Integer> blockQueue = availableStates.get(targetType);
159-
if (blockQueue == null) {
160-
throw new IllegalStateException("No BlockState of requested type is registered");
161-
}
162-
163-
Integer nextState = blockQueue.poll();
164-
if (nextState == null) {
165-
throw new IllegalStateException("No remaining BlockState of requested type");
166-
}
145+
if (!availableStates.containsKey(targetBlockstate)) throw new IllegalStateException("Block state is already used or does not exist");
167146

168147
try {
169148
Material newMat = ReflectionHelper.makeEnum(Material.class,
@@ -176,15 +155,17 @@ public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, Block
176155
} catch (Throwable t) {
177156
Logger.getGlobal().log(Level.SEVERE, "Unable to create new Material!");
178157
t.printStackTrace();
179-
throw new IllegalStateException();
158+
180159
}
181160

182161
Holder.Reference<Block> registeredBlock = blockRegistry.register(key, block, RegistrationInfo.BUILT_IN);
183162

184163
// TODO: This technically doesn't work since several blockstates are mapped to a single one, but it's easier for now
185164
for (BlockState blockState : registeredBlock.value().getStateDefinition().getPossibleStates()) {
186165

187-
stateMap.put(Block.BLOCK_STATE_REGISTRY.size(), nextState); // Map the new block state to an unused state
166+
Integer stateId = availableStates.remove(targetBlockstate);
167+
168+
stateMap.put(Block.BLOCK_STATE_REGISTRY.size(), stateId); // Map the new block state to an unused state
188169
blockDataMap.put(Block.BLOCK_STATE_REGISTRY.size(), new ParallelBlockData(drops, particle));
189170
placeMap.put(ItemStack.hashItemAndComponents(placeBlock), blockState);
190171

@@ -214,13 +195,4 @@ public ParallelBlockData getBlockData(int state) {
214195
public BlockState getBlockState(@NotNull ItemStack item) {
215196
return placeMap.get(ItemStack.hashItemAndComponents(item));
216197
}
217-
218-
public enum BlockType {
219-
FULL_BLOCK,
220-
TRANSPARENT_BLOCK,
221-
TRANSPARENT_BLOCK_WATERLOGGED,
222-
BIOME_TRANSPARENT_BLOCK,
223-
TRANSPARENT_SLAB,
224-
225-
}
226198
}

0 commit comments

Comments
 (0)