Skip to content

Commit

Permalink
feat(docs): Update blocks section
Browse files Browse the repository at this point in the history
  • Loading branch information
ChampionAsh5357 committed Oct 15, 2024
1 parent 1b57d99 commit f0f7044
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
19 changes: 11 additions & 8 deletions docs/blocks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ So now, let's register our blocks:

```java
//BLOCKS is a DeferredRegister.Blocks
public static final DeferredBlock<Block> MY_BLOCK = BLOCKS.register("my_block", () -> new Block(...));
public static final DeferredBlock<Block> MY_BLOCK = BLOCKS.register("my_block", registryName -> new Block(...));
```

After registering the block, all references to the new `my_block` should use this constant. For example, if you want to check if the block at a given position is `my_block`, the code for that would look something like this:
Expand Down Expand Up @@ -51,6 +51,8 @@ public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBloc

For simple blocks which need no special functionality (think cobblestone, wooden planks, etc.), the `Block` class can be used directly. To do so, during registration, instantiate `Block` with a `BlockBehaviour.Properties` parameter. This `BlockBehaviour.Properties` parameter can be created using `BlockBehaviour.Properties#of`, and it can be customized by calling its methods. The most important methods for this are:

- `setId` - Sets the resource key of the block.
- This **must** be set on every block; otherwise, an exception will be thrown.
- `destroyTime` - Determines the time the block needs to be destroyed.
- Stone has a destroy time of 1.5, dirt has 0.5, obsidian has 50, and bedrock has -1 (unbreakable).
- `explosionResistance` - Determines the explosion resistance of the block.
Expand All @@ -68,8 +70,9 @@ So for example, a simple implementation would look something like this:
//BLOCKS is a DeferredRegister.Blocks
public static final DeferredBlock<Block> MY_BETTER_BLOCK = BLOCKS.register(
"my_better_block",
() -> new Block(BlockBehaviour.Properties.of()
registryName -> new Block(BlockBehaviour.Properties.of()
//highlight-start
.setId(ResourceKey.create(Registries.BLOCK, registryName))
.destroyTime(2.0f)
.explosionResistance(10.0f)
.sound(SoundType.GRAVEL)
Expand Down Expand Up @@ -108,14 +111,14 @@ public class SimpleBlock extends Block {

@Override
public MapCodec<SimpleBlock> codec() {
return SIMPLE_CODEC.value();
return SIMPLE_CODEC.get();
}
}

// In some registration class
public static final DeferredRegister<MapCodec<? extends Block>> REGISTRAR = DeferredRegister.create(BuiltInRegistries.BLOCK_TYPE, "yourmodid");

public static final DeferredHolder<MapCodec<? extends Block>, MapCodec<SimpleBlock>> SIMPLE_CODEC = REGISTRAR.register(
public static final Suppler<MapCodec<SimpleBlock>> SIMPLE_CODEC = REGISTRAR.register(
"simple",
() -> simpleCodec(SimpleBlock::new)
);
Expand All @@ -133,7 +136,7 @@ public class ComplexBlock extends Block {

@Override
public MapCodec<ComplexBlock> codec() {
return COMPLEX_CODEC.value();
return COMPLEX_CODEC.get();
}

public int getValue() {
Expand All @@ -144,14 +147,14 @@ public class ComplexBlock extends Block {
// In some registration class
public static final DeferredRegister<MapCodec<? extends Block>> REGISTRAR = DeferredRegister.create(BuiltInRegistries.BLOCK_TYPE, "yourmodid");

public static final DeferredHolder<MapCodec<? extends Block>, MapCodec<ComplexBlock>> COMPLEX_CODEC = REGISTRAR.register(
public static final Supplier<MapCodec<ComplexBlock>> COMPLEX_CODEC = REGISTRAR.register(
"simple",
() -> RecordCodecBuilder.mapCodec(instance ->
instance.group(
Codec.INT.fieldOf("value").forGetter(ComplexBlock::getValue),
BlockBehaviour.propertiesCodec() // represents the BlockBehavior.Properties parameter
).apply(instance, ComplexBlock::new)
);
)
);
```

Expand All @@ -173,7 +176,7 @@ public static final DeferredBlock<Block> EXAMPLE_BLOCK = BLOCKS.registerBlock(
);
```

Internally, this will simply call `BLOCKS.register("example_block", () -> new Block(BlockBehaviour.Properties.of()))` by applying the properties parameter to the provided block factory (which is commonly the constructor).
Internally, this will simply call `BLOCKS.register("example_block", registryName -> new Block(BlockBehaviour.Properties.of().setId(ResourceKey.create(Registries.BLOCK, registryName))))` by applying the properties parameter to the provided block factory (which is commonly the constructor). The id is set on the properties.

If you want to use `Block::new`, you can leave out the factory entirely:

Expand Down
11 changes: 4 additions & 7 deletions docs/blocks/states.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ To implement a blockstate property, in your block class, create or reference a `
- Implements `Property<E>`. Defines a property that can take on the values of an Enum class.
- Created by calling `EnumProperty#create(String propertyName, Class<E> enumClass)`.
- It is also possible to use only a subset of the Enum values (e.g. 4 out of 16 `DyeColor`s), see the overloads of `EnumProperty#create`.
- `DirectionProperty`
- Extends `EnumProperty<Direction>`. Defines a property that can take on a `Direction`.
- Created by calling `DirectionProperty#create(String propertyName)`.
- Several convenience predicates are provided. For example, to get a property that represents the cardinal directions, call `DirectionProperty.create("<name>", Direction.Plane.HORIZONTAL)`; to get the X directions, `DirectionProperty.create("<name>", Direction.Axis.X)`.

The class `BlockStateProperties` contains shared vanilla properties which should be used or referenced whenever possible, in place of creating your own properties.

Expand All @@ -72,7 +68,7 @@ To further illustrate this, this is what the relevant bits of the `EndPortalFram
public class EndPortalFrameBlock extends Block {
// Note: It is possible to directly use the values in BlockStateProperties instead of referencing them here again.
// However, for the sake of simplicity and readability, it is recommended to add constants like this.
public static final DirectionProperty FACING = BlockStateProperties.FACING;
public static final EnumProperty<Direction> FACING = BlockStateProperties.FACING;
public static final BooleanProperty EYE = BlockStateProperties.EYE;

public EndPortalFrameBlock(BlockBehaviour.Properties pProperties) {
Expand Down Expand Up @@ -107,7 +103,7 @@ To go from `Block` to `BlockState`, call `Block#defaultBlockState()`. The defaul
You can get the value of a property by calling `BlockState#getValue(Property<?>)`, passing it the property you want to get the value of. Reusing our end portal frame example, this would look something like this:

```java
// EndPortalFrameBlock.FACING is a DirectionProperty and thus can be used to obtain a Direction from the BlockState
// EndPortalFrameBlock.FACING is an EnumPropery<Direction> and thus can be used to obtain a Direction from the BlockState
Direction direction = endPortalFrameBlockState.getValue(EndPortalFrameBlock.FACING);
```

Expand Down Expand Up @@ -138,9 +134,10 @@ To help setting the update flags correctly, there are a number of `int` constant
- `Block.UPDATE_KNOWN_SHAPE` stops neighbor update recursion.
- `Block.UPDATE_SUPPRESS_DROPS` disables block drops for the old block at that position.
- `Block.UPDATE_MOVE_BY_PISTON` is only used by piston code to signal that the block was moved by a piston. This is mainly responsible for delaying light engine updates.
- `Block.UPDATE_SKIP_SHAPE_UPDATE_ON_WIRE` is used by the `ExperimentalRedstoneWireEvaluator` to signal if the shape should be skipped. This is set only when the power strength is changed from not a placement or that the original source on the signal is not the current wire.
- `Block.UPDATE_ALL` is an alias for `Block.UPDATE_NEIGHBORS | Block.UPDATE_CLIENTS`.
- `Block.UPDATE_ALL_IMMEDIATE` is an alias for `Block.UPDATE_NEIGHBORS | Block.UPDATE_CLIENTS | Block.UPDATE_IMMEDIATE`.
- `Block.NONE` is an alias for `Block.UPDATE_INVISIBLE`.
- `Block.UPDATE_NONE` is an alias for `Block.UPDATE_INVISIBLE`.

There is also a convenience method `Level#setBlockAndUpdate(BlockPos pos, BlockState state)` that calls `setBlock(pos, state, Block.UPDATE_ALL)` internally.

Expand Down

1 comment on commit f0f7044

@neoforged-pages-deployments
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploying with Cloudflare Pages

Name Result
Last commit: f0f70446e563c20f137e15a15ed48ab115fb37ef
Status: ✅ Deploy successful!
Preview URL: https://adedd712.neoforged-docs-previews.pages.dev
PR Preview URL: https://pr-178.neoforged-docs-previews.pages.dev

Please sign in to comment.