Skip to content

Commit

Permalink
Now with dimension dependent things!
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Aug 12, 2014
1 parent 1823bd0 commit 6c0b856
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 189 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -28,7 +28,7 @@ apply plugin: "maven"
apply plugin: "idea-utils"

group = "net.doubledoordev.craycrafting"
version = "1.0.0"
version = "1.2.0"

targetCompatibility = 1.7
sourceCompatibility = 1.7
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/net/doubledoordev/craycrafting/CrayCrafting.java
Expand Up @@ -35,11 +35,13 @@
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
import net.doubledoordev.craycrafting.network.ConfigSyncMessage;
import net.doubledoordev.craycrafting.network.RecipeMessage;
import net.doubledoordev.craycrafting.network.ResetMessage;
import net.doubledoordev.craycrafting.recipes.*;
Expand Down Expand Up @@ -99,11 +101,20 @@ public void preInit(FMLPreInitializationEvent event)
snw = NetworkRegistry.INSTANCE.newSimpleChannel(MODID);
snw.registerMessage(RecipeMessage.Handler.class, RecipeMessage.class, id++, Side.CLIENT);
snw.registerMessage(ResetMessage.Handler.class, ResetMessage.class, id++, Side.CLIENT);
snw.registerMessage(ConfigSyncMessage.Handler.class, ConfigSyncMessage.class, id++, Side.CLIENT);
}

@Mod.EventHandler()
public void eventHandler(FMLServerStoppingEvent event)
{
if (!MinecraftServer.getServer().isDedicatedServer()) RecipeRegistry.undo();
}

@Mod.EventHandler()
public void eventHandler(FMLServerStartingEvent event)
{
RecipeRegistry.setConfigFromServer(config.listType, config.list);

recipeFile = new File(DimensionManager.getCurrentSaveRootDirectory(), MODID + ".dat");
if (recipeFile.exists())
{
Expand Down Expand Up @@ -136,7 +147,7 @@ public void run()
logger.warn("Recipe timer! Resetting all the recipes!");
RecipeRegistry.undo();
RecipeRegistry.randomizeRecipes(recipeFile);
RecipeRegistry.sendPacketToAll();
if (MinecraftServer.getServer().isDedicatedServer()) RecipeRegistry.sendPacketToAll();

if (!Strings.isNullOrEmpty(config.timermessage)) MinecraftServer.getServer().getConfigurationManager().sendChatMsg(new ChatComponentText(config.timermessage));

Expand All @@ -147,7 +158,7 @@ public void run()
}

@SubscribeEvent
public void login(PlayerEvent.PlayerLoggedInEvent event)
public void loginEvent(PlayerEvent.PlayerLoggedInEvent event)
{
if (MinecraftServer.getServer().isDedicatedServer()) RecipeRegistry.sendPacketTo(event.player);
}
Expand Down
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2014, DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package net.doubledoordev.craycrafting.network;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.doubledoordev.craycrafting.recipes.RecipeRegistry;

/**
* This is a copy paste class :p
*
* @author Dries007
*/
public class ConfigSyncMessage implements IMessage
{
boolean listType;
Integer[] list;

public ConfigSyncMessage(boolean listType, Integer[] list)
{
this.listType = listType;
this.list = list;
}

public ConfigSyncMessage()
{
}

@Override
public void fromBytes(ByteBuf buf)
{
listType = buf.readBoolean();
list = new Integer[buf.readInt()];
for (int i = 0; i < list.length; i++) list[i] = buf.readInt();
}

@Override
public void toBytes(ByteBuf buf)
{
buf.writeBoolean(listType);
buf.writeInt(list.length);
for (int i : list) buf.writeInt(i);
}

public static class Handler implements IMessageHandler<ConfigSyncMessage, IMessage>
{
@Override
public IMessage onMessage(ConfigSyncMessage message, MessageContext ctx)
{
if (ctx.side.isClient()) RecipeRegistry.setConfigFromServer(message.listType, message.list);
return null;
}
}
}
19 changes: 10 additions & 9 deletions src/main/java/net/doubledoordev/craycrafting/recipes/BaseType.java
Expand Up @@ -38,6 +38,7 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -52,8 +53,8 @@ public abstract class BaseType<T extends IRecipe>
{
final Class<T> type;

final protected LinkedList<T> addedList = new LinkedList<T>();
final protected LinkedList<T> removedList = new LinkedList<T>();
final protected LinkedList<T> addedList = new LinkedList<>();
final protected LinkedList<T> removedList = new LinkedList<>();
private NBTTagList nbtList = new NBTTagList();

boolean applied;
Expand All @@ -79,7 +80,7 @@ public BaseType(Class<T> type)
* Don't forget to set special properties if required.
* Used on both sides.
*/
public abstract T getRecipeFromNBT(NBTTagCompound nbtRecipe);
public abstract T[] getRecipesFromNBT(NBTTagCompound nbtRecipe);

/**
* Check everything EXCEPT the output ItemStack.
Expand Down Expand Up @@ -114,17 +115,17 @@ public boolean accept(IRecipe recipe)
@SuppressWarnings("unchecked")
public void loadRecipesFromNBT(NBTTagCompound root) throws Exception
{
NBTTagList list = root.getTagList(getTypeName(), 10);
for (int i = 0; i < list.tagCount(); i++)
nbtList = root.getTagList(getTypeName(), 10);
for (int i = 0; i < nbtList.tagCount(); i++)
{
T newRecipe = getRecipeFromNBT(list.getCompoundTagAt(i));
addedList.add(newRecipe);
T[] newRecipes = getRecipesFromNBT(nbtList.getCompoundTagAt(i));
Collections.addAll(addedList, newRecipes);

for (IRecipe oldRecipe : (List<IRecipe>) CraftingManager.getInstance().getRecipeList())
{
if (type.isAssignableFrom(oldRecipe.getClass())) // instanceof basically
{
if (equalsExceptOutput(newRecipe, (T) oldRecipe))
if (equalsExceptOutput(newRecipes[0], (T) oldRecipe))
{
removedList.add((T) oldRecipe);
break;
Expand Down Expand Up @@ -186,7 +187,7 @@ public void applyRandomization(T recipe, ItemStack itemStack)
removedList.add(recipe);
NBTTagCompound nbtRecipe = getNBTFromRecipe(recipe, itemStack);
nbtList.appendTag(nbtRecipe);
addedList.add(getRecipeFromNBT(nbtRecipe));
Collections.addAll(addedList, getRecipesFromNBT(nbtRecipe));
}
catch (Exception e)
{
Expand Down
Expand Up @@ -30,8 +30,10 @@

package net.doubledoordev.craycrafting.recipes;

import com.google.common.collect.Sets;
import cpw.mods.fml.common.FMLCommonHandler;
import net.doubledoordev.craycrafting.CrayCrafting;
import net.doubledoordev.craycrafting.network.ConfigSyncMessage;
import net.doubledoordev.craycrafting.network.RecipeMessage;
import net.doubledoordev.craycrafting.network.ResetMessage;
import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -41,12 +43,12 @@
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;

/**
* Replaces a lot of the old helper crap
Expand All @@ -55,7 +57,13 @@
*/
public class RecipeRegistry
{
private static final ArrayList<BaseType<IRecipe>> typeList = new ArrayList<BaseType<IRecipe>>();
private static final ArrayList<BaseType<IRecipe>> typeList = new ArrayList<>();
/**
* True means that the list is a whitelist. Craycrafting only applies in the dimensions in the list.
* False means that the list is a blacklist. Craycrafting applies in all dimensions except the ones in the list
*/
public static boolean listType;
public static Set<Integer> list;

/**
* Used to read from disk and from the packet send to the server.
Expand Down Expand Up @@ -88,26 +96,43 @@ protected static void register(BaseType<IRecipe> baseType)
typeList.add(baseType);
}

public static void setConfigFromServer(boolean listTypeP, Integer[] listP)
{
listType = listTypeP;
list = Sets.newHashSet(listP);
}

public static boolean doesCrayApplyTo(World world)
{
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) System.out.println(world.provider.dimensionId);
if (listType) return list.contains(world.provider.dimensionId);
else return !list.contains(world.provider.dimensionId);
}

public static void sendPacketTo(EntityPlayer player)
{
CrayCrafting.getSnw().sendTo(new ResetMessage(), (EntityPlayerMP) player);
CrayCrafting.getSnw().sendTo(new ConfigSyncMessage(CrayCrafting.getConfig().listType, CrayCrafting.getConfig().list), (EntityPlayerMP) player);
for (BaseType<IRecipe> baseType : typeList)
{
NBTTagCompound nbtTagCompound = new NBTTagCompound();
NBTTagList nbtTagList = baseType.getNBTList();
nbtTagCompound.setTag(baseType.getTypeName(), baseType.getNBTList());
CrayCrafting.instance.logger.info("Sending " + baseType.getTypeName());
CrayCrafting.instance.logger.info("Sending " + baseType.getTypeName() + " " + nbtTagList.tagCount());
CrayCrafting.getSnw().sendTo(new RecipeMessage(nbtTagCompound), (EntityPlayerMP) player);
}
}

public static void sendPacketToAll()
{
CrayCrafting.getSnw().sendToAll(new ResetMessage());
CrayCrafting.getSnw().sendToAll(new ConfigSyncMessage(CrayCrafting.getConfig().listType, CrayCrafting.getConfig().list));
for (BaseType<IRecipe> baseType : typeList)
{
NBTTagCompound nbtTagCompound = new NBTTagCompound();
nbtTagCompound.setTag(baseType.getTypeName(), baseType.getNBTList());
CrayCrafting.instance.logger.info("Sending " + baseType.getTypeName());
NBTTagList nbtTagList = baseType.getNBTList();
nbtTagCompound.setTag(baseType.getTypeName(), nbtTagList);
CrayCrafting.instance.logger.info("Sending " + baseType.getTypeName() + " " + nbtTagList.tagCount());
CrayCrafting.getSnw().sendToAll(new RecipeMessage(nbtTagCompound));
}
}
Expand Down
Expand Up @@ -33,11 +33,13 @@
import com.google.common.collect.HashBiMap;
import net.doubledoordev.craycrafting.CrayCrafting;
import net.doubledoordev.craycrafting.util.Helper;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;

Expand Down Expand Up @@ -154,17 +156,19 @@ public NBTTagCompound getNBTFromRecipe(ShapedOreRecipe recipe, ItemStack newOutp
}
}
nbtRecipe.setTag(NBT_map, nbtMap);
nbtRecipe.setTag(NBT_output, newOutput.writeToNBT(new NBTTagCompound()));
nbtRecipe.setTag(NBT_newOutput, newOutput.writeToNBT(new NBTTagCompound()));
nbtRecipe.setTag(NBT_oldOutput, recipe.getRecipeOutput().writeToNBT(new NBTTagCompound()));
nbtRecipe.setBoolean(NBT_mirror, ShapedOreRecipe_mirror.getBoolean(recipe));

return nbtRecipe;
}

@Override
public ShapedOreRecipe getRecipeFromNBT(NBTTagCompound nbtRecipe)
public ShapedOreRecipe[] getRecipesFromNBT(NBTTagCompound nbtRecipe)
{
ArrayList<Object> input = new ArrayList<Object>(); // Becomes entire recipe input
ItemStack output = ItemStack.loadItemStackFromNBT(nbtRecipe.getCompoundTag(NBT_output));
ItemStack newOutput = ItemStack.loadItemStackFromNBT(nbtRecipe.getCompoundTag(NBT_newOutput));
ItemStack oldOutput = ItemStack.loadItemStackFromNBT(nbtRecipe.getCompoundTag(NBT_oldOutput));

NBTTagList inputs = nbtRecipe.getTagList(NBT_input, 8);
for (int i = 0; i < inputs.tagCount(); i++) input.add(inputs.getStringTagAt(i));
Expand All @@ -178,7 +182,23 @@ public ShapedOreRecipe getRecipeFromNBT(NBTTagCompound nbtRecipe)
else input.add(ItemStack.loadItemStackFromNBT((NBTTagCompound) entry));
}

return new ShapedOreRecipe(output, input.toArray()).setMirrored(nbtRecipe.getBoolean(NBT_mirror));
return new ShapedOreRecipe[] { new DimBasedShapedOreRecipe(true, newOutput, input.toArray()).setMirrored(nbtRecipe.getBoolean(NBT_mirror)), new DimBasedShapedOreRecipe(false, oldOutput, input.toArray()).setMirrored(nbtRecipe.getBoolean(NBT_mirror))};
}

public static class DimBasedShapedOreRecipe extends ShapedOreRecipe
{
boolean isCrayRecipe;
public DimBasedShapedOreRecipe(boolean isCrayRecipe, ItemStack newOutput, Object[] objects)
{
super(newOutput, objects);
this.isCrayRecipe = isCrayRecipe;
}

@Override
public boolean matches(InventoryCrafting inv, World world)
{
return ((RecipeRegistry.doesCrayApplyTo(world) && isCrayRecipe) || (!RecipeRegistry.doesCrayApplyTo(world) && !isCrayRecipe)) && super.matches(inv, world);
}
}

@Override
Expand Down

0 comments on commit 6c0b856

Please sign in to comment.