-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement depth strider modifier and JSON enchantment modifier
The thing holding enchantments back before was grindstones, as we had no way to prevent grindstone disenchanting leading to an XP dupe. Now we have a way The hook is still not as ideal as the 1.19 hook, but I wanted as modifiers, whether it works or not depends on the enchantment implementation
- Loading branch information
1 parent
ce3ea5c
commit 5c65b94
Showing
24 changed files
with
256 additions
and
111 deletions.
There are no files selected for viewing
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
11 changes: 11 additions & 0 deletions
11
...ated/resources/data/tconstruct/recipes/tools/modifiers/salvage/upgrade/depth_strider.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"type": "tconstruct:modifier_salvage", | ||
"tools": { | ||
"tag": "tconstruct:modifiable/armor/boots" | ||
}, | ||
"slots": { | ||
"upgrades": 1 | ||
}, | ||
"modifier": "tconstruct:depth_strider", | ||
"min_level": 1 | ||
} |
26 changes: 26 additions & 0 deletions
26
src/generated/resources/data/tconstruct/recipes/tools/modifiers/upgrade/depth_strider.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"type": "tconstruct:modifier", | ||
"inputs": [ | ||
{ | ||
"tag": "minecraft:fishes" | ||
}, | ||
{ | ||
"item": "minecraft:prismarine_bricks" | ||
}, | ||
{ | ||
"tag": "minecraft:fishes" | ||
} | ||
], | ||
"tools": { | ||
"tag": "tconstruct:modifiable/armor/boots" | ||
}, | ||
"slots": { | ||
"upgrades": 1 | ||
}, | ||
"allow_crystal": true, | ||
"result": { | ||
"name": "tconstruct:depth_strider", | ||
"level": 1 | ||
}, | ||
"max_level": 3 | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/generated/resources/data/tconstruct/tinkering/modifiers/depth_strider.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"type": "tconstruct:enchantment", | ||
"level_display": "tconstruct:default", | ||
"enchantment": { | ||
"name": "minecraft:depth_strider", | ||
"level": 1 | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
src/main/java/slimeknights/tconstruct/library/modifiers/dynamic/EnchantmentModifier.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,121 @@ | ||
package slimeknights.tconstruct.library.modifiers.dynamic; | ||
|
||
import com.google.gson.JsonObject; | ||
import lombok.RequiredArgsConstructor; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.ListTag; | ||
import net.minecraft.nbt.Tag; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.util.GsonHelper; | ||
import net.minecraft.world.item.enchantment.Enchantment; | ||
import net.minecraft.world.item.enchantment.EnchantmentHelper; | ||
import net.minecraftforge.registries.ForgeRegistries; | ||
import slimeknights.mantle.data.GenericLoaderRegistry.IGenericLoader; | ||
import slimeknights.mantle.util.JsonHelper; | ||
import slimeknights.tconstruct.library.modifiers.Modifier; | ||
import slimeknights.tconstruct.library.modifiers.util.ModifierLevelDisplay; | ||
import slimeknights.tconstruct.library.tools.helper.ModifierUtil; | ||
import slimeknights.tconstruct.library.tools.nbt.IToolStackView; | ||
import slimeknights.tconstruct.library.utils.RestrictedCompoundTag; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Modifier that adds an enchantment. The current implementation is a bit of a hack, and will clobber enchantments from other sources. | ||
* TODO 1.19: switch to new hook to make this less of a hack | ||
*/ | ||
@RequiredArgsConstructor | ||
public class EnchantmentModifier extends Modifier { | ||
private final Enchantment enchantment; | ||
private final int enchantmentLevel; | ||
private final ModifierLevelDisplay levelDisplay; | ||
|
||
@Override | ||
public Component getDisplayName(int level) { | ||
return levelDisplay.nameForLevel(this, level); | ||
} | ||
|
||
@Override | ||
public void addRawData(IToolStackView tool, int level, RestrictedCompoundTag tag) { | ||
// first, find the enchantment tag | ||
ListTag enchantments; | ||
if (tag.contains(ModifierUtil.TAG_ENCHANTMENTS, Tag.TAG_LIST)) { | ||
enchantments = tag.getList(ModifierUtil.TAG_ENCHANTMENTS, Tag.TAG_COMPOUND); | ||
} else { | ||
enchantments = new ListTag(); | ||
tag.put(ModifierUtil.TAG_ENCHANTMENTS, enchantments); | ||
} | ||
// first, see if it already exists, if so we need to replace it | ||
String id = Objects.requireNonNull(enchantment.getRegistryName()).toString(); | ||
for (int i = 0; i < enchantments.size(); i++) { | ||
CompoundTag enchantmentTag = enchantments.getCompound(i); | ||
if (id.equals(enchantmentTag.getString("id"))) { | ||
EnchantmentHelper.setEnchantmentLevel(enchantmentTag, level * enchantmentLevel); | ||
return; | ||
} | ||
} | ||
// none of the existing tags match the enchant, so add it | ||
enchantments.add(EnchantmentHelper.storeEnchantment(enchantment.getRegistryName(), level * enchantmentLevel)); | ||
} | ||
|
||
@Override | ||
public void beforeRemoved(IToolStackView tool, RestrictedCompoundTag tag) { | ||
// when removing the modifier, remove the enchant | ||
// this will clobber anyone else trying to remove it, not much we can do | ||
if (tag.contains(ModifierUtil.TAG_ENCHANTMENTS, Tag.TAG_LIST)) { | ||
ListTag enchantments = tag.getList(ModifierUtil.TAG_ENCHANTMENTS, Tag.TAG_COMPOUND); | ||
String id = Objects.requireNonNull(enchantment.getRegistryName()).toString(); | ||
for (int i = 0; i < enchantments.size(); i++) { | ||
CompoundTag enchantmentTag = enchantments.getCompound(i); | ||
if (id.equals(enchantmentTag.getString("id"))) { | ||
enchantments.remove(i); | ||
if (enchantments.isEmpty()) { | ||
tag.remove(ModifierUtil.TAG_ENCHANTMENTS); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public IGenericLoader<? extends Modifier> getLoader() { | ||
return LOADER; | ||
} | ||
|
||
public static final IGenericLoader<EnchantmentModifier> LOADER = new IGenericLoader<>() { | ||
@Override | ||
public EnchantmentModifier deserialize(JsonObject json) { | ||
JsonObject enchantmentJson = GsonHelper.getAsJsonObject(json, "enchantment"); | ||
Enchantment enchantment = JsonHelper.getAsEntry(ForgeRegistries.ENCHANTMENTS, enchantmentJson, "name"); | ||
int level = GsonHelper.getAsInt(enchantmentJson, "level", 1); | ||
ModifierLevelDisplay display = ModifierLevelDisplay.LOADER.getAndDeserialize(json, "level_display"); | ||
return new EnchantmentModifier(enchantment, level, display); | ||
} | ||
|
||
@Override | ||
public void serialize(EnchantmentModifier object, JsonObject json) { | ||
json.add("level_display", ModifierLevelDisplay.LOADER.serialize(object.levelDisplay)); | ||
JsonObject enchantmentJson = new JsonObject(); | ||
enchantmentJson.addProperty("name", Objects.requireNonNull(object.enchantment.getRegistryName()).toString()); | ||
enchantmentJson.addProperty("level", object.enchantmentLevel); | ||
json.add("enchantment", enchantmentJson); | ||
} | ||
|
||
@Override | ||
public EnchantmentModifier fromNetwork(FriendlyByteBuf buffer) { | ||
Enchantment enchantment = buffer.readRegistryIdUnsafe(ForgeRegistries.ENCHANTMENTS); | ||
int level = buffer.readVarInt(); | ||
ModifierLevelDisplay display = ModifierLevelDisplay.LOADER.fromNetwork(buffer); | ||
return new EnchantmentModifier(enchantment, level, display); | ||
} | ||
|
||
@Override | ||
public void toNetwork(EnchantmentModifier object, FriendlyByteBuf buffer) { | ||
buffer.writeRegistryIdUnsafe(ForgeRegistries.ENCHANTMENTS, object.enchantment); | ||
buffer.writeVarInt(object.enchantmentLevel); | ||
ModifierLevelDisplay.LOADER.toNetwork(object.levelDisplay, buffer); | ||
} | ||
}; | ||
} |
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
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
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
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
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.