Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Prepare modules for implementation of ForgeHooks and co. #121

Closed
wants to merge 7 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.Direction;

import net.patchworkmc.impl.capability.CapabilityEvents;

@ParametersAreNonnullByDefault
public abstract class CapabilityProvider<B> implements ICapabilityProvider {
protected final Class<B> baseClass;
Expand All @@ -45,14 +45,7 @@ public final void gatherCapabilities() {
}

public void gatherCapabilities(@Nullable ICapabilityProvider parent) {
AttachCapabilitiesEvent<B> event = new AttachCapabilitiesEvent<>(baseClass, (B) this);
MinecraftForge.EVENT_BUS.post(event);

if (!event.getCapabilities().isEmpty() || parent != null) {
capabilities = new CapabilityDispatcher(event.getCapabilities(), event.getListeners(), parent);
} else {
capabilities = null;
}
capabilities = CapabilityEvents.gatherCapabilities(baseClass, this, parent);
}

public final @Nullable CapabilityDispatcher getCapabilities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@

import javax.annotation.Nullable;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.CapabilityDispatcher;
import net.minecraftforge.common.capabilities.CapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.event.AttachCapabilitiesEvent;

public class BaseCapabilityProvider<T> extends CapabilityProvider<T> {
private final T provider;
Expand All @@ -37,13 +34,6 @@ public BaseCapabilityProvider(Class<T> baseClass, T provider) {

@Override
public void gatherCapabilities(@Nullable ICapabilityProvider parent) {
AttachCapabilitiesEvent<T> event = new AttachCapabilitiesEvent<>(baseClass, provider);
MinecraftForge.EVENT_BUS.post(event);

if (!event.getCapabilities().isEmpty() || parent != null) {
capabilities = new CapabilityDispatcher(event.getCapabilities(), event.getListeners(), parent);
} else {
capabilities = null;
}
capabilities = CapabilityEvents.gatherCapabilities(baseClass, provider, parent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Minecraft Forge, Patchwork Project
* Copyright (c) 2016-2020, 2019-2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package net.patchworkmc.impl.capability;

import javax.annotation.Nullable;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.CapabilityDispatcher;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.event.AttachCapabilitiesEvent;

public class CapabilityEvents {
// This is less restrictive than Forge's implementation, since patchwork can't make vanilla extend stuff at random.
@SuppressWarnings("unchecked")
@Nullable
public static <T> CapabilityDispatcher gatherCapabilities(Class<? extends T> type, T provider, @Nullable ICapabilityProvider parent) {
AttachCapabilitiesEvent<T> event = new AttachCapabilitiesEvent<T>((Class<T>) type, provider);
MinecraftForge.EVENT_BUS.post(event);

if (!event.getCapabilities().isEmpty() || parent != null) {
return new CapabilityDispatcher(event.getCapabilities(), event.getListeners(), parent);
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@

package net.patchworkmc.impl.event.entity;

import java.util.Collection;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.extensions.IForgeItem;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
Expand All @@ -43,6 +46,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.SpawnType;
import net.minecraft.entity.damage.DamageSource;
Expand Down Expand Up @@ -123,6 +127,10 @@ public static float onLivingDamage(LivingEntity entity, DamageSource src, float
return MinecraftForge.EVENT_BUS.post(event) ? 0 : event.getAmount();
}

public static boolean onLivingDrops(LivingEntity entity, DamageSource source, Collection<ItemEntity> drops, int lootingLevel, boolean recentlyHit) {
return MinecraftForge.EVENT_BUS.post(new LivingDropsEvent(entity, source, drops, lootingLevel, recentlyHit));
}

public static float getEyeHeight(Entity entity, EntityPose pose, EntityDimensions size, float defaultHeight) {
EntityEvent.EyeHeight event = new EntityEvent.EyeHeight(entity, pose, size, defaultHeight);
MinecraftForge.EVENT_BUS.post(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import java.util.ArrayList;
import java.util.Collection;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.extensions.IForgeEntity;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand Down Expand Up @@ -154,7 +152,7 @@ private void hookDropForDropsEvent(DamageSource src, CallbackInfo info) {
IForgeEntity forgeEntity = (IForgeEntity) this;
Collection<ItemEntity> drops = forgeEntity.captureDrops(null);

if (!MinecraftForge.EVENT_BUS.post(new LivingDropsEvent(entity, src, drops, dropLootingLevel.get(), playerHitTimer > 0))) {
if (!EntityEvents.onLivingDrops(entity, src, drops, dropLootingLevel.get(), playerHitTimer > 0)) {
for (ItemEntity item : drops) {
forgeEntity.getEntity().world.spawnEntity(item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Minecraft Forge, Patchwork Project
* Copyright (c) 2016-2020, 2019-2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package net.patchworkmc.api.input;

public interface ForgeMouse {
boolean isMiddleDown();
double getXVelocity();
double getYVelocity();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Minecraft Forge, Patchwork Project
* Copyright (c) 2016-2020, 2019-2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package net.patchworkmc.impl.event.input;

import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Event;

import net.minecraft.client.Mouse;

import net.patchworkmc.api.input.ForgeMouse;

public class InputEvents {
public static void fireMouseInput(int button, int action, int mods) {
MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods));
}

public static void fireKeyInput(int key, int scanCode, int action, int modifiers) {
MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scanCode, action, modifiers));
}

public static boolean onMouseScroll(Mouse mouseHelper, double scrollDelta) {
final Event event = new InputEvent.MouseScrollEvent(scrollDelta, mouseHelper.wasLeftButtonClicked(), ((ForgeMouse) mouseHelper).isMiddleDown(), mouseHelper.wasRightButtonClicked(), mouseHelper.getX(), mouseHelper.getY());

return MinecraftForge.EVENT_BUS.post(event);
}

public static boolean onRawMouseClicked(int button, int action, int mods) {
return MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package net.patchworkmc.mixin.event.input;

import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.common.MinecraftForge;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -30,15 +28,17 @@
import net.minecraft.client.Keyboard;
import net.minecraft.client.MinecraftClient;

import net.patchworkmc.impl.event.input.InputEvents;

@Mixin(Keyboard.class)
public abstract class MixinKeyboard {
@Shadow
MinecraftClient client;

@Inject(method = "onKey", at = @At("RETURN"))
private void fireKeyInput(long window, int key, int scancode, int i, int j, CallbackInfo info) {
private void fireKeyInput(long window, int key, int scancode, int action, int modifiers, CallbackInfo info) {
if (window == this.client.window.getHandle()) {
MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scancode, i, j));
InputEvents.fireKeyInput(key, scancode, action, modifiers);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@

package net.patchworkmc.mixin.event.input;

import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Event;
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.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -32,36 +28,26 @@

import net.minecraft.client.Mouse;

@Mixin(Mouse.class)
public abstract class MixinMouse {
@Shadow
boolean middleButtonClicked;
@Shadow
abstract boolean wasLeftButtonClicked();
@Shadow
abstract boolean wasRightButtonClicked();
@Shadow
abstract double getX();
@Shadow
abstract double getY();
import net.patchworkmc.api.input.ForgeMouse;
import net.patchworkmc.impl.event.input.InputEvents;

@Mixin(Mouse.class)
public abstract class MixinMouse implements ForgeMouse {
@Inject(method = "onMouseButton", at = @At("RETURN"), cancellable = true)
private void fireMouseInput(long window, int button, int action, int mods, CallbackInfo info) {
MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods));
InputEvents.fireMouseInput(button, action, mods);
}

@Inject(method = "onMouseButton", at = @At(value = "FIELD", ordinal = 3, target = "Lnet/minecraft/client/Mouse;client:Lnet/minecraft/client/MinecraftClient;", shift = Shift.BEFORE), cancellable = true)
private void onRawMouseClicked(long window, int button, int action, int mods, CallbackInfo info) {
if (MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods))) {
if (InputEvents.onRawMouseClicked(button, action, mods)) {
info.cancel();
}
}

@Inject(method = "onMouseScroll", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isSpectator()Z", shift = Shift.BEFORE), cancellable = true)
private void onMouseScroll(long window, double d, double e, CallbackInfo info, double f, float i) {
final Event event = new InputEvent.MouseScrollEvent(f, wasLeftButtonClicked(), middleButtonClicked, wasRightButtonClicked(), getX(), getY());

if (MinecraftForge.EVENT_BUS.post(event)) {
private void onMouseScroll(long window, double d, double e, CallbackInfo info, double scrollDelta, float i) {
if (InputEvents.onMouseScroll((Mouse) (Object) this, scrollDelta)) {
info.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public static void fireWorldTickEvent(TickEvent.Phase phase, World world) {
MinecraftForge.EVENT_BUS.post(event);
}

public static void onPlayerPreTick(PlayerEntity player) {
MinecraftForge.EVENT_BUS.post(new TickEvent.PlayerTickEvent(TickEvent.Phase.START, player));
public static void fireClientTickEvent(TickEvent.Phase phase) {
MinecraftForge.EVENT_BUS.post(new TickEvent.ClientTickEvent(phase));
}

public static void onPlayerPostTick(PlayerEntity player) {
MinecraftForge.EVENT_BUS.post(new TickEvent.PlayerTickEvent(TickEvent.Phase.END, player));
public static void firePlayerTickEvent(TickEvent.Phase phase, PlayerEntity player) {
MinecraftForge.EVENT_BUS.post(new TickEvent.PlayerTickEvent(phase, player));
}

public static void handleServerStarting(final MinecraftServer server) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.objectweb.asm.Opcodes;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;

import net.minecraft.client.MinecraftClient;
Expand All @@ -40,14 +39,12 @@ public class MixinMinecraftClient {
@Inject(method = "tick()V", at = @At(value = "FIELD", opcode = Opcodes.H_GETFIELD, ordinal = 0,
target = "Lnet/minecraft/client/MinecraftClient;profiler:Lnet/minecraft/util/profiler/DisableableProfiler;"))
private void hookClientTickStart(CallbackInfo info) {
TickEvent.ClientTickEvent event = new TickEvent.ClientTickEvent(TickEvent.Phase.START);
MinecraftForge.EVENT_BUS.post(event);
LifecycleEvents.fireClientTickEvent(TickEvent.Phase.START);
}

@Inject(method = "tick()V", at = @At("RETURN"))
private void hookClientTickEnd(CallbackInfo info) {
TickEvent.ClientTickEvent event = new TickEvent.ClientTickEvent(TickEvent.Phase.END);
MinecraftForge.EVENT_BUS.post(event);
LifecycleEvents.fireClientTickEvent(TickEvent.Phase.END);
}

@Inject(method = "init", at = @At("RETURN"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package net.patchworkmc.mixin.event.lifecycle;

import net.minecraftforge.event.TickEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -32,11 +33,11 @@
public class MixinPlayerEntity {
@Inject(method = "tick", at = @At("HEAD"))
public void onPlayerPreTick(CallbackInfo callback) {
LifecycleEvents.onPlayerPreTick((PlayerEntity) (Object) this);
LifecycleEvents.firePlayerTickEvent(TickEvent.Phase.START, (PlayerEntity) (Object) this);
}

@Inject(method = "tick", at = @At("TAIL"))
public void onPlayerPostTick(CallbackInfo callback) {
LifecycleEvents.onPlayerPostTick((PlayerEntity) (Object) this);
LifecycleEvents.firePlayerTickEvent(TickEvent.Phase.END, (PlayerEntity) (Object) this);
}
}
Loading