Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Move events to their correct event class as inner classes #233

Closed
wants to merge 8 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
import net.minecraft.item.ItemStack;
import nova.core.block.BlockFactory;
import nova.core.component.Category;
import nova.core.event.ItemEvent;
import nova.core.item.Item;
import nova.core.item.ItemBlock;
import nova.core.item.ItemFactory;
import nova.core.item.ItemManager;
import nova.core.item.event.ItemIDNotFoundEvent;
import nova.core.loader.Loadable;
import nova.core.nativewrapper.NativeConverter;
import nova.core.retention.Data;
Expand Down Expand Up @@ -169,10 +168,10 @@ public void preInit() {
private void registerNOVAItemsToMinecraft() {
//There should be no items registered during Native Converter preInit()
// item.registry.forEach(this::registerNOVAItem);
Game.events().on(ItemManager.ItemRegistrationEvent.class).bind(this::onItemRegistered);
Game.events().on(ItemEvent.Register.class).bind(this::onItemRegistered);
}

private void onItemRegistered(ItemManager.ItemRegistrationEvent event) {
private void onItemRegistered(ItemEvent.Register event) {
registerNOVAItem(event.itemFactory);
}

Expand Down Expand Up @@ -232,10 +231,10 @@ private void registerMinecraftItemsToNOVA() {
}

private void registerSubtypeResolution() {
Game.events().on(ItemIDNotFoundEvent.class).bind(this::onIDNotFound);
Game.events().on(ItemEvent.IDNotFound.class).bind(this::onIDNotFound);
}

private void onIDNotFound(ItemIDNotFoundEvent event) {
private void onIDNotFound(ItemEvent.IDNotFound event) {
// if item minecraft:planks:2 is detected, this code will register minecraft:planks:2 dynamically
// we cannot do this up front since there is **NO** reliable way to get the sub-items of an item

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
import net.minecraftforge.fml.common.registry.GameRegistry;
import nova.core.block.BlockFactory;
import nova.core.component.Category;
import nova.core.event.ItemEvent;
import nova.core.item.Item;
import nova.core.item.ItemBlock;
import nova.core.item.ItemFactory;
import nova.core.item.ItemManager;
import nova.core.item.event.ItemIDNotFoundEvent;
import nova.core.loader.Loadable;
import nova.core.nativewrapper.NativeConverter;
import nova.core.retention.Data;
Expand Down Expand Up @@ -171,10 +170,10 @@ public void preInit() {
private void registerNOVAItemsToMinecraft() {
//There should be no items registered during Native Converter preInit()
// item.registry.forEach(this::registerNOVAItem);
Game.events().on(ItemManager.ItemRegistrationEvent.class).bind(this::onItemRegistered);
Game.events().on(ItemEvent.Register.class).bind(this::onItemRegistered);
}

private void onItemRegistered(ItemManager.ItemRegistrationEvent event) {
private void onItemRegistered(ItemEvent.Register event) {
registerNOVAItem(event.itemFactory);
}

Expand Down Expand Up @@ -234,10 +233,10 @@ private void registerMinecraftItemsToNOVA() {
}

private void registerSubtypeResolution() {
Game.events().on(ItemIDNotFoundEvent.class).bind(this::onIDNotFound);
Game.events().on(ItemEvent.IDNotFound.class).bind(this::onIDNotFound);
}

private void onIDNotFound(ItemIDNotFoundEvent event) {
private void onIDNotFound(ItemEvent.IDNotFound event) {
// if item minecraft:planks:2 is detected, this code will register minecraft:planks:2 dynamically
// we cannot do this up front since there is **NO** reliable way to get the sub-items of an item

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/nova/core/event/BlockEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import nova.core.world.World;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

/**
* All events related to the block.
*/
public abstract class BlockEvent extends CancelableEvent {
//The world
public final World world;
Expand All @@ -37,6 +40,13 @@ public BlockEvent(World world, Vector3D position) {
this.position = position;
}

/**
* Event is triggered when a BlockFactory is registered.
*
* @see BlockFactory
* @see nova.core.block.BlockManager#register(nova.core.block.BlockFactory)
* @see nova.core.block.BlockManager#register(java.lang.String, java.util.function.Supplier)
*/
public static class Register extends CancelableEvent {
public BlockFactory blockFactory;

Expand All @@ -46,7 +56,7 @@ public Register(BlockFactory blockFactory) {
}

/**
* Called when a block in the world changes.
* Event is triggered when a block in the world changes.
*/
public static class Change extends BlockEvent {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nova/core/event/EntityEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import nova.core.event.bus.CancelableEvent;

/**
* All events related to the entity
* All events related to the entity.
* @author Calclavia
*/
public class EntityEvent extends CancelableEvent {
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/nova/core/event/ItemEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2017 NOVA, All rights reserved.
* This library is free software, licensed under GNU Lesser General Public License version 3
*
* This file is part of NOVA.
*
* NOVA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NOVA 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.event;

import nova.core.event.bus.CancelableEvent;
import nova.core.event.bus.Event;
import nova.core.item.ItemFactory;

/**
* All events related to the item.
* @author ExE Boss
*/
public abstract class ItemEvent extends CancelableEvent {

/**
* Event is triggered when an ItemFactory is registered.
*
* @see ItemFactory
* @see nova.core.item.ItemManager#register(nova.core.item.ItemFactory)
* @see nova.core.item.ItemManager#register(java.lang.String, java.util.function.Supplier)
*/
public static class Register extends CancelableEvent {
public ItemFactory itemFactory;

public Register(ItemFactory itemFactory) {
this.itemFactory = itemFactory;
}
}

/**
* Event is triggered when an Item ID is not found.
*
* @author Stan
*/
public static class IDNotFound extends Event {
public final String id;

private ItemFactory remappedFactory = null;

public IDNotFound(String id) {
this.id = id;
}

public ItemFactory getRemappedFactory() {
return remappedFactory;
}

public void setRemappedFactory(ItemFactory remappedFactory) {
this.remappedFactory = remappedFactory;
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/nova/core/event/ServerEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
*/
public abstract class ServerEvent extends Event {
/**
* Called when the server starts running
* Event is triggered when the server starts running.
*/
public static class Start extends ServerEvent {

}

/**
* Called when the server stops running.
* Event is triggered when the server stops running.
*/
public static class Stop extends ServerEvent {

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/nova/core/event/WorldEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import nova.core.event.bus.Event;
import nova.core.world.World;

/**
* All events related to the world.
*/
public abstract class WorldEvent extends Event {
public final World world;

Expand All @@ -31,7 +34,7 @@ public WorldEvent(World world) {
}

/**
* Called when a world loads.
* Event is triggered when a world loads.
*/
public static class Load extends WorldEvent {
public Load(World world) {
Expand All @@ -40,7 +43,7 @@ public Load(World world) {
}

/**
* Called when a world unloads.
* Event is triggered when a world unloads.
*/
public static class Unload extends WorldEvent {
public Unload(World world) {
Expand Down
21 changes: 6 additions & 15 deletions src/main/java/nova/core/item/ItemManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

import nova.core.block.BlockFactory;
import nova.core.block.BlockManager;
import nova.core.event.bus.CancelableEvent;
import nova.core.item.event.ItemIDNotFoundEvent;
import nova.core.event.ItemEvent;
import nova.core.util.registry.FactoryManager;
import nova.core.util.registry.Registry;
import nova.internal.core.Game;
Expand Down Expand Up @@ -52,9 +51,10 @@ public ItemFactory register(String id, Supplier<Item> constructor) {

@Override
public ItemFactory register(ItemFactory factory) {
registry.register(factory);
Game.events().publish(new ItemRegistrationEvent(factory));
return factory;
ItemEvent.Register event = new ItemEvent.Register(factory);
Game.events().publish(event);
registry.register(event.itemFactory);
return event.itemFactory;
}

public ItemFactory getItemFromBlock(BlockFactory block) {
Expand All @@ -68,7 +68,7 @@ public Optional<BlockFactory> getBlockFromItem(Item item) {
@Override
public Optional<ItemFactory> get(String name) {
if (!registry.contains(name)) {
ItemIDNotFoundEvent event = new ItemIDNotFoundEvent(name);
ItemEvent.IDNotFound event = new ItemEvent.IDNotFound(name);
Game.events().publish(event);

if (event.getRemappedFactory() != null) {
Expand All @@ -79,15 +79,6 @@ public Optional<ItemFactory> get(String name) {
return registry.get(name);
}

//TODO: Move to item event
public class ItemRegistrationEvent extends CancelableEvent {
public final ItemFactory itemFactory;

public ItemRegistrationEvent(ItemFactory itemFactory) {
this.itemFactory = itemFactory;
}
}

@Override
public void init() {
Game.events().publish(new Init(this));
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/nova/core/item/event/ItemIDNotFoundEvent.java

This file was deleted.