From f422465b51ecd1c1a00fab2d88eedecfb0a75abe Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Tue, 17 Jan 2017 22:57:57 +0100 Subject: [PATCH] Create RecipeEvent and move recipe events there --- .../v17/recipes/MinecraftRecipeRegistry.java | 15 +++-- .../v18/recipes/MinecraftRecipeRegistry.java | 15 +++-- .../java/nova/core/event/RecipeEvent.java | 63 +++++++++++++++++++ .../nova/core/recipes/RecipeAddedEvent.java | 40 ------------ .../java/nova/core/recipes/RecipeManager.java | 48 +++++++++----- .../nova/core/recipes/RecipeRemovedEvent.java | 40 ------------ .../crafting/CraftingRecipeManager.java | 27 ++++---- 7 files changed, 124 insertions(+), 124 deletions(-) create mode 100644 src/main/java/nova/core/event/RecipeEvent.java delete mode 100644 src/main/java/nova/core/recipes/RecipeAddedEvent.java delete mode 100644 src/main/java/nova/core/recipes/RecipeRemovedEvent.java diff --git a/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/recipes/MinecraftRecipeRegistry.java b/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/recipes/MinecraftRecipeRegistry.java index b60e7a16d..e076aa23d 100644 --- a/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/recipes/MinecraftRecipeRegistry.java +++ b/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/recipes/MinecraftRecipeRegistry.java @@ -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; @@ -58,7 +57,7 @@ public void registerRecipes() { RecipeManager recipeManager = Game.recipes(); - List recipes = (List) CraftingManager.getInstance().getRecipeList(); + List recipes = CraftingManager.getInstance().getRecipeList(); for (IRecipe recipe : recipes) { CraftingRecipe converted = convert(recipe); if (converted != null) { @@ -84,8 +83,8 @@ private IRecipe convert(CraftingRecipe recipe) { return RecipeConverter.toMinecraft(recipe); } - private void onNOVARecipeAdded(RecipeAddedEvent e) { - CraftingRecipe recipe = e.getRecipe(); + private void onNOVARecipeAdded(RecipeEvent.Add evt) { + CraftingRecipe recipe = evt.recipe; if (forwardWrappers.containsKey(recipe)) { return; } @@ -98,10 +97,10 @@ private void onNOVARecipeAdded(RecipeAddedEvent e) { CraftingManager.getInstance().getRecipeList().add(minecraftRecipe); } - private void onNOVARecipeRemoved(RecipeRemovedEvent e) { - IRecipe minecraftRecipe = forwardWrappers.get(e.getRecipe()); + private void onNOVARecipeRemoved(RecipeEvent.Remove evt) { + IRecipe minecraftRecipe = forwardWrappers.get(evt.recipe); - forwardWrappers.remove(e.getRecipe()); + forwardWrappers.remove(evt.recipe); backwardWrappers.remove(minecraftRecipe); CraftingManager.getInstance().getRecipeList().remove(minecraftRecipe); diff --git a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/recipes/MinecraftRecipeRegistry.java b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/recipes/MinecraftRecipeRegistry.java index e5efd721e..6bf591619 100644 --- a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/recipes/MinecraftRecipeRegistry.java +++ b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/recipes/MinecraftRecipeRegistry.java @@ -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; @@ -58,7 +57,7 @@ public void registerRecipes() { RecipeManager recipeManager = Game.recipes(); - List recipes = (List) CraftingManager.getInstance().getRecipeList(); + List recipes = CraftingManager.getInstance().getRecipeList(); for (IRecipe recipe : recipes) { CraftingRecipe converted = convert(recipe); if (converted != null) { @@ -84,8 +83,8 @@ private IRecipe convert(CraftingRecipe recipe) { return RecipeConverter.toMinecraft(recipe); } - private void onNOVARecipeAdded(RecipeAddedEvent e) { - CraftingRecipe recipe = e.getRecipe(); + private void onNOVARecipeAdded(RecipeEvent.Add evt) { + CraftingRecipe recipe = evt.recipe; if (forwardWrappers.containsKey(recipe)) { return; } @@ -98,10 +97,10 @@ private void onNOVARecipeAdded(RecipeAddedEvent e) { CraftingManager.getInstance().getRecipeList().add(minecraftRecipe); } - private void onNOVARecipeRemoved(RecipeRemovedEvent e) { - IRecipe minecraftRecipe = forwardWrappers.get(e.getRecipe()); + private void onNOVARecipeRemoved(RecipeEvent.Remove evt) { + IRecipe minecraftRecipe = forwardWrappers.get(evt.recipe); - forwardWrappers.remove(e.getRecipe()); + forwardWrappers.remove(evt.recipe); backwardWrappers.remove(minecraftRecipe); CraftingManager.getInstance().getRecipeList().remove(minecraftRecipe); diff --git a/src/main/java/nova/core/event/RecipeEvent.java b/src/main/java/nova/core/event/RecipeEvent.java new file mode 100644 index 000000000..6bb9960d8 --- /dev/null +++ b/src/main/java/nova/core/event/RecipeEvent.java @@ -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 . + */ +package nova.core.event; + +import nova.core.event.bus.CancelableEvent; +import nova.core.recipes.Recipe; + +/** + * All events related to the recipe. + * + * @param recipe type + * @author ExE Boss + */ +public abstract class RecipeEvent 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 recipe type + * @author Stan Hebben + */ + public static class Add extends RecipeEvent{ + public Add(T recipe) { + super(recipe); + } + } + + /** + * A recipe removed event is fired when a recipe of the right type has been + * removed from the RecipeManager. + * + * @param recipe type + * @author Stan Hebben + */ + public static class Remove extends RecipeEvent { + public Remove(T recipe) { + super(recipe); + } + } +} diff --git a/src/main/java/nova/core/recipes/RecipeAddedEvent.java b/src/main/java/nova/core/recipes/RecipeAddedEvent.java deleted file mode 100644 index ba97cc480..000000000 --- a/src/main/java/nova/core/recipes/RecipeAddedEvent.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2015 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 . - */ - -package nova.core.recipes; - -/** - * A recipe added event is fired when a recipe of the right type has been added - * to the RecipeManager. - * - * @param recipe type - * @author Stan Hebben - */ -public class RecipeAddedEvent { - private final T recipe; - - public RecipeAddedEvent(T recipe) { - this.recipe = recipe; - } - - public T getRecipe() { - return recipe; - } -} diff --git a/src/main/java/nova/core/recipes/RecipeManager.java b/src/main/java/nova/core/recipes/RecipeManager.java index 81e932e99..cb3fe77ac 100644 --- a/src/main/java/nova/core/recipes/RecipeManager.java +++ b/src/main/java/nova/core/recipes/RecipeManager.java @@ -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; @@ -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. @@ -67,16 +71,14 @@ public Collection getRecipes(Class type) { return getRecipeList(type).unmodifiableRecipes; } - public EventListenerHandle> whenRecipeAdded( - Class type, - EventListener> listener) { - return getRecipeList(type).recipeAddedListeners.on().bind(listener); + public EventListenerHandle> whenRecipeAdded( + Class type, EventListener> listener) { + return getRecipeList(type).events.on(RecipeEvent.Add.class).bind(listener); } - public EventListenerHandle> whenRecipeRemoved( - Class type, - EventListener> listener) { - return getRecipeList(type).recipeRemovedListeners.on().bind(listener); + public EventListenerHandle> whenRecipeRemoved( + Class type, EventListener> listener) { + return getRecipeList(type).events.on(RecipeEvent.Remove.class).bind(listener); } // ####################### @@ -103,30 +105,46 @@ private RecipeList collectRecipes(Class type) { return new RecipeList<>(result); } - private class RecipeList { + private class RecipeList implements Iterable { private Set recipes; private Set unmodifiableRecipes; - private EventBus> recipeAddedListeners; - private EventBus> recipeRemovedListeners; + private EventBus> events; private RecipeList(Set 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.Add<>(recipe)); } } private void remove(T recipe) { if (recipes.remove(recipe)) { - recipeRemovedListeners.publish(new RecipeRemovedEvent<>(recipe)); + events.publish(new RecipeEvent.Remove<>(recipe)); } } + + @Override + public Iterator iterator() { + return this.unmodifiableRecipes.iterator(); + } + + @Override + public Spliterator spliterator() { + return this.unmodifiableRecipes.spliterator(); + } + + public Stream stream() { + return this.unmodifiableRecipes.stream(); + } + + public Stream parallelStream() { + return this.unmodifiableRecipes.parallelStream(); + } } @Override diff --git a/src/main/java/nova/core/recipes/RecipeRemovedEvent.java b/src/main/java/nova/core/recipes/RecipeRemovedEvent.java deleted file mode 100644 index 607be5fe9..000000000 --- a/src/main/java/nova/core/recipes/RecipeRemovedEvent.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2015 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 . - */ - -package nova.core.recipes; - -/** - * A recipe removed event is fired when a recipe of the right type has been - * removed from the RecipeManager. - * - * @param recipe type - * @author Stan Hebben - */ -public class RecipeRemovedEvent { - private final T recipe; - - public RecipeRemovedEvent(T recipe) { - this.recipe = recipe; - } - - public T getRecipe() { - return recipe; - } -} diff --git a/src/main/java/nova/core/recipes/crafting/CraftingRecipeManager.java b/src/main/java/nova/core/recipes/crafting/CraftingRecipeManager.java index 6648c79ce..15b0b6a91 100644 --- a/src/main/java/nova/core/recipes/crafting/CraftingRecipeManager.java +++ b/src/main/java/nova/core/recipes/crafting/CraftingRecipeManager.java @@ -16,14 +16,15 @@ * * You should have received a copy of the GNU General Public License * along with NOVA. If not, see . - */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; @@ -59,8 +60,8 @@ public void addRecipe(CraftingRecipe recipe) { recipeManager.addRecipe(recipe); } - /* - * Removes a recipe. Removes if from the global recipe list. + /** + * Removes a recipe. Removes it from the global recipe list. * * @param recipe {@link CraftingRecipe} */ @@ -104,25 +105,25 @@ public Optional getRecipe(CraftingGrid grid) { // ### Private Methods ### // ####################### - private void onCraftingRecipeAdded(RecipeAddedEvent e) { - Optional> possibleFirstItemIds = e.getRecipe().getPossibleItemsInFirstSlot(); + private void onCraftingRecipeAdded(RecipeEvent.Add evt) { + Optional> 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 void onCraftingRecipeRemoved(RecipeRemovedEvent e) { - Optional> possibleFirstItemIds = e.getRecipe().getPossibleItemsInFirstSlot(); + private void onCraftingRecipeRemoved(RecipeEvent.Remove evt) { + Optional> 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); } } }