Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/main/java/cam72cam/mod/block/BlockType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import cam72cam.mod.entity.Player;
import cam72cam.mod.entity.boundingbox.BoundingBox;
import cam72cam.mod.entity.boundingbox.IBoundingBox;
import cam72cam.mod.event.CommonEvents;
import cam72cam.mod.event.platform.CommonEventListener;
import cam72cam.mod.item.ItemStack;
import cam72cam.mod.math.Vec3d;
import cam72cam.mod.math.Vec3i;
import cam72cam.mod.resource.Identifier;
import cam72cam.mod.util.Facing;
import cam72cam.mod.util.SingleCache;
import cam72cam.mod.world.World;
import net.minecraft.block.Block;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
Expand All @@ -31,7 +30,7 @@ public abstract class BlockType {
Hook into the Block Broken event (not specific per block)
*/
static {
CommonEvents.Block.BROKEN.subscribe((world, pos, player) -> {
CommonEventListener.Block.BROKEN.subscribe((world, pos, player) -> {
net.minecraft.block.Block block = world.getBlockState(pos).getBlock();
if (block instanceof BlockInternal) {
return ((BlockInternal) block).tryBreak(world, pos, player);
Expand All @@ -54,7 +53,7 @@ Hook into the Block Broken event (not specific per block)
public BlockType(String modID, String name) {
this.id = new Identifier(modID, name);
internal = getBlock();
CommonEvents.Block.REGISTER.subscribe(() -> ForgeRegistries.BLOCKS.register(internal));
CommonEventListener.Block.REGISTER.subscribe(() -> ForgeRegistries.BLOCKS.register(internal));
}

/** Override to provide a custom Minecraft Block implementation (ex: support tile entities) */
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/cam72cam/mod/entity/EntityRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cam72cam.mod.ModCore;
import cam72cam.mod.event.ClientEvents;
import cam72cam.mod.event.CommonEvents;
import cam72cam.mod.event.platform.CommonEventListener;
import cam72cam.mod.resource.Identifier;
import cam72cam.mod.serialization.TagCompound;
import cam72cam.mod.text.PlayerMessage;
Expand Down Expand Up @@ -32,7 +32,7 @@ public static void register(ModCore.Mod mod, Supplier<CustomEntity> ctr, int dis
CustomEntity tmp = ctr.get();
Class<? extends CustomEntity> type = tmp.getClass();

CommonEvents.Entity.REGISTER.subscribe(() -> {
CommonEventListener.Entity.REGISTER.subscribe(() -> {
Identifier id = new Identifier(mod.modID(), type.getSimpleName());

// This has back-compat for older entity names
Expand Down Expand Up @@ -62,11 +62,11 @@ public static CustomEntity create(World world, Class<? extends Entity> cls) {


public static void registerEvents() {
CommonEvents.Entity.REGISTER.subscribe(() -> {
CommonEventListener.Entity.REGISTER.subscribe(() -> {
net.minecraftforge.fml.common.registry.EntityRegistry.registerModEntity(SeatEntity.ID, SeatEntity.class, SeatEntity.class.getSimpleName(), constructors.size()+1, ModCore.instance, 512, 20, false);
});

CommonEvents.Entity.JOIN.subscribe((world, entity) -> {
CommonEventListener.Entity.JOIN.subscribe((world, entity) -> {
if (entity instanceof ModdedEntity) {
if (World.get(world) != null) {
Pair<String, TagCompound> msg = ((ModdedEntity) entity).refusedToJoin;
Expand All @@ -92,7 +92,7 @@ public static void registerClientEvents() {
missingResources = null;
}
});
CommonEvents.World.UNLOAD.subscribe(w -> {
CommonEventListener.World.UNLOAD.subscribe(w -> {
if (w.isRemote) {
// Cleanup client side since mc does not call setDead client side...
// See ClientEvents registration for related crap
Expand Down
217 changes: 39 additions & 178 deletions src/main/java/cam72cam/mod/event/ClientEvents.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,42 @@
package cam72cam.mod.event;

import cam72cam.mod.ModCore;
import cam72cam.mod.entity.EntityRegistry;
import cam72cam.mod.entity.Player;
import cam72cam.mod.gui.helpers.GUIHelpers;
import cam72cam.mod.input.Mouse;
import cam72cam.mod.math.Vec3d;
import cam72cam.mod.render.EntityRenderer;
import cam72cam.mod.render.GlobalRender;
import cam72cam.mod.render.opengl.CustomTexture;
import cam72cam.mod.render.opengl.VBO;
import cam72cam.mod.world.World;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraftforge.client.event.*;
import net.minecraftforge.client.event.sound.SoundLoadEvent;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.relauncher.Side;
import org.jetbrains.annotations.ApiStatus;

import java.util.function.Consumer;
import java.util.function.Function;

/** Registry of events that fire off client side only. Do not use directly! */
/** Registry of events that are client side only. */
public class ClientEvents {

private static net.minecraft.world.World clientLast = null;
private static void registerClientEvents() {
EntityRegistry.registerClientEvents();
EntityRenderer.registerClientEvents();
Mouse.registerClientEvents();
GlobalRender.registerClientEvents();
World.registerClientEvnets();

VBO.registerClientEvents();
CustomTexture.registerClientEvents();

// Forge does not fire world unloaded client side
TICK.subscribe(() -> {
WorldClient mcw = Minecraft.getMinecraft().world;
if (clientLast != mcw && clientLast != null) {
CommonEvents.World.UNLOAD.execute(worldConsumer -> worldConsumer.accept(clientLast));
}
clientLast = mcw;
});
}

/** Fires off a client resource reload event (UMC only). Do not use directly */
public static void fireReload() {
RELOAD.execute(Runnable::run);
}
/** Fired at client world ticking */
public static final Event<Runnable> TICK = new Event<>();
/**
* Fired at mouse scrolling<br>
* The double inputted indicates the scrolling direction, where 1.0 is scrolling upwards<br>
* The event can be canceled by returning {@code false} in the handler
*/
public static final Event<Function<Double, Boolean>> SCROLL = new Event<>();
/**
* Fired at mouse clicking<br>
* The Player.Hand inputted indicates the clicking button, where {@link Player.Hand#PRIMARY} is main button
* and {@link Player.Hand#SECONDARY} is others<br>
* It's recommended to use with helpers in {@link Mouse}, like {@link Mouse#isLMBDown()}, to check the actual button<br>
* The event can be canceled by returning {@code false} in the handler
*/
public static final Event<Function<Player.Hand, Boolean>> CLICK = new Event<>();
/**
* Fired at mouse events in GUIs<br>
* The event can be canceled by returning {@code false} in the handler
*/
public static final Event<Function<MouseGuiEvent, Boolean>> MOUSE_GUI = new Event<>();
/** Fired at client resource reloads */
public static final Event<Runnable> RELOAD = new Event<>();

public enum MouseAction {
CLICK,
Expand All @@ -77,144 +61,21 @@ public MouseGuiEvent(MouseAction action, int x, int y, int button, double scroll
}
}

public static final Event<Runnable> TICK = new Event<>();
public static final Event<Function<Player.Hand, Boolean>> DRAG = new Event<>();
public static final Event<Function<Double, Boolean>> SCROLL = new Event<>();
public static final Event<Function<Player.Hand, Boolean>> CLICK = new Event<>();
public static final Event<Function<MouseGuiEvent, Boolean>> MOUSE_GUI = new Event<>();
public static final Event<Runnable> MODEL_CREATE = new Event<>();
public static final Event<Consumer<ModelBakeEvent>> MODEL_BAKE = new Event<>();
public static final Event<Runnable> TEXTURE_STITCH = new Event<>();
public static final Event<Runnable> REGISTER_ENTITY = new Event<>();
public static final Event<Consumer<RenderGameOverlayEvent.Text>> RENDER_DEBUG = new Event<>();
public static final Event<Consumer<RenderGameOverlayEvent.Pre>> RENDER_OVERLAY = new Event<>();
public static final Event<Consumer<Float>> RENDER_MOUSEOVER = new Event<>();
public static final Event<Consumer<SoundLoadEvent>> SOUND_LOAD = new Event<>();
public static final Event<Runnable> RELOAD = new Event<>();

@Mod.EventBusSubscriber(value = Side.CLIENT, modid = ModCore.MODID)
public static class ClientEventBus {
private static Vec3d dragPos = null;

static {
registerClientEvents();
}

@SubscribeEvent
public static void onClientTick(TickEvent.ClientTickEvent event) {
if (event.phase == TickEvent.Phase.START) {
TICK.execute(Runnable::run);
}
}

@SubscribeEvent
public static void onGuiClick(GuiScreenEvent.MouseInputEvent.Pre event) {
int x = org.lwjgl.input.Mouse.getEventX() * GUIHelpers.getScreenWidth() / Minecraft.getMinecraft().displayWidth;
int y = org.lwjgl.input.Mouse.getEventY() * GUIHelpers.getScreenHeight() / Minecraft.getMinecraft().displayHeight;
int btn = org.lwjgl.input.Mouse.getEventButton();
MouseAction action;
if (org.lwjgl.input.Mouse.getEventButtonState()) {
// click
action = MouseAction.CLICK;
} else if (btn != -1) {
// release
action = MouseAction.RELEASE;
} else {
// move
action = MouseAction.MOVE;
}

double scroll = Math.signum(org.lwjgl.input.Mouse.getEventDWheel());
if (scroll != 0) {
action = MouseAction.SCROLL;
}

MouseGuiEvent mevt = new MouseGuiEvent(action, x, GUIHelpers.getScreenHeight() - y, btn, scroll);
if (!MOUSE_GUI.executeCancellable(h -> h.apply(mevt))) {
event.setCanceled(true);
}
}

@SubscribeEvent
public static void onClick(MouseEvent event) {
int attackID = Minecraft.getMinecraft().gameSettings.keyBindAttack.getKeyCode() + 100;
int useID = Minecraft.getMinecraft().gameSettings.keyBindUseItem.getKeyCode() + 100;

if (event.getButton() == -1 && event.getDwheel() != 0) {
if (!SCROLL.executeCancellable(x -> x.apply(Math.signum((double)event.getDwheel())))) {
event.setCanceled(true);
return;
}
return;
}

if ((event.getButton() == attackID || event.getButton() == useID)) {
if (event.isButtonstate()) {
Player.Hand button = attackID == event.getButton() ? Player.Hand.SECONDARY : Player.Hand.PRIMARY;
if (!DRAG.executeCancellable(x -> x.apply(button))) {
event.setCanceled(true);
dragPos = new Vec3d(0, 0, 0);
return;
}
if (!CLICK.executeCancellable(x -> x.apply(button))) {
event.setCanceled(true);
}
} else {
dragPos = null;
}
}
}

@SubscribeEvent
public static void onFrame(TickEvent.RenderTickEvent event) {
if (dragPos != null) {
//Minecraft.getMinecraft().mouseHelper.mouseXYChange();
dragPos = dragPos.add(Minecraft.getMinecraft().mouseHelper.deltaX, Minecraft.getMinecraft().mouseHelper.deltaY, 0);
}
}

public static Vec3d getDragPos() {
return dragPos;
}

@SubscribeEvent
public static void registerModels(ModelRegistryEvent event) {
MODEL_CREATE.execute(Runnable::run);
}

@SubscribeEvent
public static void onModelBakeEvent(ModelBakeEvent event) {
MODEL_BAKE.execute(x -> x.accept(event));
}

@SubscribeEvent
public static void onTextureStitchEvent(TextureStitchEvent.Pre event) {
TEXTURE_STITCH.execute(Runnable::run);
}

@SubscribeEvent
public static void registerEntities(RegistryEvent.Register<EntityEntry> event) {
REGISTER_ENTITY.execute(Runnable::run);
}

@SubscribeEvent
public static void onDebugRender(RenderGameOverlayEvent.Text event) {
RENDER_DEBUG.execute(x -> x.accept(event));
}

@SubscribeEvent
public static void onOverlayEvent(RenderGameOverlayEvent.Pre event) {
RENDER_OVERLAY.execute(x -> x.accept(event));
}
@ApiStatus.Internal
public static void registerClientEvents() {
EntityRegistry.registerClientEvents();
EntityRenderer.registerClientEvents();
Mouse.registerClientEvents();
GlobalRender.registerClientEvents();
World.registerClientEvnets();

@SubscribeEvent
public static void onRenderMouseover(DrawBlockHighlightEvent event) {
RENDER_MOUSEOVER.execute(x -> x.accept(event.getPartialTicks()));
}
VBO.registerClientEvents();
CustomTexture.registerClientEvents();
}

@SubscribeEvent
public static void onSoundLoad(SoundLoadEvent event) {
SOUND_LOAD.execute(x -> x.accept(event));
}
/** Manually fires off a fake client resource reload event for UMC mods. */
@ApiStatus.Internal
public static void fireReload() {
RELOAD.execute(Runnable::run);
}
}
Loading