Skip to content

Commit

Permalink
Create RecipeEvent and move recipe events there
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Jan 17, 2017
1 parent 8ec2f06 commit 48d09c9
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import nova.core.recipes.RecipeAddedEvent;
import nova.core.event.RecipeEvent;
import nova.core.recipes.RecipeManager;
import nova.core.recipes.RecipeRemovedEvent;
import nova.core.recipes.crafting.CraftingRecipe;
import nova.core.wrapper.mc.forge.v17.util.ReflectionUtil;
import nova.internal.core.Game;
Expand Down Expand Up @@ -58,7 +57,7 @@ public void registerRecipes() {

RecipeManager recipeManager = Game.recipes();

List<IRecipe> recipes = (List<IRecipe>) CraftingManager.getInstance().getRecipeList();
List<IRecipe> recipes = CraftingManager.getInstance().getRecipeList();
for (IRecipe recipe : recipes) {
CraftingRecipe converted = convert(recipe);
if (converted != null) {
Expand All @@ -84,8 +83,8 @@ private IRecipe convert(CraftingRecipe recipe) {
return RecipeConverter.toMinecraft(recipe);
}

private void onNOVARecipeAdded(RecipeAddedEvent<CraftingRecipe> e) {
CraftingRecipe recipe = e.getRecipe();
private void onNOVARecipeAdded(RecipeEvent.Added<CraftingRecipe> evt) {
CraftingRecipe recipe = evt.recipe;
if (forwardWrappers.containsKey(recipe)) {
return;
}
Expand All @@ -98,10 +97,10 @@ private void onNOVARecipeAdded(RecipeAddedEvent<CraftingRecipe> e) {
CraftingManager.getInstance().getRecipeList().add(minecraftRecipe);
}

private void onNOVARecipeRemoved(RecipeRemovedEvent<CraftingRecipe> e) {
IRecipe minecraftRecipe = forwardWrappers.get(e.getRecipe());
private void onNOVARecipeRemoved(RecipeEvent.Removed<CraftingRecipe> evt) {
IRecipe minecraftRecipe = forwardWrappers.get(evt.recipe);

forwardWrappers.remove(e.getRecipe());
forwardWrappers.remove(evt.recipe);
backwardWrappers.remove(minecraftRecipe);

CraftingManager.getInstance().getRecipeList().remove(minecraftRecipe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import nova.core.recipes.RecipeAddedEvent;
import nova.core.event.RecipeEvent;
import nova.core.recipes.RecipeManager;
import nova.core.recipes.RecipeRemovedEvent;
import nova.core.recipes.crafting.CraftingRecipe;
import nova.core.wrapper.mc.forge.v18.util.ReflectionUtil;
import nova.internal.core.Game;
Expand Down Expand Up @@ -58,7 +57,7 @@ public void registerRecipes() {

RecipeManager recipeManager = Game.recipes();

List<IRecipe> recipes = (List<IRecipe>) CraftingManager.getInstance().getRecipeList();
List<IRecipe> recipes = CraftingManager.getInstance().getRecipeList();
for (IRecipe recipe : recipes) {
CraftingRecipe converted = convert(recipe);
if (converted != null) {
Expand All @@ -84,8 +83,8 @@ private IRecipe convert(CraftingRecipe recipe) {
return RecipeConverter.toMinecraft(recipe);
}

private void onNOVARecipeAdded(RecipeAddedEvent<CraftingRecipe> e) {
CraftingRecipe recipe = e.getRecipe();
private void onNOVARecipeAdded(RecipeEvent.Added<CraftingRecipe> evt) {
CraftingRecipe recipe = evt.recipe;
if (forwardWrappers.containsKey(recipe)) {
return;
}
Expand All @@ -98,10 +97,10 @@ private void onNOVARecipeAdded(RecipeAddedEvent<CraftingRecipe> e) {
CraftingManager.getInstance().getRecipeList().add(minecraftRecipe);
}

private void onNOVARecipeRemoved(RecipeRemovedEvent<CraftingRecipe> e) {
IRecipe minecraftRecipe = forwardWrappers.get(e.getRecipe());
private void onNOVARecipeRemoved(RecipeEvent.Removed<CraftingRecipe> evt) {
IRecipe minecraftRecipe = forwardWrappers.get(evt.recipe);

forwardWrappers.remove(e.getRecipe());
forwardWrappers.remove(evt.recipe);
backwardWrappers.remove(minecraftRecipe);

CraftingManager.getInstance().getRecipeList().remove(minecraftRecipe);
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/nova/core/event/RecipeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.recipes.Recipe;

/**
* All events related to the recipe.
*
* @param <T> recipe type
* @author ExE Boss
*/
public abstract class RecipeEvent<T extends Recipe> extends CancelableEvent {
public final T recipe;

public RecipeEvent(T recipe) {
this.recipe = recipe;
}

/**
* A recipe added event is fired when a recipe of the right type has been added
* to the RecipeManager.
*
* @param <T> recipe type
* @author Stan Hebben
*/
public static class Added<T extends Recipe> extends RecipeEvent<T>{
public Added(T recipe) {
super(recipe);
}
}

/**
* A recipe removed event is fired when a recipe of the right type has been
* removed from the RecipeManager.
*
* @param <T> recipe type
* @author Stan Hebben
*/
public static class Removed<T extends Recipe> extends RecipeEvent<T> {
public Removed(T recipe) {
super(recipe);
}
}
}
40 changes: 0 additions & 40 deletions src/main/java/nova/core/recipes/RecipeAddedEvent.java

This file was deleted.

48 changes: 33 additions & 15 deletions src/main/java/nova/core/recipes/RecipeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package nova.core.recipes;

import nova.core.event.RecipeEvent;
import nova.core.event.bus.EventBus;
import nova.core.event.bus.EventListener;
import nova.core.event.bus.EventListenerHandle;
Expand All @@ -30,8 +31,11 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Spliterator;
import java.util.stream.Stream;

/**
* The RecipeManager manages all recipes (of any type) in the game.
Expand Down Expand Up @@ -67,16 +71,14 @@ public <T extends Recipe> Collection<T> getRecipes(Class<T> type) {
return getRecipeList(type).unmodifiableRecipes;
}

public <T extends Recipe> EventListenerHandle<RecipeAddedEvent<T>> whenRecipeAdded(
Class<T> type,
EventListener<RecipeAddedEvent<T>> listener) {
return getRecipeList(type).recipeAddedListeners.on().bind(listener);
public <T extends Recipe> EventListenerHandle<RecipeEvent.Added<T>> whenRecipeAdded(
Class<T> type, EventListener<RecipeEvent.Added<T>> listener) {
return getRecipeList(type).events.on(RecipeEvent.Added.class).bind(listener);
}

public <T extends Recipe> EventListenerHandle<RecipeRemovedEvent<T>> whenRecipeRemoved(
Class<T> type,
EventListener<RecipeRemovedEvent<T>> listener) {
return getRecipeList(type).recipeRemovedListeners.on().bind(listener);
public <T extends Recipe> EventListenerHandle<RecipeEvent.Removed<T>> whenRecipeRemoved(
Class<T> type, EventListener<RecipeEvent.Removed<T>> listener) {
return getRecipeList(type).events.on(RecipeEvent.Removed.class).bind(listener);
}

// #######################
Expand All @@ -103,30 +105,46 @@ private <T extends Recipe> RecipeList<T> collectRecipes(Class<T> type) {
return new RecipeList<>(result);
}

private class RecipeList<T extends Recipe> {
private class RecipeList<T extends Recipe> implements Iterable<T> {
private Set<T> recipes;
private Set<T> unmodifiableRecipes;
private EventBus<RecipeAddedEvent<T>> recipeAddedListeners;
private EventBus<RecipeRemovedEvent<T>> recipeRemovedListeners;
private EventBus<RecipeEvent<T>> events;

private RecipeList(Set<T> recipes) {
this.recipes = recipes;
this.unmodifiableRecipes = Collections.unmodifiableSet(recipes);
this.recipeAddedListeners = new EventBus<>();
this.recipeRemovedListeners = new EventBus<>();
this.events = new EventBus<>();
}

private void add(T recipe) {
if (recipes.add(recipe)) {
recipeAddedListeners.publish(new RecipeAddedEvent<>(recipe));
events.publish(new RecipeEvent.Added<>(recipe));
}
}

private void remove(T recipe) {
if (recipes.remove(recipe)) {
recipeRemovedListeners.publish(new RecipeRemovedEvent<>(recipe));
events.publish(new RecipeEvent.Removed<>(recipe));
}
}

@Override
public Iterator<T> iterator() {
return this.unmodifiableRecipes.iterator();
}

@Override
public Spliterator<T> spliterator() {
return this.unmodifiableRecipes.spliterator();
}

public Stream<T> stream() {
return this.unmodifiableRecipes.stream();
}

public Stream<T> parallelStream() {
return this.unmodifiableRecipes.parallelStream();
}
}

@Override
Expand Down
40 changes: 0 additions & 40 deletions src/main/java/nova/core/recipes/RecipeRemovedEvent.java

This file was deleted.

23 changes: 12 additions & 11 deletions src/main/java/nova/core/recipes/crafting/CraftingRecipeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
*
* 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.recipes.crafting;
*/

package nova.core.recipes.crafting;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import nova.core.event.RecipeEvent;
import nova.core.item.Item;
import nova.core.recipes.RecipeAddedEvent;
import nova.core.recipes.RecipeManager;
import nova.core.recipes.RecipeRemovedEvent;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -104,25 +105,25 @@ public Optional<CraftingRecipe> getRecipe(CraftingGrid grid) {
// ### Private Methods ###
// #######################

private <T extends CraftingRecipe> void onCraftingRecipeAdded(RecipeAddedEvent<T> e) {
Optional<Collection<String>> possibleFirstItemIds = e.getRecipe().getPossibleItemsInFirstSlot();
private <T extends CraftingRecipe> void onCraftingRecipeAdded(RecipeEvent.Added<T> evt) {
Optional<Collection<String>> possibleFirstItemIds = evt.recipe.getPossibleItemsInFirstSlot();
if (possibleFirstItemIds.isPresent()) {
for (String itemId : possibleFirstItemIds.get()) {
staticRecipes.put(itemId, e.getRecipe());
staticRecipes.put(itemId, evt.recipe);
}
} else {
dynamicRecipes.add(e.getRecipe());
dynamicRecipes.add(evt.recipe);
}
}

private <T extends CraftingRecipe> void onCraftingRecipeRemoved(RecipeRemovedEvent<T> e) {
Optional<Collection<String>> possibleFirstItemIds = e.getRecipe().getPossibleItemsInFirstSlot();
private <T extends CraftingRecipe> void onCraftingRecipeRemoved(RecipeEvent.Removed<T> evt) {
Optional<Collection<String>> possibleFirstItemIds = evt.recipe.getPossibleItemsInFirstSlot();
if (possibleFirstItemIds.isPresent()) {
for (String itemId : possibleFirstItemIds.get()) {
staticRecipes.remove(itemId, e.getRecipe());
staticRecipes.remove(itemId, evt.recipe);
}
} else {
dynamicRecipes.remove(e.getRecipe());
dynamicRecipes.remove(evt.recipe);
}
}
}

0 comments on commit 48d09c9

Please sign in to comment.