Skip to content

Commit

Permalink
Fix some crashes and other issues, add nutrition/saturation control a…
Browse files Browse the repository at this point in the history
…nd beta hunger for cakes
  • Loading branch information
DragonEggBedrockBreaking committed Jun 8, 2024
1 parent a229d68 commit 5cd3521
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import it.unimi.dsi.fastutil.objects.*;
import net.minecraft.commands.Commands;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.cauldron.CauldronInteraction;
Expand Down Expand Up @@ -563,6 +564,10 @@ public static void populate() {
put("nutrition", String.valueOf(foodProperties.nutrition()));
put("saturation", String.valueOf(foodProperties.saturation()));
}
if (item.equals(Items.CAKE)) {
put("nutrition", "2");
put("saturation", "0.1");
}

if (item.equals(Items.POTION) || item.equals(Items.SPLASH_POTION) ||
item.equals(Items.LINGERING_POTION) || item.equals(Items.TIPPED_ARROW)) {
Expand Down Expand Up @@ -605,7 +610,7 @@ public static void populate() {
enchantmentRegistry.forEach(enchantment1 -> {
if (enchantment1.equals(enchantment)) return;
put("compatible_with_" + lightCleanup(Objects.requireNonNull(enchantmentRegistry.getKey(enchantment1))),
String.valueOf(Enchantment.areCompatible(enchantment.exclusiveSet().get(0), enchantment1.exclusiveSet().get(0))));
String.valueOf(Enchantment.areCompatible(new Holder.Direct<>(enchantment), new Holder.Direct<>(enchantment1))));
});
}}));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package uk.debb.vanilla_disable.mixin.command.enchantment;

import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.level.block.Block;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import uk.debb.vanilla_disable.data.command.CommandDataHandler;

@Mixin(Block.class)
public abstract class MixinBlock {
@ModifyArg(
method = "getDrops(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/entity/BlockEntity;Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/item/ItemStack;)Ljava/util/List;",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/level/storage/loot/LootParams$Builder;withParameter(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;Ljava/lang/Object;)Lnet/minecraft/world/level/storage/loot/LootParams$Builder;",
ordinal = 1
),
index = 1
)
private static Object vanillaDisable$getDrops(Object value) {
if (value instanceof ItemStack itemStack) {
String item = "can_enchant_" + CommandDataHandler.lightCleanup(CommandDataHandler.getKeyFromItemRegistry(itemStack.getItem()));
ItemEnchantments itemEnchantments = itemStack.getEnchantments();
itemEnchantments.enchantments = itemEnchantments.enchantments.object2IntEntrySet().stream()
.filter(e -> CommandDataHandler.getCachedBoolean("enchantments", CommandDataHandler.getKeyFromEnchantmentRegistry(e.getKey().value()), item))
.collect(Object2IntOpenHashMap::new, (m, e) -> m.put(e.getKey(), e.getIntValue()), Object2IntOpenHashMap::putAll);
EnchantmentHelper.setEnchantments(itemStack, itemEnchantments);
return itemStack;
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -21,14 +22,19 @@ public abstract class MixinEnchantment {
if (CommandDataHandler.isConnectionNull()) return original;
if (stack.getMaxDamage() == 0) return original;
String item = "can_enchant_" + CommandDataHandler.lightCleanup(CommandDataHandler.getKeyFromItemRegistry(stack.getItem()));
return CommandDataHandler.getCachedBoolean("enchantments", CommandDataHandler.getKeyFromEnchantmentRegistry((Enchantment) (Object) this), item);
ResourceLocation enchantment = CommandDataHandler.enchantmentRegistry.getKey((Enchantment) (Object) this);
if (enchantment == null) return original;
return CommandDataHandler.getCachedBoolean("enchantments", enchantment.toString(), item);
}

@ModifyReturnValue(method = "areCompatible", at = @At("RETURN"))
private static boolean vanillaDisable$areCompatible(boolean original, Holder<Enchantment> holder, Holder<Enchantment> holder2) {
if (CommandDataHandler.isConnectionNull()) return original;
String enchantment = CommandDataHandler.getKeyFromEnchantmentRegistry(holder.value());
String otherEnchantment = "compatible_with_" + CommandDataHandler.lightCleanup(CommandDataHandler.getKeyFromEnchantmentRegistry(holder2.value()));
ResourceLocation enchantmentRL = CommandDataHandler.enchantmentRegistry.getKey(holder.value());
ResourceLocation otherEnchantmentRL = CommandDataHandler.enchantmentRegistry.getKey(holder2.value());
if (enchantmentRL == null || otherEnchantmentRL == null) return original;
String enchantment = enchantmentRL.toString();
String otherEnchantment = "compatible_with_" + CommandDataHandler.lightCleanup(otherEnchantmentRL);
String reversedEnchantment = enchantment.replace("minecraft:", "compatible_with_");
String reversedOtherEnchantment = otherEnchantment.replace("compatible_with_", "minecraft:");
return CommandDataHandler.getCachedBoolean("enchantments", enchantment, otherEnchantment) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
public abstract class MixinPainting {
@ModifyReturnValue(method = "getVariant()Lnet/minecraft/core/Holder;", at = @At("RETURN"))
private Holder<PaintingVariant> vanillaDisable$getVariant(Holder<PaintingVariant> original) {
if (!CommandDataHandler.getCachedBoolean("entities", "minecraft:painting",
CommandDataHandler.lightCleanup(Objects.requireNonNull(CommandDataHandler.paintingVariantRegistry.getKey(original.value()))) + "_painting")) {
if (!CommandDataHandler.getCachedBoolean("entities", "minecraft:painting", CommandDataHandler.lightCleanup(original.value().assetId()) + "_painting")) {
return new Holder.Direct<>(Objects.requireNonNull(CommandDataHandler.paintingVariantRegistry.get(PaintingVariants.KEBAB)));
}
return original;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class MixinPlayer {

@Inject(method = "attack", at = @At("RETURN"))
private void vanillaDisable$attack(Entity target, CallbackInfo ci) {
Holder<Enchantment> fire_aspect = Objects.requireNonNull(CommandDataHandler.enchantmentRegistry.get(Enchantments.FIRE_ASPECT)).exclusiveSet().get(0);
Holder<Enchantment> fire_aspect = new Holder.Direct<>(Objects.requireNonNull(CommandDataHandler.enchantmentRegistry.get(Enchantments.FIRE_ASPECT)));
if (target instanceof Creeper creeper && EnchantmentHelper.getItemEnchantmentLevel(fire_aspect, ((Player) (Object) this).getMainHandItem()) > 0 &&
CommandDataHandler.getCachedBoolean("entities", "minecraft:creeper", "can_be_lit_by_fire_aspect")) {
creeper.ignite();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package uk.debb.vanilla_disable.mixin.command.entity.player.hunger;

import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.CakeBlock;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import uk.debb.vanilla_disable.data.command.CommandDataHandler;

@Mixin(CakeBlock.class)
public abstract class MixinCakeBlock {
@Inject(
method = "eat",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/food/FoodData;eat(IF)V"
)
)
private static void vanillaDisable$eat(LevelAccessor level, BlockPos pos, BlockState state, Player player, CallbackInfoReturnable<InteractionResult> cir) {
if (CommandDataHandler.getCachedBoolean("entities", "minecraft:player", "beta_hunger")) {
int nutrition = CommandDataHandler.getCachedInt("items", "minecraft:cake", "nutrition");
player.setHealth(player.getHealth() + nutrition);
}
}
}
2 changes: 2 additions & 0 deletions common/src/main/resources/vanilla_disable.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"command.block.redstone_timings.MixinObserverBlock",
"command.block.redstone_timings.MixinRepeaterBlock",
"command.command.MixinCommands",
"command.enchantment.MixinBlock",
"command.enchantment.MixinEnchantment",
"command.enchantment.MixinEnchantmentHelper",
"command.entity.breeding.MixinBreedGoal",
Expand Down Expand Up @@ -73,6 +74,7 @@
"command.entity.other.MixinZombieVillager",
"command.entity.player.MixinAbilities",
"command.entity.player.MixinEntity",
"command.entity.player.hunger.MixinCakeBlock",
"command.entity.player.hunger.MixinFoodData",
"command.entity.player.hunger.MixinItem",
"command.entity.player.hunger.MixinPlayer",
Expand Down

0 comments on commit 5cd3521

Please sign in to comment.