下面列出了怎么用net.minecraftforge.common.property.IUnlistedProperty的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected BlockStateContainer createBlockState()
{
BlockStateContainer superState = super.createBlockState();
List<IProperty<?>> superProperties = Lists.newArrayList(superState.getProperties());
superProperties.addAll(Arrays.asList(getProperties()));
if (superState instanceof ExtendedBlockState)
{
IUnlistedProperty<?>[] unlistedProperties = ((ExtendedBlockState) superState).getUnlistedProperties().toArray(new IUnlistedProperty<?>[0]);
return new ExtendedBlockState(this, ContentBlockBaseWithSubtypes.insertSubtype(superProperties), unlistedProperties);
} else
{
return new BlockStateContainer(this, ContentBlockBaseWithSubtypes.insertSubtype(superProperties));
}
}
public static NumericalExtendedBlockState getClean(IBlockState normalState, ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
Pair<IBlockState, ImmutableMap<IUnlistedProperty<?>, Optional<?>>> key = new ImmutablePair<>(normalState, unlistedProperties);
NumericalExtendedBlockState state = cleanStates.get(key);
if (state == null) {
state = new NumericalExtendedBlockState(normalState, unlistedProperties);
cleanStates.put(key, state);
}
return state;
}
@Override
public <V> V getValue(IUnlistedProperty<V> property) {
Optional<?> value = unlistedProperties.get(property);
if (value == null) {
throw new IllegalArgumentException("Cannot get unlisted property " + property + " as it does not exist in " + getBlock().getBlockState());
}
return property.getType().cast(value.orElse(null));
}
@Override
public <V> IExtendedBlockState withProperty(IUnlistedProperty<V> property, @Nullable V value) {
Optional<?> oldValue = unlistedProperties.get(property);
if (oldValue == null) {
throw new IllegalArgumentException("Cannot set unlisted property " + property + " as it does not exist in " + getBlock().getBlockState());
}
if (Objects.equals(oldValue.orElse(null), value)) {
return this;
}
if (!property.isValid(value)) {
throw new IllegalArgumentException("Cannot set unlisted property " + property + " to " + value + " on block " + Block.REGISTRY.getNameForObject(getBlock()) + ", it is not an allowed value");
}
boolean clean = true;
ImmutableMap.Builder<IUnlistedProperty<?>, Optional<?>> builder = ImmutableMap.builder();
for (Map.Entry<IUnlistedProperty<?>, Optional<?>> entry : unlistedProperties.entrySet()) {
IUnlistedProperty<?> key = entry.getKey();
Optional<?> newValue = key.equals(property) ? Optional.ofNullable(value) : entry.getValue();
if (newValue.isPresent()) clean = false;
builder.put(key, newValue);
}
if (clean) { // no dynamic properties, lookup normal state
return cleanState;
}
return new NumericalExtendedBlockState(normalState, builder.build(), cleanState);
}
@Override
protected IBlockState createState(ImmutableMap<IProperty<?>, Comparable<?>> properties, @Nullable ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
if (getClass() != MixinExtendedBlockState.class) {
return createState(getBlock(), properties, unlistedProperties);
}
IBlockState normalState = super.createState(properties, unlistedProperties);
if (unlistedProperties == null || unlistedProperties.isEmpty()) {
return normalState;
}
return NumericalExtendedBlockState.getClean(normalState, unlistedProperties);
}
@Overwrite(remap = false)
protected BlockStateContainer.StateImplementation createState(Block block, ImmutableMap<IProperty<?>, Comparable<?>> properties, @Nullable ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
if (!isNumerical) {
return new BlockStateContainer.StateImplementation(block, properties);
}
return null;
}
protected IBlockState createState(ImmutableMap<IProperty<?>, Comparable<?>> properties, @Nullable ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
BlockStateContainer.StateImplementation state = createState(block, properties, unlistedProperties);
if (state != null) {
return state;
} else {
return NumericalBlockState.fromPropertyValueMap((BlockStateContainer) (Object) this, properties);
}
}
/*******************************************************************************
* 3. Blockstate
*******************************************************************************/
@Override
protected BlockStateContainer createBlockState()
{
return new ExtendedBlockState(this, new IProperty[]{META_PROPERTY, BlockLeaves.FANCY}, new IUnlistedProperty[]{ B3DLoader.B3DFrameProperty.INSTANCE });
}
/*******************************************************************************
* 3. Blockstate
*******************************************************************************/
@Override
protected BlockStateContainer createBlockState()
{
return new ExtendedBlockState(this, new IProperty[]{LIT, TOOL}, new IUnlistedProperty[]{});
}
@Override
public IUnlistedProperty[] getUnlistedPropertyArray() {
return new IUnlistedProperty[]{
AgriProperties.CROP_PLANT,
AgriProperties.GROWTH_STAGE,
AgriProperties.CROSS_CROP
};
}
@Override
public final IUnlistedProperty[] getUnlistedPropertyArray() {
final List<IUnlistedProperty> list = new ArrayList<>();
this.addUnlistedProperties(list::add);
list.add(AgriProperties.CUSTOM_WOOD_TYPE);
return list.toArray(new IUnlistedProperty[list.size()]);
}
@Override
protected BlockStateContainer createBlockState()
{
return new ExtendedBlockState(this, new IProperty[] { FACING_H, CREATIVE,
LABEL_UP, LABEL_DOWN, LABEL_FRONT, LABEL_BACK, LABEL_LEFT, LABEL_RIGHT },
new IUnlistedProperty<?>[] { CAMOBLOCKSTATE, CAMOBLOCKSTATEEXTENDED });
}
@Override
public ExtendedBlockState createBlockState() {
return new ExtendedBlockState(this, new IProperty[] { FACING, Properties.StaticProperty },
new IUnlistedProperty[] { Properties.AnimationProperty });
}
@Override
public ExtendedBlockState createBlockState() {
return new ExtendedBlockState(this, new IProperty[] { FACING, Properties.StaticProperty }, new IUnlistedProperty[]{ Properties.AnimationProperty });
}
@Override
public ExtendedBlockState createBlockState() {
return new ExtendedBlockState(this, new IProperty[] { FACING, Properties.StaticProperty },
new IUnlistedProperty[] { Properties.AnimationProperty });
}
@Override
public ExtendedBlockState createBlockState() {
return new ExtendedBlockState(this, new IProperty[] { FACING, Properties.StaticProperty }, new IUnlistedProperty[]{ Properties.AnimationProperty });
}
@Override
protected BlockStateContainer createBlockState() {
return new ExtendedBlockState(this, new IProperty[]{OPAQUE}, new IUnlistedProperty[]{HARVEST_TOOL, HARVEST_LEVEL});
}
@Override
protected BlockStateContainer createBlockState() {
return new ExtendedBlockState(this,
new IProperty[] {Properties.StaticProperty, FACING_HORIZ },
new IUnlistedProperty[] {Properties.AnimationProperty});
}
public NumericalExtendedBlockState(IBlockState normalState, ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
this(normalState, unlistedProperties, null);
}
private NumericalExtendedBlockState(IBlockState normalState, ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties, NumericalExtendedBlockState cleanState) {
this.normalState = normalState;
this.unlistedProperties = unlistedProperties;
this.cleanState = cleanState == null ? this : cleanState;
}
@Override
public ImmutableMap<IUnlistedProperty<?>, Optional<?>> getUnlistedProperties() {
return unlistedProperties;
}
@Override
public Collection<IUnlistedProperty<?>> getUnlistedNames() {
return unlistedProperties.keySet();
}
public MixinExtendedBlockState(Block block, IProperty<?>[] properties, ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
super(block, properties, unlistedProperties);
}
@Inject(method = "createState", remap = false, at = @At("HEAD"), cancellable = true)
protected void overrideCreateState(Block block, ImmutableMap<IProperty<?>, Comparable<?>> properties, @Nullable ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties, CallbackInfoReturnable<BlockStateContainer.StateImplementation> cir) {
if (isNumerical) {
cir.setReturnValue(null);
}
}
@Overwrite
public MixinBlockStateContainer(Block block, IProperty<?>[] properties, ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
this.block = block;
this.unlistedProperties = unlistedProperties;
// Immutable map builder won't work, some mods have duplicate properties
Map<String, IProperty<?>> propertyMap = new LinkedHashMap<>();
int offset = 0;
for (IProperty<?> property : properties) {
validateProperty(block, property);
propertyMap.put(property.getName(), property);
if (isNumerical) {
NumericalBlockState.makePropertyInfo(property);
propertyOffsets.put(property, offset);
offset += MathHelper.log2(property.getAllowedValues().size()) + 1;
}
}
this.properties = ImmutableSortedMap.copyOf(propertyMap);
if (!isNumerical) {
Map<Map<IProperty<?>, Comparable<?>>, BlockStateContainer.StateImplementation> map2 = Maps.newLinkedHashMap();
List<BlockStateContainer.StateImplementation> validStates = Lists.newArrayList();
for (List<Comparable<?>> list : Cartesian.cartesianProduct(getAllowedValues())) {
Map<IProperty<?>, Comparable<?>> map1 = MapPopulator.createMap(this.properties.values(), list);
BlockStateContainer.StateImplementation blockstatecontainer$stateimplementation = createState(block, ImmutableMap.copyOf(map1), unlistedProperties);
map2.put(map1, blockstatecontainer$stateimplementation);
validStates.add(blockstatecontainer$stateimplementation);
}
for (BlockStateContainer.StateImplementation blockstatecontainer$stateimplementation1 : validStates) {
blockstatecontainer$stateimplementation1.buildPropertyValueTable(map2);
}
this.validStates = ImmutableList.copyOf(validStates);
}
x = new Object();
}
@Override
public BlockStateContainer createBlockState() {
return new ExtendedBlockState(this, new IProperty[0], new IUnlistedProperty[] {Properties.AnimationProperty});
}
@Override
public BlockStateContainer createBlockState() {
return new ExtendedBlockState(this, new IProperty[0], new IUnlistedProperty[] {Properties.AnimationProperty});
}
@Override
public BlockStateContainer createBlockState() {
return new ExtendedBlockState(this, new IProperty[0], new IUnlistedProperty[] {Properties.AnimationProperty});
}
@Override
public BlockStateContainer createBlockState() {
return new ExtendedBlockState(this, new IProperty[0], new IUnlistedProperty[] {Properties.AnimationProperty});
}
@Override
protected BlockStateContainer createBlockState()
{
return new ExtendedBlockState(this, new IProperty[]{}, new IUnlistedProperty[]{INVENTORY});
}