Skip to content

Commit

Permalink
refactor: fishing is FishingHelper now; refactor: Event for cancellin…
Browse files Browse the repository at this point in the history
…g block breaking in creative;
  • Loading branch information
MJaroslav committed Feb 10, 2024
1 parent f6e9ea7 commit 26e4d54
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 327 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package io.github.mjaroslav.mjutils.asm.mixin;

import io.github.mjaroslav.mjutils.event.BlockDestroyedInCreativeEvent;
import io.github.mjaroslav.mjutils.event.player.PlayerDestroyBlockInCreativeEvent;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(ForgeHooks.class)
public abstract class MixinForgeHooks {
// Disabling block breaking for configurable items in creative mode
@Redirect(method = "onBlockBreakEvent",
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getItem()Lnet/minecraft/item/Item;",
ordinal = 0))
private static Item injected(ItemStack stack) {
return MinecraftForge.EVENT_BUS.post(new BlockDestroyedInCreativeEvent(stack)) ?
@Redirect(method = "onBlockBreakEvent", at = @At(value = "INVOKE", ordinal = 0,
target = "Lnet/minecraft/item/ItemStack;getItem()Lnet/minecraft/item/Item;"))
private static @NotNull Item overrideCreativeBlockBreakCancellingByEvent(@NotNull ItemStack stack) {
return MinecraftForge.EVENT_BUS.post(new PlayerDestroyBlockInCreativeEvent(stack)) ?
Items.diamond_sword : Items.stick;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.mjaroslav.mjutils.asm.mixin;

import io.github.mjaroslav.mjutils.event.BlockDestroyedInCreativeEvent;
import io.github.mjaroslav.mjutils.event.player.PlayerDestroyBlockInCreativeEvent;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
Expand All @@ -18,7 +18,7 @@ public abstract class MixinPlayerControllerMP {
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getItem()Lnet/minecraft/item/Item;",
ordinal = 2))
private @NotNull Item injected(@NotNull ItemStack stack) {
return MinecraftForge.EVENT_BUS.post(new BlockDestroyedInCreativeEvent(stack)) ?
return MinecraftForge.EVENT_BUS.post(new PlayerDestroyBlockInCreativeEvent(stack)) ?
Items.diamond_sword : Items.stick;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.github.mjaroslav.mjutils.asm.mixin.fishing;

import io.github.mjaroslav.mjutils.event.FishingSuccessEvent;
import io.github.mjaroslav.mjutils.event.world.FishingSuccessEvent;
import lombok.val;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityFishHook;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
Expand All @@ -14,6 +15,7 @@
import net.minecraftforge.common.MinecraftForge;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -26,50 +28,49 @@ private MixinEntityFishHook(World world) {
super(world);
}

@Shadow
public EntityPlayer field_146042_b;

@Unique
private FishingSuccessEvent mjutils$fishingSuccessEvent;
private FishingSuccessEvent mjutils$event;

// Replace drop by event
@Redirect(method = "func_146034_e",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/world/World;spawnEntityInWorld(Lnet/minecraft/entity/Entity;)Z",
ordinal = 0))
private boolean injectedEntityItem(World world, Entity entity) {
if (mjutils$fishingSuccessEvent != null && !mjutils$fishingSuccessEvent.isCanceled() && mjutils$fishingSuccessEvent.caughtItem != null)
world.spawnEntityInWorld(new EntityItem(worldObj, posX, posY, posZ, mjutils$fishingSuccessEvent.caughtItem));
return false;
@Inject(method = "func_146033_f", at = @At("HEAD"), cancellable = true)
private void stage1$initializeEvent(@NotNull CallbackInfoReturnable<ItemStack> ci) {
val chance = worldObj.rand.nextFloat();
val luck = EnchantmentHelper.func_151386_g(((EntityFishHook) (Object) this).field_146042_b);
val lure = EnchantmentHelper.func_151387_h(((EntityFishHook) (Object) this).field_146042_b);
val category = FishingHooks.getFishableCategory(chance, luck, lure);
val catchStack = FishingHooks.getRandomFishable(rand, chance, luck, lure);
mjutils$event = new FishingSuccessEvent(field_146042_b, (EntityFishHook) (Object) this, category,
catchStack, rand.nextInt(6) + 1, chance, luck, lure, true);
MinecraftForge.EVENT_BUS.post(mjutils$event);
ci.setReturnValue(new ItemStack(Blocks.air));
}

// Replacing XP by event and resetting of event
@Redirect(method = "func_146034_e",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/world/World;spawnEntityInWorld(Lnet/minecraft/entity/Entity;)Z",
ordinal = 1))
private boolean injectedEntityXPOrb(World world, Entity entity) {
if (mjutils$fishingSuccessEvent != null) {
if (!mjutils$fishingSuccessEvent.isCanceled() && mjutils$fishingSuccessEvent.caughtExperience > 0)
world.spawnEntityInWorld(new EntityXPOrb(worldObj, mjutils$fishingSuccessEvent.fishman.posX,
mjutils$fishingSuccessEvent.fishman.posY, mjutils$fishingSuccessEvent.fishman.posZ,
mjutils$fishingSuccessEvent.caughtExperience));
if (mjutils$fishingSuccessEvent.increaseStatistic && mjutils$fishingSuccessEvent.caughtCategory != null)
mjutils$fishingSuccessEvent.fishman.addStat(mjutils$fishingSuccessEvent.caughtCategory.stat, 1);
@Redirect(method = "func_146034_e", at = @At(value = "INVOKE", ordinal = 0,
target = "Lnet/minecraft/world/World;spawnEntityInWorld(Lnet/minecraft/entity/Entity;)Z"))
private boolean stage2$replaceDropByEvent(@NotNull World world, @NotNull Entity entity) {
if (mjutils$event != null && !mjutils$event.isCanceled() && mjutils$event.itemStack != null) {
val entityItem = (EntityItem) entity;
entityItem.setEntityItemStack(mjutils$event.itemStack);
world.spawnEntityInWorld(entityItem);
}
mjutils$fishingSuccessEvent = null;
return false;
}

// Add stat removing and call injected event
@Inject(method = "func_146033_f",
at = @At("HEAD"), cancellable = true)
private void func_146033_f(@NotNull CallbackInfoReturnable<ItemStack> ci) {
val chance = worldObj.rand.nextFloat();
val luck = EnchantmentHelper.func_151386_g(((EntityFishHook) (Object) this).field_146042_b);
val lure = EnchantmentHelper.func_151387_h(((EntityFishHook) (Object) this).field_146042_b);
val category = net.minecraftforge.common.FishingHooks.getFishableCategory(chance, luck, lure);
val catchStack = FishingHooks.getRandomFishable(rand, chance, luck, lure);
mjutils$fishingSuccessEvent = new FishingSuccessEvent(((EntityFishHook) (Object) this).field_146042_b,
(EntityFishHook) (Object) this, category, catchStack, rand.nextInt(6) + 1, chance, luck, lure, true);
MinecraftForge.EVENT_BUS.post(mjutils$fishingSuccessEvent);
ci.setReturnValue(new ItemStack(Blocks.air));
@Redirect(method = "func_146034_e", at = @At(value = "INVOKE", ordinal = 1,
target = "Lnet/minecraft/world/World;spawnEntityInWorld(Lnet/minecraft/entity/Entity;)Z"))
private boolean stage3$replaceXPCountByEventAddStatAndFinalizeEvent(@NotNull World world, @NotNull Entity entity) {
if (mjutils$event != null && !mjutils$event.isCanceled()) {
if (mjutils$event.experience > 0) {
val xpOrb = (EntityXPOrb) entity;
xpOrb.xpValue = mjutils$event.experience;
world.spawnEntityInWorld(xpOrb);
}
if (mjutils$event.incStat && mjutils$event.category != null)
field_146042_b.addStat(mjutils$event.category.stat, 1);
}
mjutils$event = null;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

@Mixin(FishingHooks.class)
public abstract class MixinFishingHooks {
// Empty list exception dodging
@SuppressWarnings("rawtypes")
@Redirect(method = "getRandomFishable(Ljava/util/Random;FII)Lnet/minecraft/item/ItemStack;",
at = @At(value = "INVOKE", target = "Lnet/minecraft/util/WeightedRandom;getRandomItem(Ljava/util/Random;" +
"Ljava/util/Collection;)Lnet/minecraft/util/WeightedRandom$Item;"))
private static Item injectedGetRandomFishable(Random rand, @NotNull Collection list) {
@Redirect(method = "getRandomFishable(Ljava/util/Random;FII)Lnet/minecraft/item/ItemStack;", at = @At(value =
"INVOKE", target = "Lnet/minecraft/util/WeightedRandom;getRandomItem(Ljava/util/Random;" +
"Ljava/util/Collection;)Lnet/minecraft/util/WeightedRandom$Item;"))
private static @NotNull Item fixNPEWhenListIsEmpty(@NotNull Random rand, @NotNull Collection list) {
if (list.isEmpty()) return new WeightedRandomFishable(new ItemStack(Blocks.air), 0);
else return WeightedRandom.getRandomItem(rand, list);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mjaroslav.mjutils.event;
package io.github.mjaroslav.mjutils.event.player;

import cpw.mods.fml.common.eventhandler.Cancelable;
import cpw.mods.fml.common.eventhandler.Event;
Expand All @@ -11,6 +11,6 @@
@SideOnly(Side.CLIENT)
@AllArgsConstructor
@Cancelable
public class BlockDestroyedInCreativeEvent extends Event {
public class PlayerDestroyBlockInCreativeEvent extends Event {
public final @NotNull ItemStack heldItem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.mjaroslav.mjutils.event.world;

import cpw.mods.fml.common.eventhandler.Cancelable;
import cpw.mods.fml.common.eventhandler.Event;
import lombok.AllArgsConstructor;
import net.minecraft.entity.Entity;
import net.minecraft.entity.projectile.EntityFishHook;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.FishingHooks.FishableCategory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;

@AllArgsConstructor
@Cancelable
public class FishingSuccessEvent extends Event {
public final @Nullable Entity fisher;
public final @NotNull EntityFishHook fishHook;
public @Nullable FishableCategory category;
public @Nullable ItemStack itemStack;
public int experience;
@Range(from = 0, to = 1)
public final float chance;
public final int luck;
public final int lure;
public boolean incStat;

@Override
public boolean isCanceled() {
return super.isCanceled() || category == null || itemStack == null;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.mjaroslav.mjutils.internal.client.listener;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import io.github.mjaroslav.mjutils.event.BlockDestroyedInCreativeEvent;
import io.github.mjaroslav.mjutils.event.player.PlayerDestroyBlockInCreativeEvent;
import io.github.mjaroslav.mjutils.lib.General.Creative.BlockBreaking;
import io.github.mjaroslav.mjutils.lib.General.Debug.BlockCollisionHighlighting;
import io.github.mjaroslav.mjutils.util.game.UtilsDebugRender;
Expand Down Expand Up @@ -37,7 +37,7 @@ public void onRenderWorldLastEvent(@NotNull RenderWorldLastEvent event) {
}

@SubscribeEvent
public void onBlockDestroyedInCreativeEvent(@NotNull BlockDestroyedInCreativeEvent event) {
public void onBlockDestroyedInCreativeEvent(@NotNull PlayerDestroyBlockInCreativeEvent event) {
boolean cancel = false;
val item = event.heldItem.getItem();
if (BlockBreaking.swords && item instanceof ItemSword) cancel = true;
Expand Down
Loading

0 comments on commit 26e4d54

Please sign in to comment.