Skip to content

Commit

Permalink
feature/settings-rework (ldtteam#9621)
Browse files Browse the repository at this point in the history
Some setting generics to allow getSettingOrDefault without optional unpacking
  • Loading branch information
Raycoms authored Dec 13, 2023
1 parent 9a979e5 commit a03cb8d
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,16 @@ else if (getBuildingLevel() <= WOOD_HUT_LEVEL)
* @param <T> the key type.
* @return the setting.
*/
<T extends ISetting> T getSetting(@NotNull final ISettingKey<T> key);
<T extends ISetting<S>, S> T getSetting(@NotNull final ISettingKey<T> key);

/**
* Get setting value for key or some default. Utility function.
* @param key the key.
* @param <T> the key type.
* @param def the default.
* @return the setting.
*/
<T extends ISetting<S>, S> S getSettingValueOrDefault(@NotNull final ISettingKey<T> key, @NotNull final S def);

/**
* Check if the assigned citizens are allowed to eat the following stack.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public interface ISettingsModule extends IBuildingModule
* @param setting the setting.
* @return the instance of the module.
*/
ISettingsModule with(final ISettingKey<?> key, final ISetting setting);
ISettingsModule with(final ISettingKey<?> key, final ISetting<?> setting);

/**
* Get a specific setting.
* @param key the key of the setting.
* @param <T> the type of setting.
* @return the setting.
*/
<T extends ISetting> T getSetting(final ISettingKey<T> key);
<T extends ISetting<?>> T getSetting(final ISettingKey<T> key);

/**
* Get a specific setting.
Expand All @@ -35,13 +35,23 @@ public interface ISettingsModule extends IBuildingModule
* @return the setting, if it exists and is active.
*/
@NotNull
<T extends ISetting> Optional<T> getOptionalSetting(final ISettingKey<T> key);
<T extends ISetting<?>> Optional<T> getOptionalSetting(final ISettingKey<T> key);

/**
* Update a given settings value.
* @param settingKey the given key.
* @param value the value.
* @param sender the player that updated the setting.
*/
void updateSetting(ISettingKey<?> settingKey, ISetting value, final ServerPlayer sender);
void updateSetting(ISettingKey<?> settingKey, ISetting<?> value, final ServerPlayer sender);

/**
* Get setting value or default.
* @param key the key of the setting.
* @param <T> the type of setting.
* @param <S> the setting value type.
* @param def the default.
* @return the setting value or the default.
*/
<S, T extends ISetting<S>> S getSettingValueOrDefault(ISettingKey<T> key, S def);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import com.minecolonies.api.colony.buildings.IBuilding;
import com.minecolonies.api.colony.buildings.views.IBuildingView;
import com.minecolonies.api.colony.requestsystem.token.IToken;
import com.minecolonies.api.crafting.IRecipeStorage;
import net.minecraft.world.item.ItemStack;

import java.util.List;

/**
* String Setting.
* Crafting Setting.
*/
public interface ICraftingSetting extends ISetting
public interface ICraftingSetting extends ISetting<IToken<?>>
{
/**
* Get the setting value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Generic ISetting that represents all possible setting objects (string, numbers, boolean, etc).
*/
public interface ISetting
public interface ISetting<S>
{
/**
* Get the resource location of the view you want to use for this setting.
Expand Down Expand Up @@ -95,14 +95,14 @@ default void onUpdate(final IBuilding building, final ServerPlayer sender) {}
*
* @param setting the setting with new data
*/
default void updateSetting(final ISetting setting) {}
default void updateSetting(final ISetting<S> setting) {}

/**
* Copy value from another instance.
*
* @param setting the setting to copy from
*/
void copyValue(final ISetting setting);
void copyValue(final ISetting<S> setting);

/**
* Generates the hover pane for inactive settings.
Expand Down Expand Up @@ -146,4 +146,10 @@ default boolean isActive(final ISettingsModuleView module)
{
return true;
}

/**
* Get the setting value.
* @return the value.
*/
S getValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
/**
* String Setting.
*/
public interface IStringSetting extends ISetting
public interface IStringSetting<S> extends ISetting<S>
{
/**
* Get the setting value.
* @return the current value.
*/
String getValue();
S getValue();

/**
* Get the default value.
* @return the default value.
*/
String getDefault();
S getDefault();

/**
* Get the current index of the setting.
Expand All @@ -35,12 +35,12 @@ public interface IStringSetting extends ISetting
* Set the setting to a specific index.
* @param value the value to set.
*/
void set(final String value);
void set(final S value);

@Override
default void copyValue(final ISetting setting)
default void copyValue(final ISetting<S> setting)
{
if (setting instanceof final IStringSetting other)
if (setting instanceof final IStringSetting<S> other)
{
set(other.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1187,11 +1187,18 @@ public List<IItemHandler> getHandlers()
}

@Override
public <T extends ISetting> T getSetting(@NotNull final ISettingKey<T> key)
public <T extends ISetting<S>, S> T getSetting(@NotNull final ISettingKey<T> key)
{
return getFirstModuleOccurance(ISettingsModule.class).getSetting(key);
}

@Override
public <T extends ISetting<S>, S> S getSettingValueOrDefault(@NotNull final ISettingKey<T> key, @NotNull final S def)
{
return getFirstModuleOccurance(ISettingsModule.class).getSettingValueOrDefault(key, def);
}


/**
* Get the right module for the recipe.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,6 @@ public class BuildingModules
new BuildingEntry.ModuleProducer<>("miner_settings", () -> new SettingsModule()
.with(AbstractCraftingBuildingModule.RECIPE_MODE, new CrafterRecipeSetting())
.with(BuildingMiner.FILL_BLOCK, new BlockSetting((BlockItem) Items.COBBLESTONE))
.with(BuildingBuilder.BUILDING_MODE, new BuilderModeSetting())
.with(BuildingMiner.MAX_DEPTH, new IntSetting(-100))
.with(AbstractBuilding.USE_SHEARS, new BoolSetting(true)), () -> SettingsModuleView::new);
public static final BuildingEntry.ModuleProducer<IBuildingModule,MinerGuardAssignModuleView> MINER_GUARD_ASSIGN =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ public class SettingsModule extends AbstractBuildingModule implements IPersisten
/**
* Map of setting id (string) to generic setting.
*/
final Map<ISettingKey<?>, ISetting> settings = new LinkedHashMap<>();
final Map<ISettingKey<?>, ISetting<?>> settings = new LinkedHashMap<>();

@Override
public <T extends ISetting> T getSetting(final ISettingKey<T> key)
public <T extends ISetting<?>> T getSetting(final ISettingKey<T> key)
{
return (T) settings.getOrDefault(key, null);
}

@Override
@NotNull
public <T extends ISetting> Optional<T> getOptionalSetting(final ISettingKey<T> key)
public <T extends ISetting<?>> Optional<T> getOptionalSetting(final ISettingKey<T> key)
{
final T setting = getSetting(key);
return setting == null || !setting.isActive(this) ? Optional.empty() : Optional.of(setting);
}

@Override
public ISettingsModule with(final ISettingKey<?> key, final ISetting setting)
public ISettingsModule with(final ISettingKey<?> key, final ISetting<?> setting)
{
settings.put(key, setting);
return this;
Expand Down Expand Up @@ -81,7 +81,7 @@ public void deserializeNBT(final CompoundTag compound)
public void serializeNBT(final CompoundTag compound)
{
final ListTag list = new ListTag();
for (final Map.Entry<ISettingKey<?>, ISetting> setting : settings.entrySet())
for (final Map.Entry<ISettingKey<?>, ISetting<?>> setting : settings.entrySet())
{
final CompoundTag entryCompound = new CompoundTag();
entryCompound.putString("key", setting.getKey().getUniqueId().toString());
Expand All @@ -95,20 +95,27 @@ public void serializeNBT(final CompoundTag compound)
public void serializeToView(final FriendlyByteBuf buf)
{
buf.writeInt(settings.size());
for (final Map.Entry<ISettingKey<?>, ISetting> setting : settings.entrySet())
for (final Map.Entry<ISettingKey<?>, ISetting<?>> setting : settings.entrySet())
{
buf.writeResourceLocation(setting.getKey().getUniqueId());
StandardFactoryController.getInstance().serialize(buf, setting.getValue());
}
}

@Override
public void updateSetting(final ISettingKey<?> settingKey, final ISetting value, final ServerPlayer sender)
public void updateSetting(final ISettingKey<?> settingKey, final ISetting<?> value, final ServerPlayer sender)
{
if (settings.containsKey(settingKey))
{
settings.put(settingKey, value);
value.onUpdate(building, sender);
}
}

@Override
public <S, T extends ISetting<S>> S getSettingValueOrDefault(final ISettingKey<T> key, final S def)
{
final T setting = getSetting(key);
return setting == null ? def : setting.getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/**
* Stores a solid block setting.
*/
public class BlockSetting implements ISetting
public class BlockSetting implements ISetting<BlockItem>
{
/**
* Default value of the setting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Stores a boolean setting.
*/
public class BoolSetting implements ISetting
public class BoolSetting implements ISetting<Boolean>
{
/**
* Default value of the setting.
Expand Down Expand Up @@ -58,7 +58,7 @@ public BoolSetting(final boolean value, final boolean def)
*
* @return the set value.
*/
public boolean getValue()
public Boolean getValue()
{
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public BuilderModeSetting(final List<String> value, final int curr)
@NotNull
public static String getActualValue(@NotNull final IBuilding building)
{
return building.getSetting(BuildingBuilder.BUILDING_MODE).getValue();
return building.getSettingValueOrDefault(BuildingBuilder.BUILDING_MODE, Structurize.getConfig().getServer().iteratorType.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Stores an integer setting.
*/
public class IntSetting implements ISetting
public class IntSetting implements ISetting<Integer>
{
/**
* Default value of the setting.
Expand Down Expand Up @@ -51,7 +51,7 @@ public IntSetting(final int value, final int def)
* Get the setting value.
* @return the set value.
*/
public int getValue()
public Integer getValue()
{
return value;
}
Expand Down
Loading

0 comments on commit a03cb8d

Please sign in to comment.