More events and hooks

This commit is contained in:
shedaniel
2020-11-07 14:51:39 +08:00
parent d50135b5d9
commit 1d7e7adff1
22 changed files with 503 additions and 55 deletions

View File

@@ -33,7 +33,7 @@ public final class ArchitecturyPopulator {
if (Modifier.isStatic(field.getModifiers())) {
FieldUtils.removeFinalModifier(field);
field.setAccessible(true);
String type = field.getType().toString().replace("$", "");
String type = field.getType().getName().replace("$", "");
Class<?> newClass = Class.forName(type.substring(0, type.lastIndexOf('.')) + "." + Architectury.getModLoader() + "." + type.substring(type.lastIndexOf('.') + 1));
field.set(null, newClass.getConstructor().newInstance());
}
@@ -44,7 +44,7 @@ public final class ArchitecturyPopulator {
if (!Modifier.isStatic(field.getModifiers())) {
FieldUtils.removeFinalModifier(field);
field.setAccessible(true);
String type = field.getType().toString().replace("$", "");
String type = field.getType().getName().replace("$", "");
Class<?> newClass = Class.forName(type.substring(0, type.lastIndexOf('.')) + "." + Architectury.getModLoader() + "." + type.substring(type.lastIndexOf('.') + 1));
field.set(o, newClass.getConstructor().newInstance());
}

View File

@@ -17,11 +17,6 @@
package me.shedaniel.architectury.event;
import com.google.common.reflect.AbstractInvocationHandler;
import me.shedaniel.architectury.ArchitecturyPopulator;
import me.shedaniel.architectury.Populatable;
import me.shedaniel.architectury.platform.Platform;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.jodah.typetools.TypeResolver;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
@@ -38,9 +33,6 @@ import java.util.function.Function;
public final class EventFactory {
private EventFactory() {}
@Populatable
private static final Impl IMPL = null;
public static <T> Event<T> create(Function<T[], T> function) {
Class<?>[] arguments = TypeResolver.resolveRawArguments(Function.class, function.getClass());
return new EventImpl<>(arguments[1], function);
@@ -177,23 +169,4 @@ public final class EventFactory {
}
}
}
public interface Impl {
@Environment(EnvType.CLIENT)
void registerClient();
void registerCommon();
@Environment(EnvType.SERVER)
void registerServer();
}
static {
ArchitecturyPopulator.populate(EventFactory.class);
if (Platform.getEnv() == EnvType.CLIENT)
IMPL.registerClient();
IMPL.registerCommon();
if (Platform.getEnv() == EnvType.SERVER)
IMPL.registerServer();
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2020 shedaniel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.shedaniel.architectury.event;
import me.shedaniel.architectury.ArchitecturyPopulator;
import me.shedaniel.architectury.Populatable;
import me.shedaniel.architectury.platform.Platform;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
public final class EventHandler {
private EventHandler() {}
@Populatable
private static final Impl IMPL = null;
private static boolean initialized = false;
public static void init() {
if (initialized) return;
initialized = true;
if (Platform.getEnv() == EnvType.CLIENT)
IMPL.registerClient();
IMPL.registerCommon();
if (Platform.getEnv() == EnvType.SERVER)
IMPL.registerServer();
}
public interface Impl {
@Environment(EnvType.CLIENT)
void registerClient();
void registerCommon();
@Environment(EnvType.SERVER)
void registerServer();
}
static {
ArchitecturyPopulator.populate(EventHandler.class);
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2020 shedaniel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.shedaniel.architectury.event.events;
import me.shedaniel.architectury.event.Event;
import me.shedaniel.architectury.event.EventFactory;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import java.util.List;
public interface ExplosionEvent {
Event<Pre> PRE = EventFactory.createInteractionResult(Pre.class);
Event<Detonate> DETONATE = EventFactory.createInteractionResult(Detonate.class);
interface Pre {
InteractionResult explode(Level world, Explosion explosion);
}
interface Detonate {
void explode(Level world, Explosion explosion, List<Entity> affectedEntities);
}
}

View File

@@ -44,6 +44,8 @@ public interface GuiEvent {
* Invoked after Screen#init, equivalent to forge's {@code GuiScreenEvent.InitGuiEvent.Post}.
*/
Event<ScreenInitPost> INIT_POST = EventFactory.createLoop(ScreenInitPost.class);
Event<ScreenRenderPre> RENDER_PRE = EventFactory.createInteractionResult(ScreenRenderPre.class);
Event<ScreenRenderPost> RENDER_POST = EventFactory.createInteractionResult(ScreenRenderPost.class);
@Environment(EnvType.CLIENT)
interface RenderHud {
@@ -64,4 +66,14 @@ public interface GuiEvent {
interface ScreenInitPost {
void init(Screen screen, List<AbstractWidget> widgets, List<GuiEventListener> children);
}
@Environment(EnvType.CLIENT)
interface ScreenRenderPre {
InteractionResult render(Screen screen, PoseStack matrices, int mouseX, int mouseY, float delta);
}
@Environment(EnvType.CLIENT)
interface ScreenRenderPost {
void render(Screen screen, PoseStack matrices, int mouseX, int mouseY, float delta);
}
}

View File

@@ -53,9 +53,13 @@ public interface LifecycleEvent {
*/
Event<ServerState> SERVER_STOPPED = EventFactory.createLoop(ServerState.class);
/**
* Invoked after a world is loaded only on server, equivalent to forge's {@code WorldEvent.Load}.
* Invoked after a world is loaded only on server, equivalent to forge's {@code WorldEvent.Load} and fabric's {@code ServerWorldEvents#LOAD}.
*/
Event<ServerWorldState> SERVER_WORLD_LOAD = EventFactory.createLoop(ServerWorldState.class);
/**
* Invoked after a world is unloaded, equivalent to forge's {@code WorldEvent.Unload} and fabric's {@code ServerWorldEvents#UNLOAD}.
*/
Event<ServerWorldState> SERVER_WORLD_UNLOAD = EventFactory.createLoop(ServerWorldState.class);
/**
* Invoked during a world is saved, equivalent to forge's {@code WorldEvent.Save}.
*/

View File

@@ -20,6 +20,7 @@ import me.shedaniel.architectury.event.Event;
import me.shedaniel.architectury.event.EventFactory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.advancements.Advancement;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.server.level.ServerPlayer;
@@ -30,6 +31,8 @@ public interface PlayerEvent {
@Environment(EnvType.CLIENT) Event<ClientPlayerJoin> CLIENT_PLAYER_JOIN = EventFactory.createLoop(ClientPlayerJoin.class);
@Environment(EnvType.CLIENT) Event<ClientPlayerQuit> CLIENT_PLAYER_QUIT = EventFactory.createLoop(ClientPlayerQuit.class);
@Environment(EnvType.CLIENT) Event<ClientPlayerRespawn> CLIENT_PLAYER_RESPAWN = EventFactory.createLoop(ClientPlayerRespawn.class);
Event<PlayerAdvancement> PLAYER_ADVANCEMENT = EventFactory.createLoop(PlayerAdvancement.class);
Event<PlayerClone> PLAYER_CLONE = EventFactory.createLoop(PlayerClone.class);
interface PlayerJoin {
void join(ServerPlayer player);
@@ -43,6 +46,14 @@ public interface PlayerEvent {
void respawn(ServerPlayer newPlayer, boolean conqueredEnd);
}
interface PlayerClone {
void clone(ServerPlayer oldPlayer, ServerPlayer newPlayer, boolean wonGame);
}
interface PlayerAdvancement {
void award(ServerPlayer player, Advancement advancement);
}
@Environment(EnvType.CLIENT)
interface ClientPlayerJoin {
void join(LocalPlayer player);

View File

@@ -16,12 +16,16 @@
package me.shedaniel.architectury.registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
public interface Registry<T> {
public interface Registry<T> extends Iterable<T> {
Supplier<T> delegate(ResourceLocation id);
Supplier<T> register(ResourceLocation id, Supplier<T> supplier);
@@ -29,6 +33,18 @@ public interface Registry<T> {
@Nullable
ResourceLocation getId(T obj);
Optional<ResourceKey<T>> getKey(T obj);
@Nullable
T get(ResourceLocation id);
boolean contains(ResourceLocation id);
boolean containsValue(T obj);
Set<ResourceLocation> getIds();
Set<Map.Entry<ResourceKey<T>, T>> entrySet();
ResourceKey<? extends net.minecraft.core.Registry<T>> key();
}