mirror of
https://github.com/GTNewHorizons/WanionLib.git
synced 2026-11-22 21:29:46 +00:00
[ci skip] spotlessApply with the new settings
This commit is contained in:
1 parent
c4f3929c92
commit
5baeb82eb7
23 files changed
+787
-917
No files matched your search
@@ -1,19 +1,17 @@
|
||||
package wanion.lib;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
public final class Reference
|
||||
{
|
||||
public static final String MOD_ID = "wanionlib";
|
||||
public static final String MOD_NAME = "WanionLib";
|
||||
public static final String MOD_VERSION = "GRADLETOKEN_VERSION";
|
||||
public static final String ACCEPTED_MINECRAFT = "[1.7.10]";
|
||||
public final class Reference {
|
||||
|
||||
private Reference() {}
|
||||
}
|
||||
public static final String MOD_ID = "wanionlib";
|
||||
public static final String MOD_NAME = "WanionLib";
|
||||
public static final String MOD_VERSION = "GRADLETOKEN_VERSION";
|
||||
public static final String ACCEPTED_MINECRAFT = "[1.7.10]";
|
||||
|
||||
private Reference() {}
|
||||
}
|
||||
@@ -1,67 +1,62 @@
|
||||
package wanion.lib;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import static wanion.lib.Reference.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import wanion.lib.client.ClientTickHandler;
|
||||
import wanion.lib.common.Dependencies;
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.event.FMLLoadCompleteEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import wanion.lib.client.ClientTickHandler;
|
||||
import wanion.lib.common.Dependencies;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static wanion.lib.Reference.*;
|
||||
|
||||
@Mod(modid = MOD_ID, name = MOD_NAME, version = MOD_VERSION, acceptedMinecraftVersions = ACCEPTED_MINECRAFT)
|
||||
public final class WanionLib
|
||||
{
|
||||
@Mod.Instance(MOD_ID)
|
||||
public static WanionLib instance;
|
||||
public final class WanionLib {
|
||||
|
||||
private Dependencies<IDependency> dependencies = new Dependencies<>();
|
||||
private Logger logger;
|
||||
@Mod.Instance(MOD_ID)
|
||||
public static WanionLib instance;
|
||||
|
||||
public static Dependencies<IDependency> getDependencies()
|
||||
{
|
||||
return instance.dependencies;
|
||||
}
|
||||
private Dependencies<IDependency> dependencies = new Dependencies<>();
|
||||
private Logger logger;
|
||||
|
||||
public static Logger getLogger()
|
||||
{
|
||||
return instance.logger;
|
||||
}
|
||||
public static Dependencies<IDependency> getDependencies() {
|
||||
return instance.dependencies;
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static ClientTickHandler getClientTickHandler()
|
||||
{
|
||||
return instance.dependencies.get(ClientTickHandler.class);
|
||||
}
|
||||
public static Logger getLogger() {
|
||||
return instance.logger;
|
||||
}
|
||||
|
||||
@Mod.EventHandler
|
||||
public void preInit(final FMLPreInitializationEvent event)
|
||||
{
|
||||
logger = event.getModLog();
|
||||
}
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static ClientTickHandler getClientTickHandler() {
|
||||
return instance.dependencies.get(ClientTickHandler.class);
|
||||
}
|
||||
|
||||
@Mod.EventHandler
|
||||
public void loadComplete(final FMLLoadCompleteEvent event)
|
||||
{
|
||||
dependencies = null;
|
||||
}
|
||||
@Mod.EventHandler
|
||||
public void preInit(final FMLPreInitializationEvent event) {
|
||||
logger = event.getModLog();
|
||||
}
|
||||
|
||||
public boolean matchModVersions(final Map<String, String> remoteVersions, final Side side)
|
||||
{
|
||||
return side == Side.CLIENT ? remoteVersions.containsKey(MOD_ID) : !remoteVersions.containsKey(MOD_ID) || remoteVersions.get(MOD_ID).equals(MOD_VERSION);
|
||||
}
|
||||
@Mod.EventHandler
|
||||
public void loadComplete(final FMLLoadCompleteEvent event) {
|
||||
dependencies = null;
|
||||
}
|
||||
|
||||
public interface IDependency {}
|
||||
}
|
||||
public boolean matchModVersions(final Map<String, String> remoteVersions, final Side side) {
|
||||
return side == Side.CLIENT ? remoteVersions.containsKey(MOD_ID)
|
||||
: !remoteVersions.containsKey(MOD_ID) || remoteVersions.get(MOD_ID).equals(MOD_VERSION);
|
||||
}
|
||||
|
||||
public interface IDependency {
|
||||
}
|
||||
}
|
||||
@@ -1,43 +1,39 @@
|
||||
package wanion.lib.client;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import wanion.lib.WanionLib;
|
||||
import wanion.lib.client.animation.Animation;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import wanion.lib.WanionLib;
|
||||
import wanion.lib.client.animation.Animation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public final class ClientTickHandler implements WanionLib.IDependency
|
||||
{
|
||||
private final List<Animation> registeredAnimations = new ArrayList<>();
|
||||
public final class ClientTickHandler implements WanionLib.IDependency {
|
||||
|
||||
private ClientTickHandler()
|
||||
{
|
||||
FMLCommonHandler.instance().bus().register(this);
|
||||
}
|
||||
private final List<Animation> registeredAnimations = new ArrayList<>();
|
||||
|
||||
public void registerAnimation(@Nonnull final Animation animation)
|
||||
{
|
||||
registeredAnimations.add(animation);
|
||||
}
|
||||
private ClientTickHandler() {
|
||||
FMLCommonHandler.instance().bus().register(this);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void tickEvent(final TickEvent.ClientTickEvent event)
|
||||
{
|
||||
registeredAnimations.forEach(Animation::updateAnimation);
|
||||
}
|
||||
}
|
||||
public void registerAnimation(@Nonnull final Animation animation) {
|
||||
registeredAnimations.add(animation);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void tickEvent(final TickEvent.ClientTickEvent event) {
|
||||
registeredAnimations.forEach(Animation::updateAnimation);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,33 @@
|
||||
package wanion.lib.client.animation;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public abstract class Animation
|
||||
{
|
||||
private final ResourceLocation[] frames;
|
||||
protected final int lastFrameIndex;
|
||||
protected int currentFrame = 0;
|
||||
public abstract class Animation {
|
||||
|
||||
public Animation(@Nonnull final ResourceLocation[] frames)
|
||||
{
|
||||
if ((lastFrameIndex = (this.frames = frames).length) - 1 == -1)
|
||||
throw new RuntimeException("What is an Animation with no frames?");
|
||||
}
|
||||
private final ResourceLocation[] frames;
|
||||
protected final int lastFrameIndex;
|
||||
protected int currentFrame = 0;
|
||||
|
||||
public abstract void updateAnimation();
|
||||
public Animation(@Nonnull final ResourceLocation[] frames) {
|
||||
if ((lastFrameIndex = (this.frames = frames).length) - 1 == -1)
|
||||
throw new RuntimeException("What is an Animation with no frames?");
|
||||
}
|
||||
|
||||
public final ResourceLocation getCurrentFrame()
|
||||
{
|
||||
return frames[currentFrame];
|
||||
}
|
||||
}
|
||||
public abstract void updateAnimation();
|
||||
|
||||
public final ResourceLocation getCurrentFrame() {
|
||||
return frames[currentFrame];
|
||||
}
|
||||
}
|
||||
@@ -1,37 +1,33 @@
|
||||
package wanion.lib.client.animation;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class ComplexAnimation extends Animation
|
||||
{
|
||||
private final int[] animationStages;
|
||||
private final int lastStageIndex;
|
||||
private int currentStage = 0;
|
||||
public class ComplexAnimation extends Animation {
|
||||
|
||||
public ComplexAnimation(@Nonnull final ResourceLocation[] frames, final int[] animationStages)
|
||||
{
|
||||
super(frames);
|
||||
lastStageIndex = (this.animationStages = animationStages).length - 1;
|
||||
}
|
||||
private final int[] animationStages;
|
||||
private final int lastStageIndex;
|
||||
private int currentStage = 0;
|
||||
|
||||
@Override
|
||||
public void updateAnimation()
|
||||
{
|
||||
if (++currentStage > lastStageIndex)
|
||||
currentStage = 0;
|
||||
currentFrame = animationStages[currentStage];
|
||||
}
|
||||
}
|
||||
public ComplexAnimation(@Nonnull final ResourceLocation[] frames, final int[] animationStages) {
|
||||
super(frames);
|
||||
lastStageIndex = (this.animationStages = animationStages).length - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAnimation() {
|
||||
if (++currentStage > lastStageIndex) currentStage = 0;
|
||||
currentFrame = animationStages[currentStage];
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,29 @@
|
||||
package wanion.lib.client.animation;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public final class ComplexHalfAnimation extends ComplexAnimation
|
||||
{
|
||||
private boolean half;
|
||||
public final class ComplexHalfAnimation extends ComplexAnimation {
|
||||
|
||||
public ComplexHalfAnimation(@Nonnull final ResourceLocation[] frames, final int[] stages)
|
||||
{
|
||||
super(frames, stages);
|
||||
}
|
||||
private boolean half;
|
||||
|
||||
@Override
|
||||
public void updateAnimation()
|
||||
{
|
||||
if (half ^= true)
|
||||
super.updateAnimation();
|
||||
}
|
||||
}
|
||||
public ComplexHalfAnimation(@Nonnull final ResourceLocation[] frames, final int[] stages) {
|
||||
super(frames, stages);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAnimation() {
|
||||
if (half ^= true) super.updateAnimation();
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,27 @@
|
||||
package wanion.lib.client.animation;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class SimpleAnimation extends Animation
|
||||
{
|
||||
public SimpleAnimation(@Nonnull final ResourceLocation[] frames)
|
||||
{
|
||||
super(frames);
|
||||
}
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
@Override
|
||||
public void updateAnimation()
|
||||
{
|
||||
if (++currentFrame > lastFrameIndex)
|
||||
currentFrame = 0;
|
||||
}
|
||||
}
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class SimpleAnimation extends Animation {
|
||||
|
||||
public SimpleAnimation(@Nonnull final ResourceLocation[] frames) {
|
||||
super(frames);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAnimation() {
|
||||
if (++currentFrame > lastFrameIndex) currentFrame = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,29 @@
|
||||
package wanion.lib.client.animation;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public final class SimpleHalfAnimation extends SimpleAnimation
|
||||
{
|
||||
private boolean half;
|
||||
public final class SimpleHalfAnimation extends SimpleAnimation {
|
||||
|
||||
public SimpleHalfAnimation(@Nonnull final ResourceLocation[] frames)
|
||||
{
|
||||
super(frames);
|
||||
}
|
||||
private boolean half;
|
||||
|
||||
@Override
|
||||
public void updateAnimation()
|
||||
{
|
||||
if (half ^= true)
|
||||
super.updateAnimation();
|
||||
}
|
||||
}
|
||||
public SimpleHalfAnimation(@Nonnull final ResourceLocation[] frames) {
|
||||
super(frames);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAnimation() {
|
||||
if (half ^= true) super.updateAnimation();
|
||||
}
|
||||
}
|
||||
@@ -1,82 +1,74 @@
|
||||
package wanion.lib.common;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class Dependencies<D>
|
||||
{
|
||||
private final Map<Class, D> dependencies = new IdentityHashMap<>();
|
||||
private final Map<Class, DependenceWatcher<? extends D>> dependenciesWatchers = new IdentityHashMap<>();
|
||||
public class Dependencies<D> {
|
||||
|
||||
public final <I extends D> void add(final Class<? extends I> typeClass)
|
||||
{
|
||||
if (dependencies.containsKey(typeClass))
|
||||
return;
|
||||
final DependenceWatcher<? extends D> dependenceWatcher = dependenciesWatchers.get(typeClass);
|
||||
if (dependenceWatcher != null) {
|
||||
add(dependenceWatcher.instantiate());
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final Constructor constructor = typeClass.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
add((I) constructor.newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
private final Map<Class, D> dependencies = new IdentityHashMap<>();
|
||||
private final Map<Class, DependenceWatcher<? extends D>> dependenciesWatchers = new IdentityHashMap<>();
|
||||
|
||||
public final <I extends D> void add(final I instance)
|
||||
{
|
||||
final Class typeClass = instance.getClass();
|
||||
if (dependencies.containsKey(typeClass))
|
||||
return;
|
||||
dependencies.put(typeClass, instance);
|
||||
}
|
||||
public final <I extends D> void add(final Class<? extends I> typeClass) {
|
||||
if (dependencies.containsKey(typeClass)) return;
|
||||
final DependenceWatcher<? extends D> dependenceWatcher = dependenciesWatchers.get(typeClass);
|
||||
if (dependenceWatcher != null) {
|
||||
add(dependenceWatcher.instantiate());
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final Constructor constructor = typeClass.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
add((I) constructor.newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException
|
||||
| InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public final <I extends D> I get(final Class<I> typeClass)
|
||||
{
|
||||
final I instance = (I) dependencies.get(typeClass);
|
||||
if (instance != null)
|
||||
return instance;
|
||||
add(typeClass);
|
||||
return (I) dependencies.get(typeClass);
|
||||
}
|
||||
public final <I extends D> void add(final I instance) {
|
||||
final Class typeClass = instance.getClass();
|
||||
if (dependencies.containsKey(typeClass)) return;
|
||||
dependencies.put(typeClass, instance);
|
||||
}
|
||||
|
||||
public final boolean contains(final Class<? extends D> typeClass)
|
||||
{
|
||||
return dependencies.containsKey(typeClass);
|
||||
}
|
||||
public final <I extends D> I get(final Class<I> typeClass) {
|
||||
final I instance = (I) dependencies.get(typeClass);
|
||||
if (instance != null) return instance;
|
||||
add(typeClass);
|
||||
return (I) dependencies.get(typeClass);
|
||||
}
|
||||
|
||||
public final void subscribe(final DependenceWatcher<? extends D> dependenceWatcher)
|
||||
{
|
||||
if (!dependenciesWatchers.containsKey(dependenceWatcher.dependenceClass))
|
||||
dependenciesWatchers.put(dependenceWatcher.dependenceClass, dependenceWatcher);
|
||||
}
|
||||
public final boolean contains(final Class<? extends D> typeClass) {
|
||||
return dependencies.containsKey(typeClass);
|
||||
}
|
||||
|
||||
public abstract class DependenceWatcher<W extends D>
|
||||
{
|
||||
private final Class<W> dependenceClass;
|
||||
public final void subscribe(final DependenceWatcher<? extends D> dependenceWatcher) {
|
||||
if (!dependenciesWatchers.containsKey(dependenceWatcher.dependenceClass))
|
||||
dependenciesWatchers.put(dependenceWatcher.dependenceClass, dependenceWatcher);
|
||||
}
|
||||
|
||||
protected DependenceWatcher()
|
||||
{
|
||||
this.dependenceClass = (Class<W>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
|
||||
}
|
||||
public abstract class DependenceWatcher<W extends D> {
|
||||
|
||||
@Nonnull
|
||||
public abstract W instantiate();
|
||||
}
|
||||
}
|
||||
private final Class<W> dependenceClass;
|
||||
|
||||
protected DependenceWatcher() {
|
||||
this.dependenceClass = (Class<W>) ((ParameterizedType) getClass().getGenericSuperclass())
|
||||
.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public abstract W instantiate();
|
||||
}
|
||||
}
|
||||
@@ -1,107 +1,89 @@
|
||||
package wanion.lib.common;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.AbstractList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class FixedSizeList<E> extends AbstractList<E>
|
||||
{
|
||||
private static final int DEFAULT_CAPACITY = 64;
|
||||
private final E[] allTheData;
|
||||
private final int capacity;
|
||||
private int size;
|
||||
public class FixedSizeList<E> extends AbstractList<E> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public FixedSizeList()
|
||||
{
|
||||
allTheData = (E[]) new Object[capacity = DEFAULT_CAPACITY];
|
||||
}
|
||||
private static final int DEFAULT_CAPACITY = 64;
|
||||
private final E[] allTheData;
|
||||
private final int capacity;
|
||||
private int size;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public FixedSizeList(int capacity)
|
||||
{
|
||||
if (capacity <= 0)
|
||||
capacity = DEFAULT_CAPACITY;
|
||||
allTheData = (E[]) new Object[this.capacity = capacity];
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public FixedSizeList() {
|
||||
allTheData = (E[]) new Object[capacity = DEFAULT_CAPACITY];
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private FixedSizeList(@Nonnull final Collection<E> collection)
|
||||
{
|
||||
allTheData = (E[]) Arrays.copyOf(collection.toArray(), capacity = size = collection.size());
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public FixedSizeList(int capacity) {
|
||||
if (capacity <= 0) capacity = DEFAULT_CAPACITY;
|
||||
allTheData = (E[]) new Object[this.capacity = capacity];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
private FixedSizeList(@Nonnull final Collection<E> collection) {
|
||||
allTheData = (E[]) Arrays.copyOf(collection.toArray(), capacity = size = collection.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return size == 0;
|
||||
}
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(final E e)
|
||||
{
|
||||
if (size == capacity)
|
||||
return false;
|
||||
allTheData[size++] = e;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final int index, E e)
|
||||
{
|
||||
allTheData[index] = e;
|
||||
}
|
||||
@Override
|
||||
public boolean add(final E e) {
|
||||
if (size == capacity) return false;
|
||||
allTheData[size++] = e;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(final int index)
|
||||
{
|
||||
if (index > size)
|
||||
return null;
|
||||
return allTheData[index];
|
||||
}
|
||||
@Override
|
||||
public void add(final int index, E e) {
|
||||
allTheData[index] = e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(final int index)
|
||||
{
|
||||
if (index >= size)
|
||||
throw new IndexOutOfBoundsException();
|
||||
modCount++;
|
||||
final E removed = allTheData[index];
|
||||
if (size != 1)
|
||||
System.arraycopy(allTheData, index + 1, allTheData, index, size - index - 1);
|
||||
allTheData[--size] = null;
|
||||
return removed;
|
||||
}
|
||||
@Override
|
||||
public E get(final int index) {
|
||||
if (index > size) return null;
|
||||
return allTheData[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Object[] toArray()
|
||||
{
|
||||
return Arrays.copyOf(allTheData, size);
|
||||
}
|
||||
@Override
|
||||
public E remove(final int index) {
|
||||
if (index >= size) throw new IndexOutOfBoundsException();
|
||||
modCount++;
|
||||
final E removed = allTheData[index];
|
||||
if (size != 1) System.arraycopy(allTheData, index + 1, allTheData, index, size - index - 1);
|
||||
allTheData[--size] = null;
|
||||
return removed;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
modCount++;
|
||||
while (size > 0)
|
||||
allTheData[--size] = null;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
@Nonnull
|
||||
public Object[] toArray() {
|
||||
return Arrays.copyOf(allTheData, size);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void clear() {
|
||||
modCount++;
|
||||
while (size > 0) allTheData[--size] = null;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
package wanion.lib.common;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
public interface IRemovable
|
||||
{
|
||||
boolean removed();
|
||||
void setRemoved(final boolean removed);
|
||||
}
|
||||
public interface IRemovable {
|
||||
|
||||
boolean removed();
|
||||
|
||||
void setRemoved(final boolean removed);
|
||||
}
|
||||
@@ -1,18 +1,16 @@
|
||||
package wanion.lib.common;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Instantiator<I>
|
||||
{
|
||||
@Nonnull
|
||||
I instantiate(Class<? extends I> moduleThreadClass) throws InstantiationException, IllegalAccessException;
|
||||
}
|
||||
public interface Instantiator<I> {
|
||||
|
||||
@Nonnull
|
||||
I instantiate(Class<? extends I> moduleThreadClass) throws InstantiationException, IllegalAccessException;
|
||||
}
|
||||
@@ -1,13 +1,19 @@
|
||||
package wanion.lib.common;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
|
||||
import cpw.mods.fml.common.registry.FMLControlledNamespacedRegistry;
|
||||
import cpw.mods.fml.common.registry.GameData;
|
||||
import gnu.trove.list.TIntList;
|
||||
@@ -18,102 +24,81 @@ import gnu.trove.map.TIntObjectMap;
|
||||
import gnu.trove.map.hash.TIntIntHashMap;
|
||||
import gnu.trove.set.TIntSet;
|
||||
import gnu.trove.set.hash.TIntHashSet;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Collection;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class MetaItem
|
||||
{
|
||||
public static final FMLControlledNamespacedRegistry<Item> itemRegistry = GameData.getItemRegistry();
|
||||
public final class MetaItem {
|
||||
|
||||
private MetaItem() {}
|
||||
public static final FMLControlledNamespacedRegistry<Item> itemRegistry = GameData.getItemRegistry();
|
||||
|
||||
public static int get(final ItemStack itemStack)
|
||||
{
|
||||
Item item;
|
||||
if (itemStack == null || (item = itemStack.getItem()) == null)
|
||||
return 0;
|
||||
final int id = itemRegistry.getId(item);
|
||||
return id > 0 ? item.getDamage(itemStack) == OreDictionary.WILDCARD_VALUE ? id : id | item.getDamage(itemStack) + 1 << 16 : 0;
|
||||
}
|
||||
private MetaItem() {}
|
||||
|
||||
public static int get(final Item item)
|
||||
{
|
||||
if (item == null)
|
||||
return 0;
|
||||
final int id = itemRegistry.getIDForObject(item);
|
||||
return id > 0 ? id | 65536 : 0;
|
||||
}
|
||||
public static int get(final ItemStack itemStack) {
|
||||
Item item;
|
||||
if (itemStack == null || (item = itemStack.getItem()) == null) return 0;
|
||||
final int id = itemRegistry.getId(item);
|
||||
return id > 0 ? item.getDamage(itemStack) == OreDictionary.WILDCARD_VALUE ? id
|
||||
: id | item.getDamage(itemStack) + 1 << 16 : 0;
|
||||
}
|
||||
|
||||
public static ItemStack toItemStack(final int metaItemKey)
|
||||
{
|
||||
return metaItemKey > 0 ? new ItemStack(itemRegistry.getRaw(metaItemKey ^ (metaItemKey & 65536)), 0, metaItemKey >> 16) : null;
|
||||
}
|
||||
public static int get(final Item item) {
|
||||
if (item == null) return 0;
|
||||
final int id = itemRegistry.getIDForObject(item);
|
||||
return id > 0 ? id | 65536 : 0;
|
||||
}
|
||||
|
||||
public static int[] getArray(final Collection<ItemStack> itemStackCollection)
|
||||
{
|
||||
return getList(itemStackCollection).toArray();
|
||||
}
|
||||
public static ItemStack toItemStack(final int metaItemKey) {
|
||||
return metaItemKey > 0
|
||||
? new ItemStack(itemRegistry.getRaw(metaItemKey ^ (metaItemKey & 65536)), 0, metaItemKey >> 16)
|
||||
: null;
|
||||
}
|
||||
|
||||
public static TIntList getList(final Collection<ItemStack> itemStackCollection)
|
||||
{
|
||||
final TIntList keys = new TIntArrayList();
|
||||
int hash;
|
||||
for (final ItemStack itemStack : itemStackCollection)
|
||||
if ((hash = get(itemStack)) != 0)
|
||||
keys.add(hash);
|
||||
return keys;
|
||||
}
|
||||
public static int[] getArray(final Collection<ItemStack> itemStackCollection) {
|
||||
return getList(itemStackCollection).toArray();
|
||||
}
|
||||
|
||||
public static TIntSet getSet(final Collection<ItemStack> itemStackCollection)
|
||||
{
|
||||
return new TIntHashSet(getList(itemStackCollection));
|
||||
}
|
||||
public static TIntList getList(final Collection<ItemStack> itemStackCollection) {
|
||||
final TIntList keys = new TIntArrayList();
|
||||
int hash;
|
||||
for (final ItemStack itemStack : itemStackCollection) if ((hash = get(itemStack)) != 0) keys.add(hash);
|
||||
return keys;
|
||||
}
|
||||
|
||||
public static <E> void populateMap(final Collection<ItemStack> itemStackCollection, final TIntObjectMap<E> map, final E defaultValue)
|
||||
{
|
||||
for (final int id : getArray(itemStackCollection))
|
||||
map.put(id, defaultValue);
|
||||
}
|
||||
public static TIntSet getSet(final Collection<ItemStack> itemStackCollection) {
|
||||
return new TIntHashSet(getList(itemStackCollection));
|
||||
}
|
||||
|
||||
public static void populateMap(final Collection<ItemStack> itemStackCollection, final TIntLongMap map, long defaultValue)
|
||||
{
|
||||
for (final int id : getArray(itemStackCollection))
|
||||
map.put(id, defaultValue);
|
||||
}
|
||||
public static <E> void populateMap(final Collection<ItemStack> itemStackCollection, final TIntObjectMap<E> map,
|
||||
final E defaultValue) {
|
||||
for (final int id : getArray(itemStackCollection)) map.put(id, defaultValue);
|
||||
}
|
||||
|
||||
public static TIntIntMap getKeySizeMap(final int startIndex, final int endIndex, @Nonnull ItemStack[] itemStacks)
|
||||
{
|
||||
final TIntIntMap keySizeMap = new TIntIntHashMap();
|
||||
for (int i = startIndex; i < endIndex; i++) {
|
||||
if (itemStacks[i] == null)
|
||||
continue;
|
||||
final int key = get(itemStacks[i]);
|
||||
if (keySizeMap.containsKey(key))
|
||||
keySizeMap.put(key, keySizeMap.get(key) + 1);
|
||||
else
|
||||
keySizeMap.put(key, 1);
|
||||
}
|
||||
return keySizeMap;
|
||||
}
|
||||
public static void populateMap(final Collection<ItemStack> itemStackCollection, final TIntLongMap map,
|
||||
long defaultValue) {
|
||||
for (final int id : getArray(itemStackCollection)) map.put(id, defaultValue);
|
||||
}
|
||||
|
||||
public static TIntIntMap getSmartKeySizeMap(final int startIndex, final int endIndex, @Nonnull ItemStack[] itemStacks)
|
||||
{
|
||||
final TIntIntMap smartKeySizeMap = new TIntIntHashMap();
|
||||
for (int i = startIndex; i < endIndex; i++) {
|
||||
final ItemStack itemStack = itemStacks[i];
|
||||
if (itemStack == null)
|
||||
continue;
|
||||
final int key = get(itemStack);
|
||||
if (smartKeySizeMap.containsKey(key))
|
||||
smartKeySizeMap.put(key, smartKeySizeMap.get(key) + itemStack.stackSize);
|
||||
else
|
||||
smartKeySizeMap.put(key, itemStack.stackSize);
|
||||
}
|
||||
return smartKeySizeMap;
|
||||
}
|
||||
}
|
||||
public static TIntIntMap getKeySizeMap(final int startIndex, final int endIndex, @Nonnull ItemStack[] itemStacks) {
|
||||
final TIntIntMap keySizeMap = new TIntIntHashMap();
|
||||
for (int i = startIndex; i < endIndex; i++) {
|
||||
if (itemStacks[i] == null) continue;
|
||||
final int key = get(itemStacks[i]);
|
||||
if (keySizeMap.containsKey(key)) keySizeMap.put(key, keySizeMap.get(key) + 1);
|
||||
else keySizeMap.put(key, 1);
|
||||
}
|
||||
return keySizeMap;
|
||||
}
|
||||
|
||||
public static TIntIntMap getSmartKeySizeMap(final int startIndex, final int endIndex,
|
||||
@Nonnull ItemStack[] itemStacks) {
|
||||
final TIntIntMap smartKeySizeMap = new TIntIntHashMap();
|
||||
for (int i = startIndex; i < endIndex; i++) {
|
||||
final ItemStack itemStack = itemStacks[i];
|
||||
if (itemStack == null) continue;
|
||||
final int key = get(itemStack);
|
||||
if (smartKeySizeMap.containsKey(key))
|
||||
smartKeySizeMap.put(key, smartKeySizeMap.get(key) + itemStack.stackSize);
|
||||
else smartKeySizeMap.put(key, itemStack.stackSize);
|
||||
}
|
||||
return smartKeySizeMap;
|
||||
}
|
||||
}
|
||||
@@ -1,71 +1,62 @@
|
||||
package wanion.lib.common;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import minetweaker.MineTweakerAPI;
|
||||
import minetweaker.api.item.IIngredient;
|
||||
import minetweaker.api.item.IItemStack;
|
||||
import minetweaker.api.oredict.IOreDictEntry;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class MineTweakerHelper
|
||||
{
|
||||
private MineTweakerHelper() {}
|
||||
public final class MineTweakerHelper {
|
||||
|
||||
public static ItemStack toStack(final IItemStack item)
|
||||
{
|
||||
if (item == null)
|
||||
return null;
|
||||
final Object internal = item.getInternal();
|
||||
if (internal == null || !(internal instanceof ItemStack))
|
||||
MineTweakerAPI.getLogger().logError("Not a valid item stack: " + item);
|
||||
return internal instanceof ItemStack ? (ItemStack) internal : null;
|
||||
}
|
||||
private MineTweakerHelper() {}
|
||||
|
||||
public static Object[] toObjects(final IIngredient[] list)
|
||||
{
|
||||
if (list == null)
|
||||
return null;
|
||||
Object[] ingredients = new Object[list.length];
|
||||
for (int x = 0; x < list.length; x++) {
|
||||
ingredients[x] = toObject(list[x]);
|
||||
}
|
||||
return ingredients;
|
||||
}
|
||||
public static ItemStack toStack(final IItemStack item) {
|
||||
if (item == null) return null;
|
||||
final Object internal = item.getInternal();
|
||||
if (internal == null || !(internal instanceof ItemStack))
|
||||
MineTweakerAPI.getLogger().logError("Not a valid item stack: " + item);
|
||||
return internal instanceof ItemStack ? (ItemStack) internal : null;
|
||||
}
|
||||
|
||||
public static Object toActualObject(final IIngredient ingredient)
|
||||
{
|
||||
if (ingredient == null) return null;
|
||||
else {
|
||||
if (ingredient instanceof IOreDictEntry) {
|
||||
return OreDictionary.getOres(toString((IOreDictEntry) ingredient));
|
||||
} else if (ingredient instanceof IItemStack) {
|
||||
return toStack((IItemStack) ingredient);
|
||||
} else return null;
|
||||
}
|
||||
}
|
||||
public static Object[] toObjects(final IIngredient[] list) {
|
||||
if (list == null) return null;
|
||||
Object[] ingredients = new Object[list.length];
|
||||
for (int x = 0; x < list.length; x++) {
|
||||
ingredients[x] = toObject(list[x]);
|
||||
}
|
||||
return ingredients;
|
||||
}
|
||||
|
||||
public static Object toObject(final IIngredient ingredient)
|
||||
{
|
||||
if (ingredient == null)
|
||||
return null;
|
||||
if (ingredient instanceof IOreDictEntry) {
|
||||
return toString((IOreDictEntry) ingredient);
|
||||
} else if (ingredient instanceof IItemStack) {
|
||||
return toStack((IItemStack) ingredient);
|
||||
} else return null;
|
||||
}
|
||||
public static Object toActualObject(final IIngredient ingredient) {
|
||||
if (ingredient == null) return null;
|
||||
else {
|
||||
if (ingredient instanceof IOreDictEntry) {
|
||||
return OreDictionary.getOres(toString((IOreDictEntry) ingredient));
|
||||
} else if (ingredient instanceof IItemStack) {
|
||||
return toStack((IItemStack) ingredient);
|
||||
} else return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String toString(final IOreDictEntry entry)
|
||||
{
|
||||
return entry.getName();
|
||||
}
|
||||
}
|
||||
public static Object toObject(final IIngredient ingredient) {
|
||||
if (ingredient == null) return null;
|
||||
if (ingredient instanceof IOreDictEntry) {
|
||||
return toString((IOreDictEntry) ingredient);
|
||||
} else if (ingredient instanceof IItemStack) {
|
||||
return toStack((IItemStack) ingredient);
|
||||
} else return null;
|
||||
}
|
||||
|
||||
public static String toString(final IOreDictEntry entry) {
|
||||
return entry.getName();
|
||||
}
|
||||
}
|
||||
@@ -1,45 +1,45 @@
|
||||
package wanion.lib.common;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import cpw.mods.fml.common.registry.GameData;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public final class Util
|
||||
{
|
||||
private Util() {}
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, E extends T> E getField(Class clas, String name, Object instance, Class<T> expectedClass)
|
||||
{
|
||||
try {
|
||||
final Field field = clas.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
return (E) expectedClass.cast(field.get(instance));
|
||||
} catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
import cpw.mods.fml.common.registry.GameData;
|
||||
|
||||
public static <E> void setField(Class clas, String name, Object instance, E newInstance)
|
||||
{
|
||||
try {
|
||||
final Field field = clas.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(instance, newInstance);
|
||||
} catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
public final class Util {
|
||||
|
||||
public static String getModName(final ItemStack itemStack)
|
||||
{
|
||||
String name = GameData.getItemRegistry().getNameForObject(itemStack.getItem());
|
||||
return name.substring(0, name.indexOf(58));
|
||||
}
|
||||
}
|
||||
private Util() {}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, E extends T> E getField(Class clas, String name, Object instance, Class<T> expectedClass) {
|
||||
try {
|
||||
final Field field = clas.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
return (E) expectedClass.cast(field.get(instance));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <E> void setField(Class clas, String name, Object instance, E newInstance) {
|
||||
try {
|
||||
final Field field = clas.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(instance, newInstance);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getModName(final ItemStack itemStack) {
|
||||
String name = GameData.getItemRegistry().getNameForObject(itemStack.getItem());
|
||||
return name.substring(0, name.indexOf(58));
|
||||
}
|
||||
}
|
||||
@@ -1,98 +1,99 @@
|
||||
package wanion.lib.module;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import wanion.lib.WanionLib;
|
||||
import wanion.lib.common.Instantiator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public abstract class AbstractModule
|
||||
{
|
||||
protected final Manager manager;
|
||||
private final String moduleName;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
protected AbstractModule(@Nonnull final String moduleName, @Nonnull final Instantiator<AbstractModuleThread> moduleThreadInstantiator)
|
||||
{
|
||||
this.moduleName = moduleName;
|
||||
manager = new Manager(moduleThreadInstantiator);
|
||||
}
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
protected abstract void init();
|
||||
import wanion.lib.WanionLib;
|
||||
import wanion.lib.common.Instantiator;
|
||||
|
||||
final void start(@Nonnull final LoadStage loadStage, @Nonnull final Manager manager)
|
||||
{
|
||||
final List<AbstractModuleThread> threadList = manager.getInstances(loadStage);
|
||||
if (threadList.isEmpty())
|
||||
return;
|
||||
final ExecutorService moduleThreadExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
final Logger logger = WanionLib.getLogger();
|
||||
try {
|
||||
final long initialTime = System.nanoTime();
|
||||
final List<Future<String>> futureOfThreads = moduleThreadExecutor.invokeAll(threadList);
|
||||
final long took = System.nanoTime() - initialTime;
|
||||
for (final Future<String> threadModuleSay : futureOfThreads)
|
||||
logger.info(threadModuleSay.get());
|
||||
logger.info("All " + threadList.size() + " " + moduleName + "s took " + took / 1000000 + "ms to finish. at load stage " + loadStage.name());
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
logger.error("Something really bad happened on " + moduleName + " at load stage " + loadStage.name());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public abstract class AbstractModule {
|
||||
|
||||
public static class Manager
|
||||
{
|
||||
private final Map<LoadStage, Set<Class<? extends AbstractModuleThread>>> loadStageMap;
|
||||
private final Instantiator<AbstractModuleThread> instantiator;
|
||||
protected final Manager manager;
|
||||
private final String moduleName;
|
||||
|
||||
public Manager(@Nonnull final Instantiator<AbstractModuleThread> instantiator)
|
||||
{
|
||||
this.instantiator = instantiator;
|
||||
loadStageMap = new EnumMap<>(LoadStage.class);
|
||||
for (final LoadStage loadStage : LoadStage.values())
|
||||
loadStageMap.put(loadStage, new LinkedHashSet<>());
|
||||
}
|
||||
protected AbstractModule(@Nonnull final String moduleName,
|
||||
@Nonnull final Instantiator<AbstractModuleThread> moduleThreadInstantiator) {
|
||||
this.moduleName = moduleName;
|
||||
manager = new Manager(moduleThreadInstantiator);
|
||||
}
|
||||
|
||||
public boolean add(@Nonnull final Class<? extends AbstractModuleThread> moduleThreadClass)
|
||||
{
|
||||
final LoadStage loadStage = (moduleThreadClass.isAnnotationPresent(SpecifiedLoadStage.class)) ? moduleThreadClass.getAnnotation(SpecifiedLoadStage.class).stage() : LoadStage.POST_INIT;
|
||||
final Set<Class<? extends AbstractModuleThread>> classSet = loadStageMap.get(loadStage);
|
||||
return !classSet.contains(moduleThreadClass) && classSet.add(moduleThreadClass);
|
||||
}
|
||||
protected abstract void init();
|
||||
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return loadStageMap.values().stream().allMatch(Set::isEmpty);
|
||||
}
|
||||
final void start(@Nonnull final LoadStage loadStage, @Nonnull final Manager manager) {
|
||||
final List<AbstractModuleThread> threadList = manager.getInstances(loadStage);
|
||||
if (threadList.isEmpty()) return;
|
||||
final ExecutorService moduleThreadExecutor = Executors
|
||||
.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
final Logger logger = WanionLib.getLogger();
|
||||
try {
|
||||
final long initialTime = System.nanoTime();
|
||||
final List<Future<String>> futureOfThreads = moduleThreadExecutor.invokeAll(threadList);
|
||||
final long took = System.nanoTime() - initialTime;
|
||||
for (final Future<String> threadModuleSay : futureOfThreads) logger.info(threadModuleSay.get());
|
||||
logger.info(
|
||||
"All " + threadList.size()
|
||||
+ " "
|
||||
+ moduleName
|
||||
+ "s took "
|
||||
+ took / 1000000
|
||||
+ "ms to finish. at load stage "
|
||||
+ loadStage.name());
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
logger.error("Something really bad happened on " + moduleName + " at load stage " + loadStage.name());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty(final LoadStage loadStage)
|
||||
{
|
||||
return loadStageMap.get(loadStage).isEmpty();
|
||||
}
|
||||
public static class Manager {
|
||||
|
||||
public List<AbstractModuleThread> getInstances(final LoadStage loadStage)
|
||||
{
|
||||
final List<AbstractModuleThread> abstractModuleThreads = new ArrayList<>();
|
||||
loadStageMap.get(loadStage).forEach(t -> {
|
||||
try {
|
||||
abstractModuleThreads.add(instantiator.instantiate(t));
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return abstractModuleThreads;
|
||||
}
|
||||
}
|
||||
}
|
||||
private final Map<LoadStage, Set<Class<? extends AbstractModuleThread>>> loadStageMap;
|
||||
private final Instantiator<AbstractModuleThread> instantiator;
|
||||
|
||||
public Manager(@Nonnull final Instantiator<AbstractModuleThread> instantiator) {
|
||||
this.instantiator = instantiator;
|
||||
loadStageMap = new EnumMap<>(LoadStage.class);
|
||||
for (final LoadStage loadStage : LoadStage.values()) loadStageMap.put(loadStage, new LinkedHashSet<>());
|
||||
}
|
||||
|
||||
public boolean add(@Nonnull final Class<? extends AbstractModuleThread> moduleThreadClass) {
|
||||
final LoadStage loadStage = (moduleThreadClass.isAnnotationPresent(SpecifiedLoadStage.class))
|
||||
? moduleThreadClass.getAnnotation(SpecifiedLoadStage.class).stage()
|
||||
: LoadStage.POST_INIT;
|
||||
final Set<Class<? extends AbstractModuleThread>> classSet = loadStageMap.get(loadStage);
|
||||
return !classSet.contains(moduleThreadClass) && classSet.add(moduleThreadClass);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return loadStageMap.values().stream().allMatch(Set::isEmpty);
|
||||
}
|
||||
|
||||
public boolean isEmpty(final LoadStage loadStage) {
|
||||
return loadStageMap.get(loadStage).isEmpty();
|
||||
}
|
||||
|
||||
public List<AbstractModuleThread> getInstances(final LoadStage loadStage) {
|
||||
final List<AbstractModuleThread> abstractModuleThreads = new ArrayList<>();
|
||||
loadStageMap.get(loadStage).forEach(t -> {
|
||||
try {
|
||||
abstractModuleThreads.add(instantiator.instantiate(t));
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return abstractModuleThreads;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,18 @@
|
||||
package wanion.lib.module;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public abstract class AbstractModuleThread implements Callable<String>
|
||||
{
|
||||
protected final String threadName;
|
||||
public abstract class AbstractModuleThread implements Callable<String> {
|
||||
|
||||
public AbstractModuleThread(String threadName, String moduleName)
|
||||
{
|
||||
this.threadName = threadName + " " + moduleName + ": ";
|
||||
}
|
||||
}
|
||||
protected final String threadName;
|
||||
|
||||
public AbstractModuleThread(String threadName, String moduleName) {
|
||||
this.threadName = threadName + " " + moduleName + ": ";
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,30 @@
|
||||
package wanion.lib.module;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import cpw.mods.fml.common.event.*;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public enum LoadStage
|
||||
{
|
||||
PRE_INIT(FMLPreInitializationEvent.class),
|
||||
INIT(FMLInitializationEvent.class),
|
||||
POST_INIT(FMLPostInitializationEvent.class),
|
||||
LOAD_COMPLETE(FMLLoadCompleteEvent.class);
|
||||
import cpw.mods.fml.common.event.*;
|
||||
|
||||
public final Class<? extends FMLStateEvent> stage;
|
||||
public enum LoadStage {
|
||||
|
||||
LoadStage(Class<? extends FMLStateEvent> stage)
|
||||
{
|
||||
this.stage = stage;
|
||||
}
|
||||
PRE_INIT(FMLPreInitializationEvent.class),
|
||||
INIT(FMLInitializationEvent.class),
|
||||
POST_INIT(FMLPostInitializationEvent.class),
|
||||
LOAD_COMPLETE(FMLLoadCompleteEvent.class);
|
||||
|
||||
public static LoadStage getStage(@Nonnull final Class<? extends FMLStateEvent> stage)
|
||||
{
|
||||
for (final LoadStage loadStage : values())
|
||||
if (loadStage.stage == stage)
|
||||
return loadStage;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public final Class<? extends FMLStateEvent> stage;
|
||||
|
||||
LoadStage(Class<? extends FMLStateEvent> stage) {
|
||||
this.stage = stage;
|
||||
}
|
||||
|
||||
public static LoadStage getStage(@Nonnull final Class<? extends FMLStateEvent> stage) {
|
||||
for (final LoadStage loadStage : values()) if (loadStage.stage == stage) return loadStage;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,48 +1,39 @@
|
||||
package wanion.lib.module;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import cpw.mods.fml.common.event.FMLStateEvent;
|
||||
import gnu.trove.map.hash.THashMap;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public final class ModuleHandler
|
||||
{
|
||||
private final Set<Class<? extends AbstractModule>> moduleClasses = new HashSet<>();
|
||||
private final Map<AbstractModule, AbstractModule.Manager> modules = new THashMap<>();
|
||||
import cpw.mods.fml.common.event.FMLStateEvent;
|
||||
import gnu.trove.map.hash.THashMap;
|
||||
|
||||
public void startModules(final FMLStateEvent event)
|
||||
{
|
||||
final LoadStage loadStage = (event != null) ? LoadStage.getStage(event.getClass()) : null;
|
||||
if (modules.isEmpty() || loadStage == null)
|
||||
return;
|
||||
modules.entrySet().forEach(e -> {
|
||||
final AbstractModule module = e.getKey();
|
||||
if (e.getValue() == null)
|
||||
e.setValue(module.manager);
|
||||
final AbstractModule.Manager manager = e.getValue();
|
||||
if (manager.isEmpty())
|
||||
module.init();
|
||||
if (!manager.isEmpty(loadStage))
|
||||
module.start(loadStage, manager);
|
||||
});
|
||||
}
|
||||
public final class ModuleHandler {
|
||||
|
||||
public void addModule(final AbstractModule module)
|
||||
{
|
||||
Class<? extends AbstractModule> moduleClass = (module != null) ? module.getClass() : null;
|
||||
if (moduleClass == null || moduleClasses.contains(moduleClass))
|
||||
return;
|
||||
moduleClasses.add(moduleClass);
|
||||
modules.put(module, null);
|
||||
}
|
||||
}
|
||||
private final Set<Class<? extends AbstractModule>> moduleClasses = new HashSet<>();
|
||||
private final Map<AbstractModule, AbstractModule.Manager> modules = new THashMap<>();
|
||||
|
||||
public void startModules(final FMLStateEvent event) {
|
||||
final LoadStage loadStage = (event != null) ? LoadStage.getStage(event.getClass()) : null;
|
||||
if (modules.isEmpty() || loadStage == null) return;
|
||||
modules.entrySet().forEach(e -> {
|
||||
final AbstractModule module = e.getKey();
|
||||
if (e.getValue() == null) e.setValue(module.manager);
|
||||
final AbstractModule.Manager manager = e.getValue();
|
||||
if (manager.isEmpty()) module.init();
|
||||
if (!manager.isEmpty(loadStage)) module.start(loadStage, manager);
|
||||
});
|
||||
}
|
||||
|
||||
public void addModule(final AbstractModule module) {
|
||||
Class<? extends AbstractModule> moduleClass = (module != null) ? module.getClass() : null;
|
||||
if (moduleClass == null || moduleClasses.contains(moduleClass)) return;
|
||||
moduleClasses.add(moduleClass);
|
||||
modules.put(module, null);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,22 @@
|
||||
package wanion.lib.module;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface SpecifiedLoadStage
|
||||
{
|
||||
@Nonnull
|
||||
LoadStage stage();
|
||||
}
|
||||
public @interface SpecifiedLoadStage {
|
||||
|
||||
@Nonnull
|
||||
LoadStage stage();
|
||||
}
|
||||
@@ -1,96 +1,85 @@
|
||||
package wanion.lib.recipe;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import wanion.lib.common.MetaItem;
|
||||
import gnu.trove.map.TObjectCharMap;
|
||||
import gnu.trove.map.hash.THashMap;
|
||||
import gnu.trove.map.hash.TObjectCharHashMap;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import wanion.lib.common.MetaItem;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Map;
|
||||
public final class RecipeHelper {
|
||||
|
||||
public final class RecipeHelper
|
||||
{
|
||||
private RecipeHelper() {}
|
||||
private RecipeHelper() {}
|
||||
|
||||
@Nonnull
|
||||
public static Object[] rawShapeToShape(@Nonnull final Object[] objects)
|
||||
{
|
||||
return rawShapeToShape(objects, 3);
|
||||
}
|
||||
@Nonnull
|
||||
public static Object[] rawShapeToShape(@Nonnull final Object[] objects) {
|
||||
return rawShapeToShape(objects, 3);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static Object[] rawShapeToShape(@Nonnull final Object[] objects, final int squareRoot)
|
||||
{
|
||||
int f = 1;
|
||||
final char[][] almostTheShape = new char[squareRoot][squareRoot];
|
||||
final TObjectCharMap<Object> thingToCharMap = new TObjectCharHashMap<>();
|
||||
final Map<Integer, ItemStack> keyStackMap = new THashMap<>();
|
||||
boolean done = false;
|
||||
for (int x = 0; x < squareRoot && !done; x++) {
|
||||
for (int y = 0; y < squareRoot && !done; y++) {
|
||||
final int index = y * squareRoot + x;
|
||||
if ((done = !(index < objects.length)) || objects[index] == null)
|
||||
continue;
|
||||
final Object key = objects[index] instanceof ItemStack ? MetaItem.get((ItemStack) objects[index]) : objects[index];
|
||||
if (key instanceof Integer)
|
||||
keyStackMap.put((Integer) key, (ItemStack) objects[index]);
|
||||
if (thingToCharMap.containsKey(key))
|
||||
almostTheShape[y][x] = thingToCharMap.get(key);
|
||||
else
|
||||
thingToCharMap.put(key, almostTheShape[y][x] = (char) f++);
|
||||
}
|
||||
}
|
||||
final Object[] shape = new Object[squareRoot + thingToCharMap.size() * 2];
|
||||
for (int i = 0; i < squareRoot; i++)
|
||||
shape[i] = new String(almostTheShape[i]);
|
||||
int i = 0;
|
||||
for (final Object object : thingToCharMap.keySet()) {
|
||||
shape[squareRoot + (2 * i)] = thingToCharMap.get(object);
|
||||
shape[squareRoot + 1 + (2 * i++)] = (object instanceof Integer) ? keyStackMap.get(object) : object;
|
||||
}
|
||||
return shape;
|
||||
}
|
||||
@Nonnull
|
||||
public static Object[] rawShapeToShape(@Nonnull final Object[] objects, final int squareRoot) {
|
||||
int f = 1;
|
||||
final char[][] almostTheShape = new char[squareRoot][squareRoot];
|
||||
final TObjectCharMap<Object> thingToCharMap = new TObjectCharHashMap<>();
|
||||
final Map<Integer, ItemStack> keyStackMap = new THashMap<>();
|
||||
boolean done = false;
|
||||
for (int x = 0; x < squareRoot && !done; x++) {
|
||||
for (int y = 0; y < squareRoot && !done; y++) {
|
||||
final int index = y * squareRoot + x;
|
||||
if ((done = !(index < objects.length)) || objects[index] == null) continue;
|
||||
final Object key = objects[index] instanceof ItemStack ? MetaItem.get((ItemStack) objects[index])
|
||||
: objects[index];
|
||||
if (key instanceof Integer) keyStackMap.put((Integer) key, (ItemStack) objects[index]);
|
||||
if (thingToCharMap.containsKey(key)) almostTheShape[y][x] = thingToCharMap.get(key);
|
||||
else thingToCharMap.put(key, almostTheShape[y][x] = (char) f++);
|
||||
}
|
||||
}
|
||||
final Object[] shape = new Object[squareRoot + thingToCharMap.size() * 2];
|
||||
for (int i = 0; i < squareRoot; i++) shape[i] = new String(almostTheShape[i]);
|
||||
int i = 0;
|
||||
for (final Object object : thingToCharMap.keySet()) {
|
||||
shape[squareRoot + (2 * i)] = thingToCharMap.get(object);
|
||||
shape[squareRoot + 1 + (2 * i++)] = (object instanceof Integer) ? keyStackMap.get(object) : object;
|
||||
}
|
||||
return shape;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static Object[] rawShapeToShape(@Nonnull final Object[][] objects, final int squareRoot)
|
||||
{
|
||||
int f = 1;
|
||||
final char[][] almostTheShape = new char[objects.length][squareRoot];
|
||||
final TObjectCharMap<Object> thingToCharMap = new TObjectCharHashMap<>();
|
||||
final Map<Integer, ItemStack> keyStackMap = new THashMap<>();
|
||||
for (int y = 0; y < objects.length; y++) {
|
||||
final Object[] inputArray = objects[y];
|
||||
if (inputArray == null)
|
||||
continue;
|
||||
for (int x = 0; x < inputArray.length; x++) {
|
||||
final Object key = inputArray[x] instanceof ItemStack ? MetaItem.get((ItemStack) inputArray[x]) : inputArray[x];
|
||||
if (key == null)
|
||||
continue;
|
||||
else if (key instanceof Integer && !keyStackMap.containsKey(key))
|
||||
keyStackMap.put((Integer) key, (ItemStack) inputArray[x]);
|
||||
if (thingToCharMap.containsKey(key))
|
||||
almostTheShape[y][x] = thingToCharMap.get(key);
|
||||
else
|
||||
thingToCharMap.put(key, almostTheShape[y][x] = (char) f++);
|
||||
}
|
||||
}
|
||||
final Object[] shape = new Object[objects.length + thingToCharMap.size() * 2];
|
||||
for (int i = 0; i < objects.length; i++)
|
||||
shape[i] = new String(almostTheShape[i]);
|
||||
int i = 0;
|
||||
for (final Object object : thingToCharMap.keySet()) {
|
||||
shape[objects.length + (2 * i)] = thingToCharMap.get(object);
|
||||
shape[objects.length + 1 + (2 * i++)] = (object instanceof Integer) ? keyStackMap.get(object) : object;
|
||||
}
|
||||
return shape;
|
||||
}
|
||||
}
|
||||
@Nonnull
|
||||
public static Object[] rawShapeToShape(@Nonnull final Object[][] objects, final int squareRoot) {
|
||||
int f = 1;
|
||||
final char[][] almostTheShape = new char[objects.length][squareRoot];
|
||||
final TObjectCharMap<Object> thingToCharMap = new TObjectCharHashMap<>();
|
||||
final Map<Integer, ItemStack> keyStackMap = new THashMap<>();
|
||||
for (int y = 0; y < objects.length; y++) {
|
||||
final Object[] inputArray = objects[y];
|
||||
if (inputArray == null) continue;
|
||||
for (int x = 0; x < inputArray.length; x++) {
|
||||
final Object key = inputArray[x] instanceof ItemStack ? MetaItem.get((ItemStack) inputArray[x])
|
||||
: inputArray[x];
|
||||
if (key == null) continue;
|
||||
else if (key instanceof Integer && !keyStackMap.containsKey(key))
|
||||
keyStackMap.put((Integer) key, (ItemStack) inputArray[x]);
|
||||
if (thingToCharMap.containsKey(key)) almostTheShape[y][x] = thingToCharMap.get(key);
|
||||
else thingToCharMap.put(key, almostTheShape[y][x] = (char) f++);
|
||||
}
|
||||
}
|
||||
final Object[] shape = new Object[objects.length + thingToCharMap.size() * 2];
|
||||
for (int i = 0; i < objects.length; i++) shape[i] = new String(almostTheShape[i]);
|
||||
int i = 0;
|
||||
for (final Object object : thingToCharMap.keySet()) {
|
||||
shape[objects.length + (2 * i)] = thingToCharMap.get(object);
|
||||
shape[objects.length + 1 + (2 * i++)] = (object instanceof Integer) ? keyStackMap.get(object) : object;
|
||||
}
|
||||
return shape;
|
||||
}
|
||||
}
|
||||
@@ -1,107 +1,90 @@
|
||||
package wanion.lib.recipe.advanced;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import gnu.trove.map.TShortObjectMap;
|
||||
import gnu.trove.map.hash.TShortObjectHashMap;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractRecipeRegistry<R extends IAdvancedRecipe>
|
||||
{
|
||||
public final TShortObjectMap<List<R>> shapedRecipes = new TShortObjectHashMap<>();
|
||||
public final TShortObjectMap<List<R>> shapelessRecipes = new TShortObjectHashMap<>();
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final void addRecipe(@Nonnull final R recipe)
|
||||
{
|
||||
final short recipeKey = recipe.getRecipeKey();
|
||||
if (recipeKey != 0) {
|
||||
if (!shapedRecipes.containsKey(recipeKey))
|
||||
shapedRecipes.put(recipeKey, new ArrayList<>());
|
||||
shapedRecipes.get(recipeKey).add(recipe);
|
||||
} else {
|
||||
final short recipeSize = recipe.getRecipeSize();
|
||||
if (!shapelessRecipes.containsKey(recipeSize))
|
||||
shapelessRecipes.put(recipeSize, new ArrayList<>());
|
||||
shapelessRecipes.get(recipeSize).add(recipe);
|
||||
}
|
||||
}
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
|
||||
public final void removeRecipe(@Nullable final R recipe)
|
||||
{
|
||||
if (recipe == null)
|
||||
return;
|
||||
final short recipeKey = recipe.getRecipeKey();
|
||||
if (recipeKey != 0) {
|
||||
final List<R> shapedRecipeList = shapedRecipes.get(recipeKey);
|
||||
if (shapedRecipeList != null)
|
||||
shapedRecipeList.remove(recipe);
|
||||
} else {
|
||||
final List<R> shapelessRecipeList = shapelessRecipes.get(recipe.getRecipeSize());
|
||||
if (shapelessRecipeList != null)
|
||||
shapelessRecipeList.remove(recipe);
|
||||
}
|
||||
}
|
||||
import gnu.trove.map.TShortObjectMap;
|
||||
import gnu.trove.map.hash.TShortObjectHashMap;
|
||||
|
||||
public final R findMatchingRecipe(@Nonnull final InventoryCrafting inventoryCrafting)
|
||||
{
|
||||
final int root = (int) Math.sqrt(inventoryCrafting.getSizeInventory());
|
||||
int offSetX = 0, offSetY = 0, width = 0, height = 0;
|
||||
short recipeKey = 0, recipeSize = 0;
|
||||
boolean foundX = false, foundY = false;
|
||||
for (int x = 0; !foundX && x < root; x++) {
|
||||
for (int y = 0; !foundX && y < root; y++)
|
||||
if (inventoryCrafting.getStackInSlot(y * root + x) != null)
|
||||
foundX = true;
|
||||
if (foundX)
|
||||
offSetX = x;
|
||||
}
|
||||
for (int y = 0; !foundY && y < root; y++) {
|
||||
for (int x = 0; x < root; x++) {
|
||||
if (inventoryCrafting.getStackInSlot(y * root + x) != null)
|
||||
foundY = true;
|
||||
if (foundY)
|
||||
offSetY = y;
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < root; y++) {
|
||||
int x = 0;
|
||||
while (true) {
|
||||
final int actualY = offSetY + y;
|
||||
if (actualY < root) {
|
||||
final int actualX = offSetX + x++;
|
||||
if (actualX < root) {
|
||||
if (inventoryCrafting.getStackInSlot(actualY * root + actualX) == null)
|
||||
continue;
|
||||
final int xDifference = actualX - (offSetX - 1);
|
||||
final int yDifference = actualY - (offSetY - 1);
|
||||
if (xDifference > width)
|
||||
width = xDifference;
|
||||
if (yDifference > height)
|
||||
height = yDifference;
|
||||
} else break;
|
||||
} else break;
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < height; y++)
|
||||
for (int x = 0; x < width; x++)
|
||||
if (inventoryCrafting.getStackInSlot((offSetY + y) * root + (offSetX + x)) != null)
|
||||
recipeSize++;
|
||||
final List<R> recipeList = shapedRecipes.containsKey((recipeKey |= recipeSize | (width << 8) | (height << 12))) ? shapedRecipes.get(recipeKey) : shapelessRecipes.get(recipeSize);
|
||||
if (recipeList != null)
|
||||
for (R recipe : recipeList)
|
||||
if (recipe.recipeMatch(inventoryCrafting, offSetX, offSetY))
|
||||
return recipe;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public abstract class AbstractRecipeRegistry<R extends IAdvancedRecipe> {
|
||||
|
||||
public final TShortObjectMap<List<R>> shapedRecipes = new TShortObjectHashMap<>();
|
||||
public final TShortObjectMap<List<R>> shapelessRecipes = new TShortObjectHashMap<>();
|
||||
|
||||
public final void addRecipe(@Nonnull final R recipe) {
|
||||
final short recipeKey = recipe.getRecipeKey();
|
||||
if (recipeKey != 0) {
|
||||
if (!shapedRecipes.containsKey(recipeKey)) shapedRecipes.put(recipeKey, new ArrayList<>());
|
||||
shapedRecipes.get(recipeKey).add(recipe);
|
||||
} else {
|
||||
final short recipeSize = recipe.getRecipeSize();
|
||||
if (!shapelessRecipes.containsKey(recipeSize)) shapelessRecipes.put(recipeSize, new ArrayList<>());
|
||||
shapelessRecipes.get(recipeSize).add(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
public final void removeRecipe(@Nullable final R recipe) {
|
||||
if (recipe == null) return;
|
||||
final short recipeKey = recipe.getRecipeKey();
|
||||
if (recipeKey != 0) {
|
||||
final List<R> shapedRecipeList = shapedRecipes.get(recipeKey);
|
||||
if (shapedRecipeList != null) shapedRecipeList.remove(recipe);
|
||||
} else {
|
||||
final List<R> shapelessRecipeList = shapelessRecipes.get(recipe.getRecipeSize());
|
||||
if (shapelessRecipeList != null) shapelessRecipeList.remove(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
public final R findMatchingRecipe(@Nonnull final InventoryCrafting inventoryCrafting) {
|
||||
final int root = (int) Math.sqrt(inventoryCrafting.getSizeInventory());
|
||||
int offSetX = 0, offSetY = 0, width = 0, height = 0;
|
||||
short recipeKey = 0, recipeSize = 0;
|
||||
boolean foundX = false, foundY = false;
|
||||
for (int x = 0; !foundX && x < root; x++) {
|
||||
for (int y = 0; !foundX && y < root; y++)
|
||||
if (inventoryCrafting.getStackInSlot(y * root + x) != null) foundX = true;
|
||||
if (foundX) offSetX = x;
|
||||
}
|
||||
for (int y = 0; !foundY && y < root; y++) {
|
||||
for (int x = 0; x < root; x++) {
|
||||
if (inventoryCrafting.getStackInSlot(y * root + x) != null) foundY = true;
|
||||
if (foundY) offSetY = y;
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < root; y++) {
|
||||
int x = 0;
|
||||
while (true) {
|
||||
final int actualY = offSetY + y;
|
||||
if (actualY < root) {
|
||||
final int actualX = offSetX + x++;
|
||||
if (actualX < root) {
|
||||
if (inventoryCrafting.getStackInSlot(actualY * root + actualX) == null) continue;
|
||||
final int xDifference = actualX - (offSetX - 1);
|
||||
final int yDifference = actualY - (offSetY - 1);
|
||||
if (xDifference > width) width = xDifference;
|
||||
if (yDifference > height) height = yDifference;
|
||||
} else break;
|
||||
} else break;
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < height; y++) for (int x = 0; x < width; x++)
|
||||
if (inventoryCrafting.getStackInSlot((offSetY + y) * root + (offSetX + x)) != null) recipeSize++;
|
||||
final List<R> recipeList = shapedRecipes.containsKey((recipeKey |= recipeSize | (width << 8) | (height << 12)))
|
||||
? shapedRecipes.get(recipeKey)
|
||||
: shapelessRecipes.get(recipeSize);
|
||||
if (recipeList != null)
|
||||
for (R recipe : recipeList) if (recipe.recipeMatch(inventoryCrafting, offSetX, offSetY)) return recipe;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,26 @@
|
||||
package wanion.lib.recipe.advanced;
|
||||
|
||||
/*
|
||||
* Created by WanionCane(https://github.com/WanionCane).
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
* Created by WanionCane(https://github.com/WanionCane). This Source Code Form is subject to the terms of the Mozilla
|
||||
* Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
* http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import wanion.lib.common.IRemovable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public interface IAdvancedRecipe extends IRemovable
|
||||
{
|
||||
short getRecipeKey();
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
short getRecipeSize();
|
||||
import wanion.lib.common.IRemovable;
|
||||
|
||||
boolean recipeMatch(@Nonnull final InventoryCrafting inventoryCrafting, final int offSetX, final int offSetY);
|
||||
public interface IAdvancedRecipe extends IRemovable {
|
||||
|
||||
@Nonnull
|
||||
ItemStack getOutput();
|
||||
}
|
||||
short getRecipeKey();
|
||||
|
||||
short getRecipeSize();
|
||||
|
||||
boolean recipeMatch(@Nonnull final InventoryCrafting inventoryCrafting, final int offSetX, final int offSetY);
|
||||
|
||||
@Nonnull
|
||||
ItemStack getOutput();
|
||||
}
|
||||
Reference in new issue
Block a user