From 5618b41ca02e17cd9e235f1465add5b882cbfa87 Mon Sep 17 00:00:00 2001 From: gottsch Date: Sun, 7 Jan 2018 11:55:21 -0500 Subject: [PATCH] Updates -Added AbstractModContainerBlock, AbstractModTileEntity. -Added getUpateURL() to IMod.\n-Renamed getVerisionURL to getVersionURL in IMod. -Added readFromNBT() and writeToNBT() methods to ICoords/Coords class. -VersionChecker now first attempts to use the Forge update.json to track versions. --- GottschCore1.12.2/build.gradle | 3 +- GottschCore1.12.2/gradle.properties | 4 +- .../gottschcore/GottschCore.java | 11 +- .../block/AbstractModContainerBlock.java | 134 ++++++++++++++++++ .../eventhandler/LoginEventHandler.java | 21 ++- .../gottschcore/mod/AbstractMod.java | 11 +- .../gottschcore/mod/IMod.java | 24 +++- .../tileentity/AbstractModTileEntity.java | 54 +++++++ .../gottschcore/version/VersionChecker.java | 85 +++++++++++ GottschCore1.12.2/src/resources/mcmod.info | 2 +- GottschCore1.12.2/update.json | 8 +- .../gottschcore/version/VersionChecker.java | 8 -- 12 files changed, 341 insertions(+), 24 deletions(-) create mode 100644 GottschCore1.12.2/src/com/someguyssoftware/gottschcore/block/AbstractModContainerBlock.java create mode 100644 GottschCore1.12.2/src/com/someguyssoftware/gottschcore/tileentity/AbstractModTileEntity.java diff --git a/GottschCore1.12.2/build.gradle b/GottschCore1.12.2/build.gradle index 59dcd8a..75f1812 100644 --- a/GottschCore1.12.2/build.gradle +++ b/GottschCore1.12.2/build.gradle @@ -23,7 +23,8 @@ ext { // revision = git.head().abbreviatedId } -version = "mc${mc_version}-v${mod_version}-${git.head().abbreviatedId}" +//version = "mc${mc_version}-v${mod_version}-${git.head().abbreviatedId}" +version = "mc${mc_version}-f${forge_version}-v${mod_version}" group = "${package_group}" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "${mod_name}" diff --git a/GottschCore1.12.2/gradle.properties b/GottschCore1.12.2/gradle.properties index 8fcd153..12d6b76 100644 --- a/GottschCore1.12.2/gradle.properties +++ b/GottschCore1.12.2/gradle.properties @@ -4,10 +4,10 @@ org.gradle.jvmargs=-Xmx3G mod_name=GottschCore package_group=someguyssoftware.gottschcore -mod_version=1.1.2 +mod_version=1.2.0 mc_version=1.12.2 -forge_version=14.23.1.2556 +forge_version=14.23.1.2555 mappings_version=snapshot_20171003 diff --git a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/GottschCore.java b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/GottschCore.java index 1766b92..fb9c3dd 100644 --- a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/GottschCore.java +++ b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/GottschCore.java @@ -45,8 +45,8 @@ public class GottschCore extends AbstractMod { // constants public static final String MODID = "gottschcore"; protected static final String NAME = "GottschCore"; - protected static final String VERSION = "1.1.2"; - public static final String UPDATE_JSON_URL = "https://raw.githubusercontent.com/gottsch/gottsch-minecraft-GottschCore/master/GottschCore1.12/update.json"; + protected static final String VERSION = "1.2.0"; + public static final String UPDATE_JSON_URL = "https://raw.githubusercontent.com/gottsch/gottsch-minecraft-GottschCore/master/GottschCore1.12.2/update.json"; // TODO [back-burner]add a message file (messages.json) to check from.... global message and mod specific messages @@ -126,6 +126,11 @@ public IMod getInstance() { return GottschCore.instance; } + @Override + public String getUpdateURL() { + return GottschCore.UPDATE_JSON_URL; + } + /* (non-Javadoc) * @see com.someguyssoftware.gottschcore.IMod#getConfig() */ @@ -146,7 +151,7 @@ public BuildVersion getMinecraftVersion() { * @see com.someguyssoftware.gottschcore.IMod#getVerisionURL() */ @Override - public String getVerisionURL() { + public String getVersionURL() { return GottschCore.VERSION_URL; } diff --git a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/block/AbstractModContainerBlock.java b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/block/AbstractModContainerBlock.java new file mode 100644 index 0000000..0921abb --- /dev/null +++ b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/block/AbstractModContainerBlock.java @@ -0,0 +1,134 @@ +/** + * + */ +package com.someguyssoftware.gottschcore.block; + +import javax.annotation.Nullable; + +import net.minecraft.block.ITileEntityProvider; +import net.minecraft.block.material.MapColor; +import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Enchantments; +import net.minecraft.init.Items; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.stats.StatList; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumBlockRenderType; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IWorldNameable; +import net.minecraft.world.World; + +/** + * This class replaces BlockContainer so that it can extend ModBlock + * @author Mark Gottschling onJan 2, 2018 + * + */ +public abstract class AbstractModContainerBlock extends ModBlock implements ITileEntityProvider { + + /** + * + * @param modID + * @param name + * @param material + */ + public AbstractModContainerBlock(String modID, String name, Material material) { + this(modID, name, material, material.getMaterialMapColor()); + } + + /** + * + * @param modID + * @param name + * @param material + * @param mapColor + */ + public AbstractModContainerBlock(String modID, String name, Material material, MapColor mapColor) { + super(modID, name, material, mapColor); + this.hasTileEntity = true; + } + + /** + * + * @param worldIn + * @param pos + * @param facing + * @return + */ + protected boolean isInvalidNeighbor(World worldIn, BlockPos pos, EnumFacing facing) { + return worldIn.getBlockState(pos.offset(facing)).getMaterial() == Material.CACTUS; + } + + /** + * + * @param worldIn + * @param pos + * @return + */ + protected boolean hasInvalidNeighbor(World worldIn, BlockPos pos) { + return this.isInvalidNeighbor(worldIn, pos, EnumFacing.NORTH) || this.isInvalidNeighbor(worldIn, pos, EnumFacing.SOUTH) || this.isInvalidNeighbor(worldIn, pos, EnumFacing.WEST) || this.isInvalidNeighbor(worldIn, pos, EnumFacing.EAST); + } + + /** + * The type of render function called. MODEL for mixed tesr and static model, MODELBLOCK_ANIMATED for TESR-only, + * LIQUID for vanilla liquids, INVISIBLE to skip all rendering. + * Since this class is abstract, default to no rendering. + */ + public EnumBlockRenderType getRenderType(IBlockState state) { + return EnumBlockRenderType.INVISIBLE; + } + + /** + * Called serverside after this block is replaced with another in Chunk, but before the Tile Entity is updated + */ + public void breakBlock(World worldIn, BlockPos pos, IBlockState state) { + super.breakBlock(worldIn, pos, state); + worldIn.removeTileEntity(pos); + } + + /** + * Spawns the block's drops in the world. By the time this is called the Block has possibly been set to air via + * Block.removedByPlayer + */ + public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack) { + if (te instanceof IWorldNameable && ((IWorldNameable)te).hasCustomName()) { + player.addStat(StatList.getBlockStats(this)); + player.addExhaustion(0.005F); + + if (worldIn.isRemote) { + return; + } + + int i = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack); + Item item = this.getItemDropped(state, worldIn.rand, i); + + if (item == Items.AIR) { + return; + } + + ItemStack itemstack = new ItemStack(item, this.quantityDropped(worldIn.rand)); + itemstack.setStackDisplayName(((IWorldNameable)te).getName()); + spawnAsEntity(worldIn, pos, itemstack); + } + else { + super.harvestBlock(worldIn, player, pos, state, (TileEntity)null, stack); + } + } + + /** + * Called on server when World#addBlockEvent is called. If server returns true, then also called on the client. On + * the Server, this may perform additional changes to the world, like pistons replacing the block with an extended + * base. On the client, the update may involve replacing tile entities or effects such as sounds or particles + */ + @SuppressWarnings("deprecation") + @Override + public boolean eventReceived(IBlockState state, World worldIn, BlockPos pos, int id, int param) { + super.eventReceived(state, worldIn, pos, id, param); + TileEntity tileentity = worldIn.getTileEntity(pos); + return tileentity == null ? false : tileentity.receiveClientEvent(id, param); + } +} diff --git a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/eventhandler/LoginEventHandler.java b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/eventhandler/LoginEventHandler.java index af8af2e..c9b8ca6 100644 --- a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/eventhandler/LoginEventHandler.java +++ b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/eventhandler/LoginEventHandler.java @@ -3,8 +3,6 @@ */ package com.someguyssoftware.gottschcore.eventhandler; -import net.minecraft.util.text.TextFormatting; - import com.someguyssoftware.gottschcore.GottschCore; import com.someguyssoftware.gottschcore.config.IConfig; import com.someguyssoftware.gottschcore.mod.IMod; @@ -12,6 +10,7 @@ import com.someguyssoftware.gottschcore.version.VersionChecker; import net.minecraft.util.text.TextComponentString; +import net.minecraft.util.text.TextFormatting; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.PlayerEvent; @@ -63,17 +62,31 @@ public void checkVersionOnLogIn(PlayerEvent.PlayerLoggedInEvent event) { // get the latest version recorded in the config BuildVersion configVersion = new BuildVersion(mod.getConfig().getLatestVersion()); - - boolean isCurrent = VersionChecker.checkVersion(mod.getModLatestVersion(), new BuildVersion(mod.getClass().getAnnotation(Mod.class).version())); boolean isConfigCurrent = VersionChecker.checkVersion(mod.getModLatestVersion(), configVersion); boolean isReminderOn = mod.getConfig().isLatestVersionReminder(); + boolean isCurrent = false; + // update the config to the latest client version if it is not set already if (!isConfigCurrent) { // update config mod.getConfig().setProperty(IConfig.MOD_CATEGORY, "latestVersion", mod.getModLatestVersion().toString()); // turn the reminder back on for the latest version mod.getConfig().setProperty(IConfig.MOD_CATEGORY, "latestVersionReminder", true); } + + if (mod.getUpdateURL() != null && !mod.getUpdateURL().equals("")) { + // use Forge update method + try { + isCurrent = VersionChecker.checkVersionUsingForge(mod.getModLatestVersion(), new BuildVersion(mod.getClass().getAnnotation(Mod.class).version())); + } + catch(Exception e) { + GottschCore.logger.error("Unable to determine version using Forge:", e); + isCurrent = VersionChecker.checkVersion(mod.getModLatestVersion(), new BuildVersion(mod.getClass().getAnnotation(Mod.class).version())); + } + } + else { + isCurrent = VersionChecker.checkVersion(mod.getModLatestVersion(), new BuildVersion(mod.getClass().getAnnotation(Mod.class).version())); + } if (!isCurrent && isReminderOn) { StringBuilder builder = new StringBuilder(); diff --git a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/mod/AbstractMod.java b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/mod/AbstractMod.java index 77c6b3f..7db299a 100644 --- a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/mod/AbstractMod.java +++ b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/mod/AbstractMod.java @@ -56,7 +56,16 @@ public void postInit(FMLPostInitializationEvent event) { // check config if version check is enabled if (getConfig().isEnableVersionChecker()) { // get the latest version from the website - setModLatestVersion(VersionChecker.getVersion(getVerisionURL(), getMinecraftVersion())); + BuildVersion buildVersion = null; + // check if updateURL is set. if so, use the forge versioning to get the latest version + if (this.getUpdateURL() != null && !this.getUpdateURL().equals("")) { + buildVersion = VersionChecker.getVersionUsingForge(this); + } + + if (buildVersion == null || buildVersion == BuildVersion.EMPTY_VERSION) { + buildVersion = VersionChecker.getVersion(getVersionURL(), getMinecraftVersion()); + } + setModLatestVersion(buildVersion); } } diff --git a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/mod/IMod.java b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/mod/IMod.java index 6ce65b4..f2cf9c6 100644 --- a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/mod/IMod.java +++ b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/mod/IMod.java @@ -34,7 +34,22 @@ public interface IMod { */ public BuildVersion getMinecraftVersion(); - public String getVerisionURL(); + /** + * By default calls deprecated getVerisionURL() to maintain backwards-compatibility. + * @return + */ + public default String getVersionURL() { + return getVerisionURL(); + } + + /** + * @deprecated use getVersionURL() instead. + * @return + */ + @Deprecated() + public default String getVerisionURL() { + return null; + } /** * Get the instance of the mod @@ -60,4 +75,11 @@ public interface IMod { */ public String getVersion(); + + /** + * The Forge update URL of the mod. + */ + public default String getUpdateURL() { + return null; + } } diff --git a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/tileentity/AbstractModTileEntity.java b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/tileentity/AbstractModTileEntity.java new file mode 100644 index 0000000..0b02431 --- /dev/null +++ b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/tileentity/AbstractModTileEntity.java @@ -0,0 +1,54 @@ +/** + * + */ +package com.someguyssoftware.gottschcore.tileentity; + +import javax.annotation.Nullable; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.play.server.SPacketUpdateTileEntity; +import net.minecraft.tileentity.TileEntity; + +/** + * @author Mark Gottschling onJan 3, 2018 + * + */ +public abstract class AbstractModTileEntity extends TileEntity { + + /** + * + */ + @Override + @Nullable + public SPacketUpdateTileEntity getUpdatePacket() { + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + writeToNBT(nbtTagCompound); + int metadata = getBlockMetadata(); + return new SPacketUpdateTileEntity(this.pos, metadata, nbtTagCompound); + } + + /** + * + */ + @Override + public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) { + readFromNBT(pkt.getNbtCompound()); + } + + /* Creates a tag containing the TileEntity information, used by vanilla to transmit from server to client + */ + @Override + public NBTTagCompound getUpdateTag() { + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + writeToNBT(nbtTagCompound); + return nbtTagCompound; + } + + /* Populates this TileEntity with information from the tag, used by vanilla to transmit from server to client + */ + @Override + public void handleUpdateTag(NBTTagCompound tag) { + this.readFromNBT(tag); + } +} diff --git a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/version/VersionChecker.java b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/version/VersionChecker.java index 1fd2d4f..d05228f 100644 --- a/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/version/VersionChecker.java +++ b/GottschCore1.12.2/src/com/someguyssoftware/gottschcore/version/VersionChecker.java @@ -11,13 +11,19 @@ import java.nio.charset.Charset; import java.util.Collection; import java.util.List; +import java.util.Map; import org.apache.commons.io.IOUtils; +import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import com.someguyssoftware.gottschcore.GottschCore; +import com.someguyssoftware.gottschcore.mod.IMod; + +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.versioning.ComparableVersion; /** * @author Mark Gottschling on Apr 30, 2015 @@ -85,10 +91,67 @@ public static BuildVersion getVersion(String url, BuildVersion mv) { return BuildVersion.EMPTY_VERSION; } + /** + * TODO instead of having 2 methods, should have 2 implementations of IVersionRetriever + * @param updateURL + * @return + */ + public static BuildVersion getVersionUsingForge(IMod mod) { + BuildVersion buildVersion = BuildVersion.EMPTY_VERSION; + + URL url = null; + try { + url = new URL(mod.getUpdateURL()); + } catch (MalformedURLException e) { + GottschCore.logger.warn("Unable to open updateURL:" + mod.getUpdateURL(), e); + return buildVersion; + } + + GottschCore.logger.info("[{}] Starting version check at {}", mod.getId(), url.toString()); + + String data = null; + try { + InputStream con = url.openStream(); + data = new String(ByteStreams.toByteArray(con), "UTF-8"); + con.close(); + } + catch(Exception e) { + GottschCore.logger.warn("Unexpected expection in data stream: ", e); + return buildVersion; + } + GottschCore.logger.debug("[{}] Received version check data:\n{}", mod.getId(), data); + + try { + @SuppressWarnings("unchecked") + Map json = new Gson().fromJson(data, Map.class); + @SuppressWarnings("unchecked") + Map promos = (Map)json.get("promos"); + + String rec = promos.get(MinecraftForge.MC_VERSION + "-recommended"); + String lat = promos.get(MinecraftForge.MC_VERSION + "-latest"); + + if (rec != null) { + buildVersion = new BuildVersion(rec); + } + else if (lat != null) { + buildVersion = new BuildVersion(lat); + } + } + catch(JsonSyntaxException e) { + GottschCore.logger.warn("Bad JSON Syntax: " + data, e); + } + catch(Exception e) { + GottschCore.logger.warn("Unexpected expection processing json: " + data, e); + } + + return buildVersion; + } + /** * * @param version the provided version to check against * @param modVersion the mod's current version + * * @return * @since 2.0 */ @@ -109,4 +172,26 @@ else if (version.getMinor() == modVersion.getMinor()) { } return true; } + + /** + * + * @param version + * @param modVersion + * @return + * @throws Exception + * @since 2.0 + */ + public static boolean checkVersionUsingForge(BuildVersion version, BuildVersion modVersion) throws Exception { + if (version == null || modVersion == null) return true; + + ComparableVersion current = new ComparableVersion(modVersion.toString()); + ComparableVersion recommended = new ComparableVersion(version.toString()); + + int diff = recommended.compareTo(current); + + if (diff > 0) + return false; + + return true; + } } diff --git a/GottschCore1.12.2/src/resources/mcmod.info b/GottschCore1.12.2/src/resources/mcmod.info index b66a7fe..8f48de9 100644 --- a/GottschCore1.12.2/src/resources/mcmod.info +++ b/GottschCore1.12.2/src/resources/mcmod.info @@ -3,7 +3,7 @@ "modid": "gottschcore", "name": "GottschCore", "description": "Base API for all my mods.", - "version": "1.1.2", + "version": "1.2.0", "mcversion": "1.12.2", "url": "", "updateUrl": "", diff --git a/GottschCore1.12.2/update.json b/GottschCore1.12.2/update.json index 98302c9..05bfa66 100644 --- a/GottschCore1.12.2/update.json +++ b/GottschCore1.12.2/update.json @@ -1,15 +1,17 @@ { "homepage": "http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/2604013-gottschs-mods", "promos": { - "1.12.2-latest": "1.1.2", - "1.12.2-recommended": "1.1.2" + "1.12.2-latest": "1.2.0", + "1.12.2-recommended": "1.2.0" }, "1.12.2": { "1.0.0": "-Initial release.", "1.0.1": "-Set modEnabled config property with default value.\n-Removed latestVersion property from AbstractMod forcing concrete classes to implement.", "1.1.0": "-Added new builder classes that build wrapper classes for common Minecraft classes (ex. Block, Item, ItemSword, etc).\nBug fixes.", "1.1.1": "-Added additional util methods.\n-Fixed Cube and Coords classes and WorldInfo methods.", - "1.1.2": "-Added additional WorldInfo methods." + "1.1.2": "-Added additional WorldInfo methods.", + "1.2.0": "-Added AbstractModContainerBlock, AbstractModTileEntity.\nAdded getUpateURL() to IMod.\n-Renamed getVerisionURL to getVersionURL in IMod.\n-Added readFromNBT() and writeToNBT() methods to ICoords/Coords class.\n-VersionChecker now first attempts to use the Forge update.json to track versions." + } } \ No newline at end of file diff --git a/GottschCore1.12/src/com/someguyssoftware/gottschcore/version/VersionChecker.java b/GottschCore1.12/src/com/someguyssoftware/gottschcore/version/VersionChecker.java index 78b6238..d05228f 100644 --- a/GottschCore1.12/src/com/someguyssoftware/gottschcore/version/VersionChecker.java +++ b/GottschCore1.12/src/com/someguyssoftware/gottschcore/version/VersionChecker.java @@ -3,13 +3,6 @@ */ package com.someguyssoftware.gottschcore.version; -import static net.minecraftforge.common.ForgeVersion.Status.AHEAD; -import static net.minecraftforge.common.ForgeVersion.Status.BETA; -import static net.minecraftforge.common.ForgeVersion.Status.BETA_OUTDATED; -import static net.minecraftforge.common.ForgeVersion.Status.OUTDATED; -import static net.minecraftforge.common.ForgeVersion.Status.PENDING; -import static net.minecraftforge.common.ForgeVersion.Status.UP_TO_DATE; - import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Type; @@ -30,7 +23,6 @@ import com.someguyssoftware.gottschcore.mod.IMod; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.common.ForgeVersion.Status; import net.minecraftforge.fml.common.versioning.ComparableVersion; /**