-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate rebalanced to modifier modules
Decided for this one to make it two modules, one for the base slots/name/data, and one to handle the penalties, named bonus here. Idea being you could use it to add penalties that are not just the opposite of the slot added, or to add additional slots when a certain type is chosen When the full composable between datapacks functionality is done, will also let addons with new slot types add their own penalties without overwriting rebalanced
- Loading branch information
1 parent
85494b7
commit 0d6e87c
Showing
7 changed files
with
272 additions
and
36 deletions.
There are no files selected for viewing
22 changes: 16 additions & 6 deletions
22
src/generated/resources/data/tconstruct/tinkering/modifiers/rebalanced.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
{ | ||
"type": "tconstruct:swappable_extra_slot", | ||
"slots": 1, | ||
"always_show": false, | ||
"penalize": { | ||
"abilities": "upgrades" | ||
} | ||
"type": "tconstruct:composable", | ||
"level_display": "tconstruct:no_levels", | ||
"tooltip_display": "tinker_station", | ||
"priority": 50, | ||
"modules": [ | ||
{ | ||
"type": "tconstruct:swappable_slot", | ||
"slots": 1 | ||
}, | ||
{ | ||
"type": "tconstruct:swappable_bonus_slot", | ||
"match": "abilities", | ||
"bonus": "upgrades", | ||
"slots": -1 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
src/main/java/slimeknights/tconstruct/library/modifiers/modules/SwappableSlotModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package slimeknights.tconstruct.library.modifiers.modules; | ||
|
||
import com.google.gson.JsonObject; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.chat.TranslatableComponent; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.util.GsonHelper; | ||
import slimeknights.mantle.data.GenericLoaderRegistry.IGenericLoader; | ||
import slimeknights.tconstruct.TConstruct; | ||
import slimeknights.tconstruct.library.modifiers.Modifier; | ||
import slimeknights.tconstruct.library.modifiers.ModifierEntry; | ||
import slimeknights.tconstruct.library.modifiers.ModifierHook; | ||
import slimeknights.tconstruct.library.modifiers.TinkerHooks; | ||
import slimeknights.tconstruct.library.modifiers.hook.DisplayNameModifierHook; | ||
import slimeknights.tconstruct.library.modifiers.hook.build.ModifierRemovalHook; | ||
import slimeknights.tconstruct.library.modifiers.hook.build.VolatileDataModifierHook; | ||
import slimeknights.tconstruct.library.modifiers.util.ModuleWithKey; | ||
import slimeknights.tconstruct.library.tools.SlotType; | ||
import slimeknights.tconstruct.library.tools.context.ToolRebuildContext; | ||
import slimeknights.tconstruct.library.tools.nbt.IToolStackView; | ||
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.List; | ||
|
||
/** | ||
* Module for a extra slot modifier with multiple variants based on the slot type | ||
* @param key Persistent data key containing the slot name. If null, uses the modifier ID. | ||
* Presently, changing this makes it incompatible with the swappable modifier recipe, this is added for future proofing. | ||
* @param slotCount Number of slots to grant | ||
*/ | ||
public record SwappableSlotModule(@Nullable ResourceLocation key, int slotCount) implements VolatileDataModifierHook, DisplayNameModifierHook, ModifierRemovalHook, ModifierModule, ModuleWithKey { | ||
private static final List<ModifierHook<?>> DEFAULT_HOOKS = List.of(TinkerHooks.VOLATILE_DATA, TinkerHooks.DISPLAY_NAME, TinkerHooks.REMOVE); | ||
/** Format key for swappable variant */ | ||
private static final String FORMAT = TConstruct.makeTranslationKey("modifier", "extra_modifier.type_format"); | ||
|
||
public SwappableSlotModule(int slotCount) { | ||
this(null, slotCount); | ||
} | ||
|
||
@Override | ||
public Component getDisplayName(IToolStackView tool, Modifier modifier, int level, Component name) { | ||
String slotName = tool.getPersistentData().getString(getKey(modifier)); | ||
if (!slotName.isEmpty()) { | ||
SlotType type = SlotType.getIfPresent(slotName); | ||
if (type != null) { | ||
return new TranslatableComponent(FORMAT, name.plainCopy(), type.getDisplayName()).withStyle(style -> style.withColor(type.getColor())); | ||
} | ||
} | ||
return name; | ||
} | ||
|
||
@Override | ||
public void addVolatileData(ToolRebuildContext context, ModifierEntry modifier, ModDataNBT volatileData) { | ||
String slotName = context.getPersistentData().getString(getKey(modifier.getModifier())); | ||
if (!slotName.isEmpty()) { | ||
SlotType type = SlotType.getIfPresent(slotName); | ||
if (type != null) { | ||
volatileData.addSlots(type, slotCount); | ||
} | ||
} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Component onRemoved(IToolStackView tool, Modifier modifier) { | ||
tool.getPersistentData().remove(modifier.getId()); | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<ModifierHook<?>> getDefaultHooks() { | ||
return DEFAULT_HOOKS; | ||
} | ||
|
||
@Override | ||
public IGenericLoader<? extends ModifierModule> getLoader() { | ||
return LOADER; | ||
} | ||
|
||
/** Loader instance */ | ||
public static final IGenericLoader<SwappableSlotModule> LOADER = new IGenericLoader<>() { | ||
@Override | ||
public SwappableSlotModule deserialize(JsonObject json) { | ||
ResourceLocation key = ModuleWithKey.parseKey(json); | ||
int slotCount = GsonHelper.getAsInt(json, "slots"); | ||
return new SwappableSlotModule(key, slotCount); | ||
} | ||
|
||
@Override | ||
public void serialize(SwappableSlotModule object, JsonObject json) { | ||
if (object.key != null) { | ||
json.addProperty("key", object.key.toString()); | ||
} | ||
json.addProperty("slots", object.slotCount); | ||
} | ||
|
||
@Override | ||
public SwappableSlotModule fromNetwork(FriendlyByteBuf buffer) { | ||
ResourceLocation key = ModuleWithKey.fromNetwork(buffer); | ||
int slotCount = buffer.readInt(); | ||
return new SwappableSlotModule(key, slotCount); | ||
} | ||
|
||
@Override | ||
public void toNetwork(SwappableSlotModule object, FriendlyByteBuf buffer) { | ||
ModuleWithKey.toNetwork(object.key, buffer); | ||
buffer.writeInt(object.slotCount); | ||
} | ||
}; | ||
|
||
/** Module to add (or remove) additional slots based on the given swappable slot type */ | ||
public record BonusSlot(@Nullable ResourceLocation key, SlotType match, SlotType bonus, int slotCount) implements VolatileDataModifierHook, ModifierModule, ModuleWithKey { | ||
private static final List<ModifierHook<?>> DEFAULT_HOOKS = List.of(TinkerHooks.VOLATILE_DATA); | ||
|
||
public BonusSlot(SlotType match, SlotType penalty, int slotCount) { | ||
this(null, match, penalty, slotCount); | ||
} | ||
|
||
@Override | ||
public void addVolatileData(ToolRebuildContext context, ModifierEntry modifier, ModDataNBT volatileData) { | ||
String slotName = context.getPersistentData().getString(getKey(modifier.getModifier())); | ||
if (!slotName.isEmpty() && match.getName().equals(slotName)) { | ||
volatileData.addSlots(bonus, slotCount); | ||
} | ||
} | ||
|
||
@Override | ||
public List<ModifierHook<?>> getDefaultHooks() { | ||
return DEFAULT_HOOKS; | ||
} | ||
|
||
@Override | ||
public IGenericLoader<? extends ModifierModule> getLoader() { | ||
return LOADER; | ||
} | ||
|
||
public static final IGenericLoader<BonusSlot> LOADER = new IGenericLoader<>() { | ||
@Override | ||
public BonusSlot deserialize(JsonObject json) { | ||
ResourceLocation key = ModuleWithKey.parseKey(json); | ||
SlotType match = SlotType.getOrCreate(GsonHelper.getAsString(json, "match")); | ||
SlotType bonus = SlotType.getOrCreate(GsonHelper.getAsString(json, "bonus")); | ||
int slotCount = GsonHelper.getAsInt(json, "slots"); | ||
return new BonusSlot(key, match, bonus, slotCount); | ||
} | ||
|
||
@Override | ||
public void serialize(BonusSlot object, JsonObject json) { | ||
if (object.key != null) { | ||
json.addProperty("key", object.key.toString()); | ||
} | ||
json.addProperty("match", object.match.getName()); | ||
json.addProperty("bonus", object.bonus.getName()); | ||
json.addProperty("slots", object.slotCount); | ||
} | ||
|
||
@Override | ||
public BonusSlot fromNetwork(FriendlyByteBuf buffer) { | ||
ResourceLocation key = ModuleWithKey.fromNetwork(buffer); | ||
SlotType match = SlotType.read(buffer); | ||
SlotType bonus = SlotType.read(buffer); | ||
int slots = buffer.readInt(); | ||
return new BonusSlot(key, match, bonus, slots); | ||
} | ||
|
||
@Override | ||
public void toNetwork(BonusSlot object, FriendlyByteBuf buffer) { | ||
ModuleWithKey.toNetwork(object.key, buffer); | ||
object.match.write(buffer); | ||
object.bonus.write(buffer); | ||
buffer.writeInt(object.slotCount); | ||
} | ||
}; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/slimeknights/tconstruct/library/modifiers/util/ModuleWithKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package slimeknights.tconstruct.library.modifiers.util; | ||
|
||
import com.google.gson.JsonObject; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.resources.ResourceLocation; | ||
import slimeknights.mantle.util.JsonHelper; | ||
import slimeknights.tconstruct.library.modifiers.Modifier; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Shared boilerplate for a module with a nullable key that can alternatively be the modifier ID | ||
*/ | ||
public interface ModuleWithKey { | ||
/** Gets the key for the module */ | ||
default ResourceLocation getKey(Modifier modifier) { | ||
ResourceLocation key = key(); | ||
if (key != null) { | ||
return key; | ||
} | ||
return modifier.getId(); | ||
} | ||
|
||
/** Gets the key field from the record */ | ||
@Nullable | ||
ResourceLocation key(); | ||
|
||
/** | ||
* Parses the key from JSON | ||
* @param json Json object | ||
* @return Key, or null if not present | ||
*/ | ||
@Nullable | ||
static ResourceLocation parseKey(JsonObject json) { | ||
if (json.has("key")) { | ||
return JsonHelper.getResourceLocation(json, "key"); | ||
} | ||
return null; | ||
} | ||
|
||
/** Reads the key from the network */ | ||
@Nullable | ||
static ResourceLocation fromNetwork(FriendlyByteBuf buffer) { | ||
if (buffer.readBoolean()) { | ||
return buffer.readResourceLocation(); | ||
} | ||
return null; | ||
} | ||
|
||
/** Writes the key to the network */ | ||
static void toNetwork(@Nullable ResourceLocation key, FriendlyByteBuf buffer) { | ||
if (key != null) { | ||
buffer.writeBoolean(true); | ||
buffer.writeResourceLocation(key); | ||
} else { | ||
buffer.writeBoolean(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.