Skip to content

Commit

Permalink
Lifecycle Events v1 (#38)
Browse files Browse the repository at this point in the history
* 1.0.0 lifecycle events start

* update existing events to v1

* lifecycle v1 good

* fix

* fix my workspaces and update config api to use halfmaven

* shut up compileTestJava
  • Loading branch information
valoeghese authored May 8, 2021
1 parent 81a0a05 commit 1c213d9
Show file tree
Hide file tree
Showing 21 changed files with 479 additions and 40 deletions.
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ allprojects {
name = 'Jitpack'
url 'https://jitpack.io/'
}
maven {
name = 'HalfOf2'
url = 'https://storage.googleapis.com/devan-maven/'
}
}

configurations {
Expand All @@ -94,11 +98,9 @@ allprojects {
//to change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"

mappings loom.fromCommit('minecraft-cursed-legacy/Plasma', "build.${project.plasma_build}") {spec ->
spec.version = "b1.7.3-${project.plasma_build}"
}
mappings "io.github.minecraft-cursed-legacy:plasma:b1.7.3-build.${project.plasma_build}"

modImplementation("com.github.minecraft-cursed-legacy:cursed-fabric-loader:${project.loader_version}") {
modImplementation("io.github.minecraft-cursed-legacy:cursed-fabric-loader:${project.loader_version}") {
transitive false //Avoid leaking Loader's dependencies forwards
}

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
minecraft_version=b1.7.3
loader_version=5ce86c8
loader_version=1.0.0
plasma_build=18

# Mod Properties
mod_version = 1.0.6
mod_version = 1.1.0
maven_group = io.github.minecraftcursedlegacy
archives_base_name = cursed-legacy-api
2 changes: 1 addition & 1 deletion legacy-config-v0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'java-library'

moduleDependencies(project, 'legacy-api-base')
dependencies {
include api('com.github.valoeghese:ZoesteriaConfig:1.3.6')
include api('valoeghese:ZoesteriaConfig:1.3.6')
}

minecraft {
Expand Down
6 changes: 0 additions & 6 deletions legacy-lifecycle-events-v0/build.gradle

This file was deleted.

6 changes: 6 additions & 0 deletions legacy-lifecycle-events-v1/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
archivesBaseName = 'legacy-lifecycle-events-v1'
version = getSubprojectVersion(project, '1.0.0')

moduleDependencies(project, 'legacy-api-base')
dependencies {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2020 The Cursed Legacy Team.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.minecraftcursedlegacy.api.event.lifecycle;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.Minecraft;

/**
* Lifecycle events for the client.
* @since 1.1.0
*/
@Environment(EnvType.CLIENT)
public class ClientLifecycleEvents {
/**
* Event for the start of the client tick.
*/
public static final Event<Tick> START_TICK = EventFactory.createArrayBacked(Tick.class,
listeners -> client -> {
for (Tick listener : listeners) {
listener.onClientTick(client);
}
});

/**
* Event for the end of the client tick.
*/
public static final Event<Tick> END_TICK = EventFactory.createArrayBacked(Tick.class,
listeners -> client -> {
for (Tick listener : listeners) {
listener.onClientTick(client);
}
});

@FunctionalInterface
public interface Tick {
/**
* Called when the client ticks.
* @param client the minecraft client instance.
*/
void onClientTick(Minecraft client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@
package io.github.minecraftcursedlegacy.api.event.lifecycle;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.Minecraft;

/**
* Callback for ticks on the client.
* @deprecated use {@linkplain ClientLifecycleEvents#END_TICK} instead.
*/
@FunctionalInterface
public interface ClientTickCallback {
Event<ClientTickCallback> EVENT = EventFactory.createArrayBacked(ClientTickCallback.class,
listeners -> client -> {
for (ClientTickCallback listener : listeners) {
listener.onClientTick(client);
}
});
@Deprecated
public interface ClientTickCallback extends ClientLifecycleEvents.Tick {
Event<ClientLifecycleEvents.Tick> EVENT = ClientLifecycleEvents.END_TICK;

/**
* Called when the client ticks.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2020 The Cursed Legacy Team.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.minecraftcursedlegacy.api.event.lifecycle;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.entity.player.Player;

/**
* Collection of common events that pertain to the game lifecycle.
* @since 1.1.0
*/
public class CommonLifecycleEvents {
/**
* Event for the player respawning.
*/
public static final Event<PlayerRespawn> PLAYER_RESPAWN = EventFactory.createArrayBacked(PlayerRespawn.class,
listeners -> player -> {
for (PlayerRespawn listener : listeners) {
listener.onRespawn(player);
}
});

@FunctionalInterface
public interface PlayerRespawn {
/**
* Called after the player respawns.
* @param player the player that has respawned.
*/
void onRespawn(Player player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@
package io.github.minecraftcursedlegacy.api.event.lifecycle;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.MinecraftServer;

/**
* Callback for ticks on the dedicated server. Does *not* run in singleplayer!
* @deprecated use {@linkplain ServerLifecycleEvents#END_TICK} instead.
*/
@FunctionalInterface
public interface DedicatedServerTickCallback {
Event<DedicatedServerTickCallback> EVENT = EventFactory.createArrayBacked(DedicatedServerTickCallback.class,
listeners -> server -> {
for (DedicatedServerTickCallback listener : listeners) {
listener.onServerTick(server);
}
});
@Deprecated
public interface DedicatedServerTickCallback extends ServerLifecycleEvents.Tick {
Event<ServerLifecycleEvents.Tick> EVENT = ServerLifecycleEvents.END_TICK;

/**
* Called when the dedicated server ticks.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2020 The Cursed Legacy Team.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.minecraftcursedlegacy.api.event.lifecycle;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginPacketHandler;
import net.minecraft.server.player.ServerPlayer;

/**
* Lifecycle events for the server.
* @since 1.1.0
* @apiNote Remember: None of these events run in singleplayer!
*/
@Environment(EnvType.SERVER)
public class ServerLifecycleEvents {
/**
* Event for the start of the dedicated server tick.
*/
public static final Event<Tick> START_TICK = EventFactory.createArrayBacked(Tick.class,
listeners -> server -> {
for (Tick listener : listeners) {
listener.onServerTick(server);
}
});

/**
* Event for the end of the dedicated server tick.
*/
public static final Event<Tick> END_TICK = EventFactory.createArrayBacked(Tick.class,
listeners -> server -> {
for (Tick listener : listeners) {
listener.onServerTick(server);
}
});

/**
* Event for a player logging in to the server.
*/
public static final Event<PlayerLogin> PLAYER_LOGIN = EventFactory.createArrayBacked(PlayerLogin.class,
listeners -> (player, packetHandler) -> {
for (PlayerLogin listener : listeners) {
listener.onPlayerLogin(player, packetHandler);

// In case a mod's listener has disconnected the player for some reason
// This is the equivalent of cancelling the login, but with vanilla functionality handling it.
if (packetHandler.closed) {
break;
}
}
});

/**
* Event for a player being disconnected / disconnecting from the server
*/
public static final Event<PlayerDisconnect> PLAYER_DISCONNECT = EventFactory.createArrayBacked(PlayerDisconnect.class,
listeners -> player -> {
for (PlayerDisconnect listener : listeners) {
listener.onPlayerDisconnect(player);
}
});

@FunctionalInterface
public interface Tick {
/**
* Called when the dedicated server ticks.
* @param server the dedicated server instance.
*/
void onServerTick(MinecraftServer server);
}

@FunctionalInterface
public interface PlayerLogin {
/**
* Called when a player successfully logs in to the server.
* @param player the player that has just logged in.
* @param packetHandler the login packetHandler.
* @apiNote {@linkplain ServerLoginPacketHandler#drop(String)} can be used to prevent the player connecting.
*/
void onPlayerLogin(ServerPlayer player, ServerLoginPacketHandler packetHandler);
}

@FunctionalInterface
public interface PlayerDisconnect {
/**
* Called when a player disconnects from the server.
* @param player the player that has disconnected.
*/
void onPlayerDisconnect(ServerPlayer player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,33 @@
package io.github.minecraftcursedlegacy.mixin.event.lifecycle;

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.callback.CallbackInfo;

import io.github.minecraftcursedlegacy.api.event.lifecycle.ClientTickCallback;
import io.github.minecraftcursedlegacy.api.event.lifecycle.ClientLifecycleEvents;
import io.github.minecraftcursedlegacy.api.event.lifecycle.CommonLifecycleEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.AbstractClientPlayer;

@Mixin(Minecraft.class)
public class MixinMinecraft {
@Shadow
private AbstractClientPlayer player;

@Inject(at = @At("HEAD"), method = "tick")
private void onStartTick(CallbackInfo info) {
ClientLifecycleEvents.START_TICK.invoker().onClientTick((Minecraft) (Object) this);
}

@Inject(at = @At("RETURN"), method = "tick")
private void onTick(CallbackInfo info) {
ClientTickCallback.EVENT.invoker().onClientTick((Minecraft) (Object) this);
private void onEndTick(CallbackInfo info) {
ClientLifecycleEvents.END_TICK.invoker().onClientTick((Minecraft) (Object) this);
}

@Inject(at = @At("RETURN"), method = "respawn")
private void onRespawn(boolean flag, int i, CallbackInfo info) {
CommonLifecycleEvents.PLAYER_RESPAWN.invoker().onRespawn(this.player);
}
}
Loading

0 comments on commit 1c213d9

Please sign in to comment.