Baited fishing rods no longer consume the worm when casting

This commit is contained in:
Sollace 2024-10-30 20:27:28 +00:00
parent e5cf19fe55
commit ff18b2d4f0
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
8 changed files with 131 additions and 3 deletions

View file

@ -65,6 +65,7 @@ import net.minecraft.client.world.ClientWorld;
import net.minecraft.component.type.DyedColorComponent; import net.minecraft.component.type.DyedColorComponent;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluids; import net.minecraft.fluid.Fluids;
import net.minecraft.item.*; import net.minecraft.item.*;
import net.minecraft.particle.ParticleEffect; import net.minecraft.particle.ParticleEffect;
@ -168,6 +169,16 @@ public interface URenderers {
return (float)lastAngle; return (float)lastAngle;
} }
}); });
ModelPredicateProviderRegistry.register(UItems.BAITED_FISHING_ROD, Identifier.ofVanilla("cast"), (stack, world, entity, seed) -> {
if (entity == null) {
return 0;
} else {
boolean main = entity.getMainHandStack() == stack;
boolean offhand = entity.getOffHandStack() == stack && !(entity.getMainHandStack().getItem() instanceof FishingRodItem);
return (main || offhand) && entity instanceof PlayerEntity p && p.fishHook != null ? 1 : 0;
}
});
ColorProviderRegistry.BLOCK.register(URenderers::getTintedBlockColor, TintedBlock.REGISTRY.stream().toArray(Block[]::new)); ColorProviderRegistry.BLOCK.register(URenderers::getTintedBlockColor, TintedBlock.REGISTRY.stream().toArray(Block[]::new));
ColorProviderRegistry.ITEM.register((stack, i) -> getTintedBlockColor(Block.getBlockFromItem(stack.getItem()).getDefaultState(), null, null, i), TintedBlock.REGISTRY.stream().map(Block::asItem).filter(i -> i != Items.AIR).toArray(Item[]::new)); ColorProviderRegistry.ITEM.register((stack, i) -> getTintedBlockColor(Block.getBlockFromItem(stack.getItem()).getDefaultState(), null, null, i), TintedBlock.REGISTRY.stream().map(Block::asItem).filter(i -> i != Items.AIR).toArray(Item[]::new));

View file

@ -151,6 +151,8 @@ public class UModelProvider extends FabricModelProvider {
.upload(UItems.GEMSTONE, itemModelGenerator); .upload(UItems.GEMSTONE, itemModelGenerator);
// fishing rod // fishing rod
ItemModels.register(itemModelGenerator, Models.HANDHELD_ROD, UItems.BAITED_FISHING_ROD); ModelOverrides.of(Models.HANDHELD_ROD)
.addOverride(ModelIds.getItemSubModelId(Items.FISHING_ROD, "_cast"), "cast", 1)
.upload(UItems.BAITED_FISHING_ROD, itemModelGenerator);
} }
} }

View file

@ -0,0 +1,5 @@
package com.minelittlepony.unicopia.entity;
public interface FishingRod {
}

View file

@ -1,9 +1,12 @@
package com.minelittlepony.unicopia.item; package com.minelittlepony.unicopia.item;
import javax.annotation.Nullable;
import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.FishingBobberEntity; import net.minecraft.entity.projectile.FishingBobberEntity;
import net.minecraft.item.FishingRodItem; import net.minecraft.item.FishingRodItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
@ -19,6 +22,8 @@ public class BaitedFishingRodItem extends FishingRodItem {
@Override @Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) { public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
@Nullable
FishingBobberEntity oldFishHook = user.fishHook;
TypedActionResult<ItemStack> result = super.use(world, user, hand); TypedActionResult<ItemStack> result = super.use(world, user, hand);
if (!world.isClient) { if (!world.isClient) {
if (user.fishHook != null) { if (user.fishHook != null) {
@ -26,14 +31,25 @@ public class BaitedFishingRodItem extends FishingRodItem {
ItemStack stack = user.getStackInHand(hand); ItemStack stack = user.getStackInHand(hand);
int lure = (int)((EnchantmentHelper.getFishingTimeReduction((ServerWorld)world, stack, user) + 1) * 20F); int lure = (int)((EnchantmentHelper.getFishingTimeReduction((ServerWorld)world, stack, user) + 1) * 20F);
int luck = (EnchantmentHelper.getFishingLuckBonus((ServerWorld)world, stack, user) + 1) * 2; int luck = (EnchantmentHelper.getFishingLuckBonus((ServerWorld)world, stack, user) + 1) * 2;
world.spawnEntity(new FishingBobberEntity(user, world, luck, lure)); FishingBobberEntity bobber = new FishingBobberEntity(user, world, luck, lure);
((BaitedFishingBobber)bobber).setRodType(this);
world.spawnEntity(bobber);
} }
if (result.getValue().isOf(this)) { if (oldFishHook != null
&& oldFishHook.getHookedEntity() == null
&& oldFishHook.getDataTracker().get(FishingBobberEntity.CAUGHT_FISH)
&& result.getValue().isOf(this)) {
ItemStack stack = result.getValue().withItem(Items.FISHING_ROD); ItemStack stack = result.getValue().withItem(Items.FISHING_ROD);
return TypedActionResult.success(stack, world.isClient()); return TypedActionResult.success(stack, world.isClient());
} }
} }
return result; return result;
} }
public interface BaitedFishingBobber {
Item getRodType();
void setRodType(Item rodType);
}
} }

View file

@ -0,0 +1,62 @@
package com.minelittlepony.unicopia.mixin;
import org.jetbrains.annotations.Nullable;
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.CallbackInfo;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.minelittlepony.unicopia.item.BaitedFishingRodItem;
import com.minelittlepony.unicopia.util.serialization.NbtSerialisable;
import net.minecraft.entity.projectile.FishingBobberEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.registry.Registries;
@Mixin(FishingBobberEntity.class)
abstract class MixinFishingBobberEntity extends ProjectileEntity implements BaitedFishingRodItem.BaitedFishingBobber {
private MixinFishingBobberEntity() { super(null, null); }
@Nullable
private Item rodType;
@Nullable
@Override
public Item getRodType() {
return rodType;
}
@Override
public void setRodType(Item rodType) {
this.rodType = rodType;
}
@ModifyExpressionValue(method = "removeIfInvalid", at = {
@At(value = "INVOKE", target = "net/minecraft/entity/player/PlayerEntity.getMainHandStack()Lnet/minecraft/item/ItemStack;"),
@At(value = "INVOKE", target = "net/minecraft/entity/player/PlayerEntity.getOffHandStack()Lnet/minecraft/item/ItemStack;")
}, expect = 2)
private ItemStack replaceFishingRodItem(ItemStack initialStack) {
if (rodType != null) {
return initialStack.isOf(rodType) ? Items.FISHING_ROD.getDefaultStack() : ItemStack.EMPTY;
}
return initialStack;
}
@Inject(method = "writeCustomDataToNbt", at = @At("HEAD"))
private void onWriteCustomDataToNbt(NbtCompound nbt, CallbackInfo info) {
nbt.putString("rodType", Registries.ITEM.getId(rodType == null ? Items.FISHING_ROD : rodType).toString());
}
@Inject(method = "readCustomDataFromNbt", at = @At("HEAD"))
private void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo info) {
rodType = nbt.contains("rodType", NbtElement.STRING_TYPE)
? NbtSerialisable.decode(Registries.ITEM.getCodec(), nbt.get("rodType"), getRegistryManager()).orElse(null)
: null;
}
}

View file

@ -0,0 +1,28 @@
package com.minelittlepony.unicopia.mixin.client;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.minelittlepony.unicopia.item.BaitedFishingRodItem;
import net.minecraft.client.render.entity.FishingBobberEntityRenderer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
@Mixin(FishingBobberEntityRenderer.class)
abstract class MixinFishingBobberEntityRenderer {
@ModifyExpressionValue(method = "getHandPos", at = {
@At(value = "INVOKE", target = "net/minecraft/entity/player/PlayerEntity.getMainHandStack()Lnet/minecraft/item/ItemStack;")
}, expect = 2)
private ItemStack replaceFishingRodItem(ItemStack initialStack, PlayerEntity player, float f, float tickDelta) {
if (player.fishHook instanceof BaitedFishingRodItem.BaitedFishingBobber bobber) {
Item rodType = bobber.getRodType();
if (rodType != null) {
return initialStack.isOf(rodType) ? Items.FISHING_ROD.getDefaultStack() : initialStack;
}
}
return initialStack;
}
}

View file

@ -21,3 +21,5 @@ accessible field net/minecraft/datafixer/fix/AttributeIdFix
mutable field net/minecraft/datafixer/fix/AttributeIdFix UUID_TO_ID Ljava/util/Map; mutable field net/minecraft/datafixer/fix/AttributeIdFix UUID_TO_ID Ljava/util/Map;
accessible field net/minecraft/datafixer/fix/AttributeIdFix NAME_TO_ID Ljava/util/Map; accessible field net/minecraft/datafixer/fix/AttributeIdFix NAME_TO_ID Ljava/util/Map;
mutable field net/minecraft/datafixer/fix/AttributeIdFix NAME_TO_ID Ljava/util/Map; mutable field net/minecraft/datafixer/fix/AttributeIdFix NAME_TO_ID Ljava/util/Map;
accessible field net/minecraft/entity/projectile/FishingBobberEntity CAUGHT_FISH Lnet/minecraft/entity/data/TrackedData;

View file

@ -22,6 +22,7 @@
"MixinEnchantment", "MixinEnchantment",
"MixinEnchantmentHelper", "MixinEnchantmentHelper",
"MixinFallLocation", "MixinFallLocation",
"MixinFishingBobberEntity",
"MixinEntity", "MixinEntity",
"MixinEntityBucketItem", "MixinEntityBucketItem",
"MixinEntityView", "MixinEntityView",
@ -94,6 +95,7 @@
"client.MixinClientWorld", "client.MixinClientWorld",
"client.MixinClientPlayNetworkHandler", "client.MixinClientPlayNetworkHandler",
"client.MixinEntityRenderDispatcher", "client.MixinEntityRenderDispatcher",
"client.MixinFishingBobberEntityRenderer",
"client.MixinGameRenderer", "client.MixinGameRenderer",
"client.MixinHeldItemRenderer", "client.MixinHeldItemRenderer",
"client.MixinInGameHud", "client.MixinInGameHud",