Skip to content

Commit

Permalink
feat: Add StringRepresentable
Browse files Browse the repository at this point in the history
For some usages it's helpful to have the serializable name that gets used and assists
with interpreting values from user parsed files.
  • Loading branch information
gabizou committed Jun 4, 2024
1 parent 791f262 commit 779d472
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/spongepowered/api/data/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ public final class Keys {
*/
public static final Key<Value<Ticks>> BREEDING_COOLDOWN = Keys.key(ResourceKey.sponge("breeding_cooldown"), Ticks.class);

/**
* Whether a {@link BlockState} can be lit on fire by things like spreading lava,
* or sometimes a {@link ServerLocation} to be directionally specific.
*/
public static final Key<Value<Boolean>> BURNABLE = Keys.key(ResourceKey.sponge("burnable"), Boolean.class);

/**
* The burntime of an {@link ItemStack} fuel in a furnace.
* See {@link #FUEL} for the time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
* Represents a block surface.
*/
@CatalogedBy(AttachmentSurfaces.class)
public interface AttachmentSurface extends DefaultedRegistryValue, Comparable<AttachmentSurface> {
public interface AttachmentSurface extends DefaultedRegistryValue, Comparable<AttachmentSurface>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
* Represents the type of leaves of a {@link BlockTypes#BAMBOO} block.
*/
@CatalogedBy(BambooLeavesTypes.class)
public interface BambooLeavesType extends DefaultedRegistryValue, Comparable<BambooLeavesType> {
public interface BambooLeavesType extends DefaultedRegistryValue, Comparable<BambooLeavesType>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
* Represents the attachment-type of a {@link BlockTypes#BELL} block.
*/
@CatalogedBy(BellAttachmentTypes.class)
public interface BellAttachmentType extends DefaultedRegistryValue, Comparable<BellAttachmentType> {
public interface BellAttachmentType extends DefaultedRegistryValue, Comparable<BellAttachmentType>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
* chests into a double chests.
*/
@CatalogedBy(ChestAttachmentTypes.class)
public interface ChestAttachmentType extends DefaultedRegistryValue, Comparable<ChestAttachmentType> {
public interface ChestAttachmentType extends DefaultedRegistryValue, Comparable<ChestAttachmentType>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
import org.spongepowered.api.util.annotation.CatalogedBy;

@CatalogedBy(ComparatorModes.class)
public interface ComparatorMode extends DefaultedRegistryValue, Comparable<ComparatorMode> {
public interface ComparatorMode extends DefaultedRegistryValue, Comparable<ComparatorMode>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
* Represents a side of a hinge.
*/
@CatalogedBy(DoorHinges.class)
public interface DoorHinge extends DefaultedRegistryValue, Cycleable<DoorHinge>, Comparable<DoorHinge> {
public interface DoorHinge extends DefaultedRegistryValue, Cycleable<DoorHinge>, Comparable<DoorHinge>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
import org.spongepowered.api.util.annotation.CatalogedBy;

@CatalogedBy(DripstoneSegments.class)
public interface DripstoneSegment extends DefaultedRegistryValue, Comparable<DripstoneSegment> {
public interface DripstoneSegment extends DefaultedRegistryValue, Comparable<DripstoneSegment>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Represents a type of instrument.
*/
@CatalogedBy(InstrumentTypes.class)
public interface InstrumentType extends DefaultedRegistryValue, Comparable<InstrumentType> {
public interface InstrumentType extends DefaultedRegistryValue, Comparable<InstrumentType>, StringRepresentable {

/**
* Gets the {@link SoundType} that is used by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* Represents the front and top orientation of {@link BlockTypes#JIGSAW} blocks.
*/
@CatalogedBy(JigsawBlockOrientations.class)
public interface JigsawBlockOrientation extends DefaultedRegistryValue, Comparable<JigsawBlockOrientation> {
public interface JigsawBlockOrientation extends DefaultedRegistryValue, Comparable<JigsawBlockOrientation>, StringRepresentable {

/**
* The front facing direction of a jigsaw block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
* Represents a "type" of piston.
*/
@CatalogedBy(PistonTypes.class)
public interface PistonType extends DefaultedRegistryValue, Comparable<PistonType> {
public interface PistonType extends DefaultedRegistryValue, Comparable<PistonType>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
import org.spongepowered.api.util.annotation.CatalogedBy;

@CatalogedBy(PortionTypes.class)
public interface PortionType extends DefaultedRegistryValue, Comparable<PortionType> {
public interface PortionType extends DefaultedRegistryValue, Comparable<PortionType>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
import org.spongepowered.api.util.annotation.CatalogedBy;

@CatalogedBy(RailDirections.class)
public interface RailDirection extends DefaultedRegistryValue, Cycleable<RailDirection>, Comparable<RailDirection> {
public interface RailDirection extends DefaultedRegistryValue, Cycleable<RailDirection>, Comparable<RailDirection>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
import org.spongepowered.api.util.annotation.CatalogedBy;

@CatalogedBy(SculkSensorStates.class)
public interface SculkSensorState extends DefaultedRegistryValue, Comparable<SculkSensorState> {
public interface SculkSensorState extends DefaultedRegistryValue, Comparable<SculkSensorState>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
* example {@link BlockTypes#STONE_SLAB}.
*/
@CatalogedBy(SlabPortions.class)
public interface SlabPortion extends DefaultedRegistryValue, Comparable<SlabPortion> {
public interface SlabPortion extends DefaultedRegistryValue, Comparable<SlabPortion>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
* Represents a stair shape.
*/
@CatalogedBy(StairShapes.class)
public interface StairShape extends DefaultedRegistryValue, Comparable<StairShape> {
public interface StairShape extends DefaultedRegistryValue, Comparable<StairShape>, StringRepresentable {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.data.type;

/**
* Represents a type that has a stable string representation, usually an enumerated
* set of values, like most catalog types:
* {@link org.spongepowered.api.data.type.ArtType},
* {@link RabbitType}, {@link RailDirection}, etc.
*/
public interface StringRepresentable {

String serializationString();

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
* Represents a mode that a {@link StructureBlock} may be in.
*/
@CatalogedBy(StructureModes.class)
public interface StructureMode extends DefaultedRegistryValue, Comparable<StructureMode> {
public interface StructureMode extends DefaultedRegistryValue, Comparable<StructureMode>, StringRepresentable {

}
2 changes: 1 addition & 1 deletion src/main/java/org/spongepowered/api/data/type/Tilt.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.spongepowered.api.util.annotation.CatalogedBy;

@CatalogedBy(Tilts.class)
public interface Tilt extends DefaultedRegistryValue, Comparable<Tilt> {
public interface Tilt extends DefaultedRegistryValue, Comparable<Tilt>, StringRepresentable {

/**
* {@return Whether a block entering this tilt state will trigger any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
* Represents the type of wall BlockStates
*/
@CatalogedBy(WallConnectionStates.class)
public interface WallConnectionState extends DefaultedRegistryValue, Comparable<WallConnectionState> {
public interface WallConnectionState extends DefaultedRegistryValue, Comparable<WallConnectionState>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
import org.spongepowered.api.util.annotation.CatalogedBy;

@CatalogedBy(WireAttachmentTypes.class)
public interface WireAttachmentType extends DefaultedRegistryValue, Comparable<WireAttachmentType> {
public interface WireAttachmentType extends DefaultedRegistryValue, Comparable<WireAttachmentType>, StringRepresentable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@
import org.spongepowered.api.data.type.StairShape;
import org.spongepowered.api.data.type.StructureMode;
import org.spongepowered.api.data.type.Tilt;
import org.spongepowered.api.data.type.TrialSpawnerState;
import org.spongepowered.api.data.type.TropicalFishShape;
import org.spongepowered.api.data.type.VaultState;
import org.spongepowered.api.data.type.VillagerType;
import org.spongepowered.api.data.type.WallConnectionState;
import org.spongepowered.api.data.type.WireAttachmentType;
Expand Down Expand Up @@ -496,10 +498,14 @@ public final class RegistryTypes {

public static final DefaultedRegistryType<TransactionType> TRANSACTION_TYPE = RegistryTypes.spongeKeyInGame("transaction_type");

public static final DefaultedRegistryType<TrialSpawnerState> TRIAL_SPAWNER_STATE = RegistryTypes.spongeKeyInGame("trial_spawner_state");

public static final DefaultedRegistryType<TropicalFishShape> TROPICAL_FISH_SHAPE = RegistryTypes.spongeKeyInGame("tropical_fish_shape");

public static final DefaultedRegistryType<Tilt> TILT = RegistryTypes.spongeKeyInGame("tilt");

public static final RegistryType<VaultState> VAULT_STATE = RegistryTypes.spongeKeyInGame("vault_state");

public static final DefaultedRegistryType<Visibility> VISIBILITY = RegistryTypes.spongeKeyInGame("visibility");

public static final DefaultedRegistryType<WallConnectionState> WALL_CONNECTION_STATE = RegistryTypes.spongeKeyInGame("wall_connection_state");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.api.state;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.type.StringRepresentable;
import org.spongepowered.api.util.annotation.CatalogedBy;

/**
Expand All @@ -33,7 +34,7 @@
* may not be exposed in the API.
*/
@CatalogedBy(EnumStateProperties.class)
public interface EnumStateProperty<E extends Comparable<E>> extends StateProperty<E> {
public interface EnumStateProperty<E extends Comparable<E> & StringRepresentable> extends StateProperty<E> {

static <E extends Comparable<E>> EnumStateProperty<E> of(String name) {
return Sponge.game().factoryProvider().provide(Factory.class).of(name);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/spongepowered/api/util/Axis.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
*/
package org.spongepowered.api.util;

import org.spongepowered.api.data.type.StringRepresentable;
import org.spongepowered.math.vector.Vector3d;

/**
* Represents a three dimensional cartesian axis.
*/
public enum Axis implements Cycleable<Axis> {
public enum Axis implements Cycleable<Axis>, StringRepresentable {

X(new Vector3d(1, 0, 0)),
Y(new Vector3d(0, 1, 0)),
Expand Down Expand Up @@ -142,4 +143,10 @@ public Axis cycleNext() {
}
return Axis.X;
}

@Override
public String serializationString() {
return this.name();
}

}
8 changes: 7 additions & 1 deletion src/main/java/org/spongepowered/api/util/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.api.util;

import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.spongepowered.api.data.type.StringRepresentable;
import org.spongepowered.math.GenericMath;
import org.spongepowered.math.TrigMath;
import org.spongepowered.math.vector.Vector3d;
Expand All @@ -42,7 +43,7 @@
* <li>{@link #DOWN} targeting towards -Y</li>
* </ul>
*/
public enum Direction {
public enum Direction implements StringRepresentable {
NORTH(new Vector3d(0, 0, -1), Division.CARDINAL),
NORTH_NORTHEAST(new Vector3d(C.S8, 0, -C.C8), Division.SECONDARY_ORDINAL),
NORTHEAST(new Vector3d(1, 0, -1), Division.ORDINAL),
Expand Down Expand Up @@ -341,6 +342,11 @@ public Vector3i asBlockOffset() {
return this.blockOffset;
}

@Override
public String serializationString() {
return this.name();
}

private interface C {

double C8 = Math.cos(Math.PI / 8);
Expand Down

0 comments on commit 779d472

Please sign in to comment.