Skip to content

Commit

Permalink
Merge pull request #18 from valoeghese/kamijo_touma
Browse files Browse the repository at this point in the history
Wake event causes a ruckus!
  • Loading branch information
valoeghese authored Oct 2, 2020
2 parents 087f187 + 7f7b18b commit e4c4064
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.16.2
yarn_mappings=1.16.2+build.21
loader_version=0.9.2+build.206
# check these on https://fabricmc.net/use
yarn_mappings=1.16.3+build.17
minecraft_version=1.16.3
loader_version=0.10.0+build.208

# Mod Properties
mod_version = 1.1.0
mod_version = 1.2.0
maven_group = io.github.fabriccommunity
archives_base_name = too-many-events

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.19.0+build.398-1.16
fabric_version=0.22.1+build.409-1.16
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import io.github.fabriccommunity.events.play.PlayerInteractionEvents;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -28,4 +30,9 @@ private int modifyExperience(int original) {
return experience.get();
}
}

@Inject(at = @At("HEAD"), method = "wakeUp")
private void wakeUp(boolean bl, boolean updateSleepingPlayers, CallbackInfo info) {
PlayerInteractionEvents.WAKE_UP.invoker().onWakeUp((PlayerEntity) (Object) this, updateSleepingPlayers, bl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.entity.player.HungerManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;

/**
Expand Down Expand Up @@ -75,6 +76,15 @@ public final class PlayerInteractionEvents {
return ActionResult.PASS;
});

/**
* Event that runs when players wake up.
*/
public static final Event<WakeUp> WAKE_UP = EventFactory.createArrayBacked(WakeUp.class, listeners -> (player, updateSleepingPlayers, setSleepTimerTo0) -> {
for (WakeUp listener : listeners) {
listener.onWakeUp(player, updateSleepingPlayers, setSleepTimerTo0);
}
});

/**
* An event which runs when the player picks up experience.
* @author Valoeghese
Expand Down Expand Up @@ -158,4 +168,18 @@ public interface RestoreSaturation {
*/
ActionResult onRestoreSaturation(HungerManager hungerManager, final float original, WrappedFloat saturation);
}

/**
* An event which runs when a player wakes up.
* @author Valoeghese
*/
@FunctionalInterface
public interface WakeUp {
/**
* @param player the player waking up.
* @param updateSleepingPlayers whether to run {@link ServerWorld#updateSleepingPlayers} if server side.
* @param whether to set the sleep timer to 0 instead of 100.
*/
void onWakeUp(PlayerEntity player, boolean updateSleepingPlayers, boolean setSleepTimerTo0);
}
}
19 changes: 19 additions & 0 deletions src/test/java/io/github/fabriccommunity/events/test/WakeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.fabriccommunity.events.test;

import io.github.fabriccommunity.events.play.PlayerInteractionEvents;
import net.fabricmc.api.ModInitializer;

public class WakeTest implements ModInitializer {
public static final boolean ENABLED = true;

@Override
public void onInitialize() {
if (ENABLED) {
PlayerInteractionEvents.WAKE_UP.register((player, updateSleepingPlayers, sleepT20) -> {
if (!player.world.isClient() && player.getHungerManager().getFoodLevel() < 16) {
player.setHealth(player.getHealth() - 1);
}
});
}
}
}
3 changes: 2 additions & 1 deletion src/test/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"io.github.fabriccommunity.events.test.BlockPlaceTest",
"io.github.fabriccommunity.events.test.FoodTest",
"io.github.fabriccommunity.events.test.EntitySpawnTest",
"io.github.fabriccommunity.events.test.PlayerInteractionTest"
"io.github.fabriccommunity.events.test.PlayerInteractionTest",
"io.github.fabriccommunity.events.test.WakeTest"
],
"client": [
"io.github.fabriccommunity.events.test.client.ClientPlayerInteractionTest"
Expand Down

0 comments on commit e4c4064

Please sign in to comment.