Skip to content

Commit

Permalink
added rotate timer
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Aug 12, 2014
1 parent 74e9561 commit 1823bd0
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/main/java/net/doubledoordev/craycrafting/CrayCrafting.java
Expand Up @@ -30,6 +30,7 @@

package net.doubledoordev.craycrafting;

import com.google.common.base.Strings;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
Expand All @@ -42,15 +43,17 @@
import net.doubledoordev.craycrafting.network.RecipeMessage;
import net.doubledoordev.craycrafting.network.ResetMessage;
import net.doubledoordev.craycrafting.recipes.*;
import net.doubledoordev.lib.DevPerks;
import net.doubledoordev.craycrafting.util.Config;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import static net.doubledoordev.craycrafting.util.Constants.MODID;

Expand All @@ -63,27 +66,29 @@ public class CrayCrafting
@Mod.Instance(MODID)
public static CrayCrafting instance;

private boolean debug = false;
public Logger logger;
private SimpleNetworkWrapper snw;
private Config config;
private File recipeFile;

public static SimpleNetworkWrapper getSnw()
{
return instance.snw;
}

public static Config getConfig()
{
return instance.config;
}

@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event)
{
logger = event.getModLog();
MinecraftForge.EVENT_BUS.register(this);
FMLCommonHandler.instance().bus().register(this);

Configuration configuration = new Configuration(event.getSuggestedConfigurationFile());

debug = configuration.getBoolean("debug", MODID, debug, "Enable extra debug output.");
if (configuration.getBoolean("sillyness", MODID, true, "Disable sillyness only if you want to piss off the developers XD")) MinecraftForge.EVENT_BUS.register(new DevPerks(debug));
if (configuration.hasChanged()) configuration.save();
config = new Config(event.getSuggestedConfigurationFile());

new ShapedRecipesType();
new ShapelessRecipesType();
Expand All @@ -99,7 +104,7 @@ public void preInit(FMLPreInitializationEvent event)
@Mod.EventHandler()
public void eventHandler(FMLServerStartingEvent event)
{
File recipeFile = new File(DimensionManager.getCurrentSaveRootDirectory(), MODID + ".dat");
recipeFile = new File(DimensionManager.getCurrentSaveRootDirectory(), MODID + ".dat");
if (recipeFile.exists())
{
try
Expand All @@ -115,6 +120,30 @@ public void eventHandler(FMLServerStartingEvent event)
{
RecipeRegistry.randomizeRecipes(recipeFile);
}

if (config.timer > 0) setupTimer();
}

public void setupTimer()
{
if (config.timer >= 1)
{
new Timer(MODID + "-Timer").schedule(new TimerTask()
{
@Override
public void run()
{
logger.warn("Recipe timer! Resetting all the recipes!");
RecipeRegistry.undo();
RecipeRegistry.randomizeRecipes(recipeFile);
RecipeRegistry.sendPacketToAll();

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

setupTimer();
}
}, 1000 * 60 * config.timer);
}
}

@SubscribeEvent
Expand Down
Expand Up @@ -100,6 +100,18 @@ public static void sendPacketTo(EntityPlayer player)
}
}

public static void sendPacketToAll()
{
CrayCrafting.getSnw().sendToAll(new ResetMessage());
for (BaseType<IRecipe> baseType : typeList)
{
NBTTagCompound nbtTagCompound = new NBTTagCompound();
nbtTagCompound.setTag(baseType.getTypeName(), baseType.getNBTList());
CrayCrafting.instance.logger.info("Sending " + baseType.getTypeName());
CrayCrafting.getSnw().sendToAll(new RecipeMessage(nbtTagCompound));
}
}

/**
* Used on both sides.
*/
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/net/doubledoordev/craycrafting/util/Config.java
@@ -0,0 +1,70 @@
/*
* 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.util;

import net.doubledoordev.lib.DevPerks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;

import java.io.File;

import static net.doubledoordev.craycrafting.util.Constants.MODID;

/**
* @author Dries007
*/
public class Config
{
private final File file;
private Configuration configuration;
public boolean debug = false;
public int timer = 0;
public String timermessage = "[CrayCrafting] Recipes have been rotated!";

public Config(File file)
{
this.file = file;
configuration = new Configuration(file);

debug = configuration.getBoolean("debug", MODID, debug, "Enable extra debug output.");
if (configuration.getBoolean("sillyness", MODID, true, "Disable sillyness only if you want to piss off the developers XD")) MinecraftForge.EVENT_BUS.register(new DevPerks(debug));

timer = configuration.get(MODID, "resetTimer", timer, "For extra evil, this timer rotates the crafting every X minutes. 0 for disable.").getInt();
timermessage = configuration.get(MODID, "timermessage", timermessage, "Message to be send to all players on timer. Empty = no message").getString();

save();
}

public void save()
{
if (configuration.hasChanged()) configuration.save();
}
}

0 comments on commit 1823bd0

Please sign in to comment.