mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-21 03:10:54 -04:00
Add builders for BlockSetType and WoodType to replace registries (#3115)
* Add builders for BlockSetType and WoodType to replace registries Adds `BlockSetTypeBuilder` and `WoodTypeBuilder` to replace `BlockSetTypeRegistry` and `WoodTypeRegistry` respectively. * whoops * double whoops * Register and Build now separate methods Separated the register and build methods, with the former calling the latter. Their relationship is explained in the javadoc. * Update BlockSetTypeBuilder.java
This commit is contained in:
parent
778f92eb32
commit
6beca848ff
5 changed files with 396 additions and 4 deletions
fabric-object-builder-api-v1/src
main/java/net/fabricmc/fabric/api/object/builder/v1/block/type
testmod/java/net/fabricmc/fabric/test/object/builder
|
@ -0,0 +1,235 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.fabricmc.fabric.api.object.builder.v1.block.type;
|
||||
|
||||
import net.minecraft.block.BlockSetType;
|
||||
import net.minecraft.sound.BlockSoundGroup;
|
||||
import net.minecraft.sound.SoundEvent;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
/**
|
||||
* This class allows easy creation of {@link BlockSetType}s.
|
||||
*
|
||||
* <p>A {@link BlockSetType} is used to tell the game various properties of related blocks, such as what sounds they should use.
|
||||
*
|
||||
* @see WoodTypeBuilder
|
||||
*/
|
||||
public final class BlockSetTypeBuilder {
|
||||
private boolean openableByHand = true;
|
||||
private BlockSoundGroup soundGroup = BlockSoundGroup.WOOD;
|
||||
private SoundEvent doorCloseSound = SoundEvents.BLOCK_WOODEN_DOOR_CLOSE;
|
||||
private SoundEvent doorOpenSound = SoundEvents.BLOCK_WOODEN_DOOR_OPEN;
|
||||
private SoundEvent trapdoorCloseSound = SoundEvents.BLOCK_WOODEN_TRAPDOOR_CLOSE;
|
||||
private SoundEvent trapdoorOpenSound = SoundEvents.BLOCK_WOODEN_TRAPDOOR_OPEN;
|
||||
private SoundEvent pressurePlateClickOffSound = SoundEvents.BLOCK_WOODEN_PRESSURE_PLATE_CLICK_OFF;
|
||||
private SoundEvent pressurePlateClickOnSound = SoundEvents.BLOCK_WOODEN_PRESSURE_PLATE_CLICK_ON;
|
||||
private SoundEvent buttonClickOffSound = SoundEvents.BLOCK_WOODEN_BUTTON_CLICK_OFF;
|
||||
private SoundEvent buttonClickOnSound = SoundEvents.BLOCK_WOODEN_BUTTON_CLICK_ON;
|
||||
|
||||
/**
|
||||
* Sets whether this block set type's door and trapdoor can be opened by hand.
|
||||
*
|
||||
* <p>Defaults to {@code true}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder openableByHand(boolean openableByHand) {
|
||||
this.openableByHand = openableByHand;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's sound group.
|
||||
*
|
||||
* <p>Defaults to {@link BlockSoundGroup#WOOD}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder soundGroup(BlockSoundGroup soundGroup) {
|
||||
this.soundGroup = soundGroup;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's door close sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_WOODEN_DOOR_CLOSE}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder doorCloseSound(SoundEvent doorCloseSound) {
|
||||
this.doorCloseSound = doorCloseSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's door open sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_WOODEN_DOOR_OPEN}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder doorOpenSound(SoundEvent doorOpenSound) {
|
||||
this.doorOpenSound = doorOpenSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's trapdoor close sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_WOODEN_TRAPDOOR_CLOSE}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder trapdoorCloseSound(SoundEvent trapdoorCloseSound) {
|
||||
this.trapdoorCloseSound = trapdoorCloseSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's trapdoor open sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_WOODEN_TRAPDOOR_OPEN}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder trapdoorOpenSound(SoundEvent trapdoorOpenSound) {
|
||||
this.trapdoorOpenSound = trapdoorOpenSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's pressure plate click off sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_WOODEN_PRESSURE_PLATE_CLICK_OFF}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder pressurePlateClickOffSound(SoundEvent pressurePlateClickOffSound) {
|
||||
this.pressurePlateClickOffSound = pressurePlateClickOffSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's pressure plate click on sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_WOODEN_PRESSURE_PLATE_CLICK_ON}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder pressurePlateClickOnSound(SoundEvent pressurePlateClickOnSound) {
|
||||
this.pressurePlateClickOnSound = pressurePlateClickOnSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's button click off sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_WOODEN_BUTTON_CLICK_OFF}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder buttonClickOffSound(SoundEvent buttonClickOffSound) {
|
||||
this.buttonClickOffSound = buttonClickOffSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this block set type's button click on sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_WOODEN_BUTTON_CLICK_ON}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public BlockSetTypeBuilder buttonClickOnSound(SoundEvent buttonClickOnSound) {
|
||||
this.buttonClickOnSound = buttonClickOnSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link BlockSetTypeBuilder} that copies all of another builder's values.
|
||||
*
|
||||
* @param builder the {@link BlockSetTypeBuilder} whose values are to be copied
|
||||
*
|
||||
* @return the created copy
|
||||
*/
|
||||
public static BlockSetTypeBuilder copyOf(BlockSetTypeBuilder builder) {
|
||||
BlockSetTypeBuilder copy = new BlockSetTypeBuilder();
|
||||
copy.openableByHand(builder.openableByHand);
|
||||
copy.soundGroup(builder.soundGroup);
|
||||
copy.doorCloseSound(builder.doorCloseSound);
|
||||
copy.doorOpenSound(builder.doorOpenSound);
|
||||
copy.trapdoorCloseSound(builder.trapdoorCloseSound);
|
||||
copy.trapdoorOpenSound(builder.trapdoorOpenSound);
|
||||
copy.pressurePlateClickOffSound(builder.pressurePlateClickOffSound);
|
||||
copy.pressurePlateClickOnSound(builder.pressurePlateClickOnSound);
|
||||
copy.buttonClickOffSound(builder.buttonClickOffSound);
|
||||
copy.buttonClickOnSound(builder.buttonClickOnSound);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link BlockSetTypeBuilder} that copies all of another block set type's values.
|
||||
*
|
||||
* @param setType the {@link BlockSetType} whose values are to be copied
|
||||
*
|
||||
* @return the created copy
|
||||
*/
|
||||
public static BlockSetTypeBuilder copyOf(BlockSetType setType) {
|
||||
BlockSetTypeBuilder copy = new BlockSetTypeBuilder();
|
||||
copy.openableByHand(setType.canOpenByHand());
|
||||
copy.soundGroup(setType.soundType());
|
||||
copy.doorCloseSound(setType.doorClose());
|
||||
copy.doorOpenSound(setType.doorOpen());
|
||||
copy.trapdoorCloseSound(setType.trapdoorClose());
|
||||
copy.trapdoorOpenSound(setType.trapdoorOpen());
|
||||
copy.pressurePlateClickOffSound(setType.pressurePlateClickOff());
|
||||
copy.pressurePlateClickOnSound(setType.pressurePlateClickOn());
|
||||
copy.buttonClickOffSound(setType.buttonClickOff());
|
||||
copy.buttonClickOnSound(setType.buttonClickOn());
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds and registers a {@link BlockSetType} from this builder's values.
|
||||
*
|
||||
* <p>Alternatively, you can use {@link #build(Identifier)} to build without registering.
|
||||
* <br>Then {@link BlockSetType#register(BlockSetType)} can be used to register it later.
|
||||
*
|
||||
* @param id the id for the built {@link BlockSetType}
|
||||
*
|
||||
* @return the built and registered {@link BlockSetType}
|
||||
*/
|
||||
public BlockSetType register(Identifier id) {
|
||||
return BlockSetType.register(this.build(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a {@link BlockSetType} from this builder's values without registering it.
|
||||
*
|
||||
* <p>Use {@link BlockSetType#register(BlockSetType)} to register it later.
|
||||
* <br>Alternatively, you can use {@link #register(Identifier)} to build and register it now.
|
||||
*
|
||||
* @param id the id for the built {@link BlockSetType}
|
||||
*
|
||||
* @return the built {@link BlockSetType}
|
||||
*/
|
||||
public BlockSetType build(Identifier id) {
|
||||
return new BlockSetType(id.toString(), openableByHand, soundGroup, doorCloseSound, doorOpenSound, trapdoorCloseSound, trapdoorOpenSound, pressurePlateClickOffSound, pressurePlateClickOnSound, buttonClickOffSound, buttonClickOnSound);
|
||||
}
|
||||
}
|
|
@ -27,7 +27,9 @@ import net.minecraft.util.Identifier;
|
|||
* <p>A {@link BlockSetType} is used to tell the game what sounds various related blocks should use.
|
||||
*
|
||||
* @see WoodTypeRegistry
|
||||
* @deprecated use {@link BlockSetTypeBuilder}
|
||||
*/
|
||||
@Deprecated
|
||||
public final class BlockSetTypeRegistry {
|
||||
private BlockSetTypeRegistry() {
|
||||
}
|
||||
|
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.fabricmc.fabric.api.object.builder.v1.block.type;
|
||||
|
||||
import net.minecraft.block.BlockSetType;
|
||||
import net.minecraft.block.WoodType;
|
||||
import net.minecraft.sound.BlockSoundGroup;
|
||||
import net.minecraft.sound.SoundEvent;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
/**
|
||||
* This class allows easy creation of {@link WoodType}s.
|
||||
*
|
||||
* <p>A {@link WoodType} is used to tell the game what textures signs should use, as well as sounds for both signs and fence gates.
|
||||
*
|
||||
* <p>Regular sign textures are stored at {@code [namespace]/textures/entity/signs/[path].png}.
|
||||
* <br>Hanging sign textures are stored at {@code [namespace]/textures/entity/signs/hanging/[path].png}.
|
||||
*
|
||||
* @see BlockSetTypeBuilder
|
||||
*/
|
||||
public final class WoodTypeBuilder {
|
||||
private BlockSoundGroup soundGroup = BlockSoundGroup.WOOD;
|
||||
private BlockSoundGroup hangingSignSoundGroup = BlockSoundGroup.HANGING_SIGN;
|
||||
private SoundEvent fenceGateCloseSound = SoundEvents.BLOCK_FENCE_GATE_CLOSE;
|
||||
private SoundEvent fenceGateOpenSound = SoundEvents.BLOCK_FENCE_GATE_OPEN;
|
||||
|
||||
/**
|
||||
* Sets this wood type's sound group.
|
||||
*
|
||||
* <p>Defaults to {@link BlockSoundGroup#WOOD}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public WoodTypeBuilder soundGroup(BlockSoundGroup soundGroup) {
|
||||
this.soundGroup = soundGroup;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this wood type's hanging sign sound group.
|
||||
*
|
||||
* <p>Defaults to {@link BlockSoundGroup#HANGING_SIGN}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public WoodTypeBuilder hangingSignSoundGroup(BlockSoundGroup hangingSignSoundGroup) {
|
||||
this.hangingSignSoundGroup = hangingSignSoundGroup;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this wood type's fence gate close sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_FENCE_GATE_CLOSE}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public WoodTypeBuilder fenceGateCloseSound(SoundEvent fenceGateCloseSound) {
|
||||
this.fenceGateCloseSound = fenceGateCloseSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this wood type's fence gate open sound.
|
||||
*
|
||||
* <p>Defaults to {@link SoundEvents#BLOCK_FENCE_GATE_OPEN}.
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public WoodTypeBuilder fenceGateOpenSound(SoundEvent fenceGateOpenSound) {
|
||||
this.fenceGateOpenSound = fenceGateOpenSound;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link WoodTypeBuilder} that copies all of another builder's values.
|
||||
*
|
||||
* @param builder the {@link WoodTypeBuilder} whose values are to be copied
|
||||
*
|
||||
* @return the created copy
|
||||
*/
|
||||
public static WoodTypeBuilder copyOf(WoodTypeBuilder builder) {
|
||||
WoodTypeBuilder copy = new WoodTypeBuilder();
|
||||
copy.soundGroup(builder.soundGroup);
|
||||
copy.hangingSignSoundGroup(builder.hangingSignSoundGroup);
|
||||
copy.fenceGateCloseSound(builder.fenceGateCloseSound);
|
||||
copy.fenceGateOpenSound(builder.fenceGateOpenSound);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link WoodTypeBuilder} that copies all of another wood type's values.
|
||||
*
|
||||
* @param woodType the {@link WoodType} whose values are to be copied
|
||||
*
|
||||
* @return the created copy
|
||||
*/
|
||||
public static WoodTypeBuilder copyOf(WoodType woodType) {
|
||||
WoodTypeBuilder copy = new WoodTypeBuilder();
|
||||
copy.soundGroup(woodType.soundType());
|
||||
copy.hangingSignSoundGroup(woodType.hangingSignSoundType());
|
||||
copy.fenceGateCloseSound(woodType.fenceGateClose());
|
||||
copy.fenceGateOpenSound(woodType.fenceGateOpen());
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds and registers a {@link WoodType} from this builder's values.
|
||||
*
|
||||
* <p>Alternatively, you can use {@link #build(Identifier, BlockSetType)} to build without registering.
|
||||
* <br>Then {@link WoodType#register(WoodType)} can be used to register it later.
|
||||
*
|
||||
* @param id the id for the built {@link WoodType}
|
||||
* @param setType the {@link BlockSetType} for the built {@link WoodType}
|
||||
*
|
||||
* @return the built and registered {@link WoodType}
|
||||
*/
|
||||
public WoodType register(Identifier id, BlockSetType setType) {
|
||||
return WoodType.register(this.build(id, setType));
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a {@link WoodType} from this builder's values without registering it.
|
||||
*
|
||||
* <p>Use {@link WoodType#register(WoodType)} to register it later.
|
||||
* <br>Alternatively, you can use {@link #register(Identifier, BlockSetType)} to build and register it now.
|
||||
*
|
||||
* @param id the id for the built {@link WoodType}
|
||||
* @param setType the {@link BlockSetType} for the built {@link WoodType}
|
||||
*
|
||||
* @return the built {@link WoodType}
|
||||
*/
|
||||
public WoodType build(Identifier id, BlockSetType setType) {
|
||||
return new WoodType(id.toString(), setType, soundGroup, hangingSignSoundGroup, fenceGateCloseSound, fenceGateOpenSound);
|
||||
}
|
||||
}
|
|
@ -31,7 +31,9 @@ import net.minecraft.util.Identifier;
|
|||
* <br>Hanging sign textures are stored at {@code [namespace]/textures/entity/signs/hanging/[path].png}.
|
||||
*
|
||||
* @see BlockSetTypeRegistry
|
||||
* @deprecated use {@link WoodTypeBuilder}
|
||||
*/
|
||||
@Deprecated
|
||||
public final class WoodTypeRegistry {
|
||||
private WoodTypeRegistry() {
|
||||
}
|
||||
|
|
|
@ -32,17 +32,19 @@ import net.minecraft.item.Item;
|
|||
import net.minecraft.item.SignItem;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.block.type.BlockSetTypeRegistry;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.block.type.WoodTypeRegistry;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.block.type.BlockSetTypeBuilder;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.block.type.WoodTypeBuilder;
|
||||
|
||||
public class TealSignTest implements ModInitializer {
|
||||
public static final BlockSetType TEAL_BLOCK_SET_TYPE = BlockSetTypeRegistry.registerWood(ObjectBuilderTestConstants.id("teal"));
|
||||
public static final WoodType TEAL_WOOD_TYPE = WoodTypeRegistry.register(ObjectBuilderTestConstants.id("teal"), TEAL_BLOCK_SET_TYPE);
|
||||
public static final Identifier TEAL_TYPE_ID = ObjectBuilderTestConstants.id("teal");
|
||||
public static final BlockSetType TEAL_BLOCK_SET_TYPE = BlockSetTypeBuilder.copyOf(BlockSetType.OAK).build(TEAL_TYPE_ID);
|
||||
public static final WoodType TEAL_WOOD_TYPE = WoodTypeBuilder.copyOf(WoodType.OAK).build(TEAL_TYPE_ID, TEAL_BLOCK_SET_TYPE);
|
||||
public static final SignBlock TEAL_SIGN = new SignBlock(FabricBlockSettings.copy(Blocks.OAK_SIGN), TEAL_WOOD_TYPE) {
|
||||
@Override
|
||||
public TealSign createBlockEntity(BlockPos pos, BlockState state) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue