aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/anthonyhilyard/iceberg/mixin/EntityMixin.java
blob: ba3a69e72c467207f3aeffb8e5973775dd2f9876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.anthonyhilyard.iceberg.mixin;

import java.util.Objects;

import com.anthonyhilyard.iceberg.events.EntityFluidEvent;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
import net.minecraftforge.common.MinecraftForge;

@Mixin(Entity.class)
public class EntityMixin extends net.minecraftforge.common.capabilities.CapabilityProvider<Entity>
{
	private Fluid previousFluidOnEyes = null;

	@Shadow
	protected Tag<Fluid> fluidOnEyes;

	protected EntityMixin(Class<Entity> baseClass) { super(baseClass); }

	@Inject(method = "updateFluidOnEyes", at = @At(value = "RETURN"))
	public void onUpdateFluidOnEyes(CallbackInfo callbackInfo)
	{
		if (fluidOnEyes != null && fluidOnEyes.getValues().size() > 0)
		{
			previousFluidOnEyes = fluidOnEyes.getValues().get(0);
		}
		else if (previousFluidOnEyes != null)
		{
			// We were submerged in a fluid that we no longer are.
			if (previousFluidOnEyes != null)
			{
				MinecraftForge.EVENT_BUS.post(new EntityFluidEvent.Exited((Entity)(Object)this, previousFluidOnEyes));
			}
			previousFluidOnEyes = null;
		}
	}

	@Inject(method = "updateFluidOnEyes",
			at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/Entity;fluidOnEyes:Lnet/minecraft/tags/Tag;", ordinal = 1, shift = Shift.AFTER))
	public void onUpdateFluidOnEyeAssign(CallbackInfo callbackInfo)
	{
		Fluid currentFluid = null;
		if (fluidOnEyes != null && fluidOnEyes.getValues().size() > 0)
		{
			currentFluid = fluidOnEyes.getValues().get(0);
		}

		if (!Objects.equals(previousFluidOnEyes, currentFluid))
		{
			// We are now submerged in a fluid that doesn't match the previous one.
			if (currentFluid != null)
			{
				MinecraftForge.EVENT_BUS.post(new EntityFluidEvent.Entered((Entity)(Object)this, currentFluid));
			}
		}
	}
}