Java tutorial
/* * SpawnChecker. * * (c) 2014 alalwww * https://github.com/alalwww * * This mod is distributed under the terms of the Minecraft Mod Public License 1.0, or MMPL. * Please check the contents of the license located in http://www.mod-buildcraft.com/MMPL-1.0.txt * * ?? MOD ??Minecraft Mod Public License (MMPL) 1.0 ??????????? * ??????????? http://www.mod-buildcraft.com/MMPL-1.0.txt */ package net.awairo.mcmod.common.v1.util.config; import static com.google.common.base.Preconditions.*; import java.util.Set; import java.util.concurrent.ConcurrentMap; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import org.apache.logging.log4j.Logger; import net.minecraft.client.Minecraft; import net.minecraft.world.World; import net.minecraftforge.common.config.Configuration; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent; import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; /** * ??TickEvent?????. * * @author alalwww * @version 1.0 */ public class ConfigHolder { /** ????. */ protected final ConcurrentMap<Class<? extends ConfigCategory>, ConfigCategory> holder; /** ??Save?Forge?Configuration??. */ protected final Set<Configuration> forgeConfigSetTemp; private Logger logger; private long interval = 3000L; /** * Constructor. */ protected ConfigHolder() { holder = Maps.newConcurrentMap(); forgeConfigSetTemp = Sets.newHashSet(); } /** * Constructor. * * @param logger */ protected ConfigHolder(Logger logger) { this(); setLogger(logger); } /** * ??????????. * * @return */ public ImmutableMap<Class<? extends ConfigCategory>, ConfigCategory> copy() { return ImmutableMap.copyOf(holder); } /** * ??????. * * <p> * ?????????????????null?? * </p> * * @param configCategory * @return null ?????????? */ @SuppressWarnings("unchecked") public <T extends ConfigCategory> T add(T configCategory) { return (T) holder.put(configCategory.getClass(), configCategory); } /** * ??{@link ConfigCategory}??. * * @param keyClass {@link ConfigCategory} * @return ????????null */ @SuppressWarnings("unchecked") public <T extends ConfigCategory> T get(Class<T> keyClass) { return (T) holder.get(checkNotNull(keyClass, "keyClass")); } /** * ???????????. * * @param interval */ protected void setInterval(long interval) { checkArgument(interval > 0, "%s is negative value.", interval); this.interval = interval; } /** * ???. * * @param logger */ protected void setLogger(Logger logger) { this.logger = checkNotNull(logger, "logger"); } /** * ??????????. */ protected void saveConfigIfChanged() { // ? for (ConfigCategory category : holder.values()) { if (category.isSettingChanged()) { forgeConfigSetTemp.add(category.config.forgeConfig); category.clearChangedFlag(); } } // ??????? if (forgeConfigSetTemp.isEmpty()) return; // ?????? if (logger != null) logger.info("saving configuration."); // ?????? for (Configuration forgeConfig : forgeConfigSetTemp) forgeConfig.save(); forgeConfigSetTemp.clear(); } // ------------------------------------------------ /** * ??Tick?. * * @return */ public Object newTickEventListener() { return new TickEventListener(); } /** * * * @author alalwww * @version 1.0 */ public final class TickEventListener { /** . */ private long nextSaveTime; private World world; private TickEventListener() { } /** * ??Tick?. * * @param event ClientTick */ @SubscribeEvent @SideOnly(Side.CLIENT) public void handleClientTick(ClientTickEvent event) { autoSave(event); } @SubscribeEvent @SideOnly(Side.SERVER) public void handleServerTick(ServerTickEvent event) { autoSave(event); } private void autoSave(TickEvent event) { if (event.phase != Phase.END) return; // ??????????? if (Minecraft.getMinecraft().theWorld != world) { if (world != null) saveConfigIfChanged(); world = Minecraft.getMinecraft().theWorld; return; } // ????????? final long now = Minecraft.getSystemTime(); if (now < nextSaveTime) return; nextSaveTime = now + interval; saveConfigIfChanged(); } } }