Skip to content

Commit

Permalink
Some minor tweaks
Browse files Browse the repository at this point in the history
- Git forgot to add "def" in the buildscript, leading to a broken commit
- Properly registered the chest data serializer for entities
- Tiny upgrade performance enhancement (chunks don't need to be forcefully saved to disk on the next tick; the renderer will do its work regardless)
  • Loading branch information
T145 committed Jun 1, 2019
1 parent be126ad commit 4f2a8dd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repositories {
version = "${gitVersion()}+mc${project.minecraft_version}"
group = mod_group
archivesBaseName = mod_name
forge = "${project.minecraft_version}-${project.forge_version}"
def forge = "${project.minecraft_version}-${project.forge_version}"

minecraft {
runDir = 'run'
Expand Down
39 changes: 23 additions & 16 deletions src/main/java/T145/metalchests/core/MetalChests.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import T145.metalchests.network.PacketHandler;
import T145.metalchests.network.client.MessageSyncMetalChest;
import T145.metalchests.tiles.TileMetalChest;
import T145.metalchests.tiles.TileMetalHungrySortingChest;
import T145.metalchests.tiles.TileMetalSortingChest;
import cofh.core.init.CoreEnchantments;
import cofh.core.util.helpers.MathHelper;
Expand All @@ -70,7 +71,6 @@
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializer;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumHand;
import net.minecraft.util.IStringSerializable;
Expand Down Expand Up @@ -107,10 +107,12 @@
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.common.registry.EntityEntryBuilder;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.registries.DataSerializerEntry;
import net.minecraftforge.registries.IForgeRegistry;

@Mod(modid = RegistryMC.ID, name = RegistryMC.NAME, version = MetalChests.VERSION, updateJSON = MetalChests.UPDATE_JSON,
Expand Down Expand Up @@ -181,14 +183,14 @@ public ChestType copyValue(ChestType value) {
};

@SubscribeEvent
public static void onConfigChanged(OnConfigChangedEvent event) {
public static void metalchests$updateConfig(OnConfigChangedEvent event) {
if (event.getModID().equals(RegistryMC.ID)) {
ConfigManager.sync(RegistryMC.ID, Config.Type.INSTANCE);
}
}

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
public void metalchests$preInit(FMLPreInitializationEvent event) {
ModMetadata meta = event.getModMetadata();
meta.authorList.add("T145");
meta.autogenerated = false;
Expand All @@ -200,7 +202,7 @@ public void preInit(FMLPreInitializationEvent event) {
meta.url = "https://github.com/T145/metalchests";
meta.useDependencyInformation = false;
meta.version = VERSION;
DataSerializers.registerSerializer(CHEST_TYPE);
PacketHandler.registerMessages();
}

private void registerFixes(DataFixer fixer, Class tileClass) {
Expand All @@ -212,7 +214,7 @@ private void registerEntityFixes(DataFixer fixer, Class entityClass) {
}

@EventHandler
public void init(FMLInitializationEvent event) {
public void metalchests$init(FMLInitializationEvent event) {
NetworkRegistry.INSTANCE.registerGuiHandler(MetalChests.instance, new GuiHandler());

DataFixer fixer = FMLCommonHandler.instance().getDataFixer();
Expand All @@ -226,17 +228,15 @@ public void init(FMLInitializationEvent event) {
}

if (ConfigMC.hasThaumcraft() && ConfigMC.hasRefinedRelocation()) {
registerFixes(fixer, TileMetalSortingChest.class);
registerFixes(fixer, TileMetalHungrySortingChest.class);
}

PacketHandler.registerMessages();
}

@EventHandler
public void postInit(FMLPostInitializationEvent event) {
for (ItemStack stack : OreDictionary.getOres("chestWood")) {
public void metalchests$postInit(FMLPostInitializationEvent event) {
OreDictionary.getOres("chestWood").forEach(stack -> {
UpgradeRegistry.registerChest(Block.getBlockFromItem(stack.getItem()), BlocksMC.METAL_CHEST);
}
});

UpgradeRegistry.registerChest(Blocks.TRAPPED_CHEST, BlocksMC.METAL_CHEST);
}
Expand All @@ -254,15 +254,22 @@ public static void registerItemBlock(IForgeRegistry<Item> registry, Block block,
}

@SubscribeEvent
public static void registerBlocks(final RegistryEvent.Register<Block> event) {
public static void metalchests$registerSerializers(final RegistryEvent.Register<DataSerializerEntry> event) {
final IForgeRegistry<DataSerializerEntry> registry = event.getRegistry();

registry.register(new DataSerializerEntry(CHEST_TYPE).setRegistryName(RegistryMC.ID, "chest_type"));
}

@SubscribeEvent
public static void metalchests$registerBlocks(final RegistryEvent.Register<Block> event) {
final IForgeRegistry<Block> registry = event.getRegistry();

registry.register(BlocksMC.METAL_CHEST = new BlockMetalChest());
registerTileEntity(TileMetalChest.class);
}

@SubscribeEvent
public static void registerItems(final RegistryEvent.Register<Item> event) {
public static void metalchests$registerItems(final RegistryEvent.Register<Item> event) {
final IForgeRegistry<Item> registry = event.getRegistry();

registerItemBlock(registry, BlocksMC.METAL_CHEST, ChestType.class);
Expand All @@ -274,7 +281,7 @@ public static void registerItems(final RegistryEvent.Register<Item> event) {
}

@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void registerRecipes(RegistryEvent.Register<IRecipe> event) {
public static void metalchests$registerRecipes(RegistryEvent.Register<IRecipe> event) {
ChestType.registerRecipes();

for (ChestType type : ChestType.values()) {
Expand All @@ -294,7 +301,7 @@ public static void registerRecipes(RegistryEvent.Register<IRecipe> event) {
}

@SubscribeEvent
public static void registerEntities(final RegistryEvent.Register<EntityEntry> event) {
public static void metalchests$registerEntities(final RegistryEvent.Register<EntityEntry> event) {
final IForgeRegistry<EntityEntry> registry = event.getRegistry();

if (ConfigMC.enableMinecarts) {
Expand Down Expand Up @@ -353,7 +360,7 @@ public static String getVariantName(IStringSerializable variant) {

@SideOnly(Side.CLIENT)
@SubscribeEvent
public static void registerModels(ModelRegistryEvent event) {
public static void metalchests$registerModels(ModelRegistryEvent event) {
for (ChestType type : ChestType.values()) {
registerModel(BlocksMC.METAL_CHEST, type.ordinal(), getVariantName(type));

Expand Down
16 changes: 7 additions & 9 deletions src/main/java/T145/metalchests/items/ItemChestUpgrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public ItemChestUpgrade(ResourceLocation registryName) {
setMaxStackSize(1);
}

private boolean canUpdateChest(TileEntity te, ChestUpgrade upgradeType) {
private boolean canUpdateChest(TileEntity te, ChestType base) {
if (te instanceof IMetalChest) {
IMetalChest chest = (IMetalChest) te;

if (chest.getChestType() != upgradeType.getBase() || chest.getChestAnimator().isOpen()) {
if (chest.getChestType() != base || chest.getChestAnimator().isOpen()) {
return false;
}

Expand All @@ -76,7 +76,7 @@ private boolean canUpdateChest(TileEntity te, ChestUpgrade upgradeType) {
}
}

return true;
return te != null;
}

return false;
Expand Down Expand Up @@ -113,11 +113,10 @@ private boolean updateChest(ChestType upgrade, TileEntity te, EntityPlayer playe
if (te instanceof IMetalChest) {
IMetalChest chest = (IMetalChest) te;
chest.setChestType(upgrade);
world.setBlockState(pos, createBlockState(block, upgrade), 3); // mark for NBT update
te.markDirty(); // mark for render update
world.setBlockState(pos, createBlockState(block, upgrade), 3);
return true;
} else {
boolean trapped = block.canProvidePower(world.getBlockState(te.getPos()));
boolean trapped = world.getBlockState(pos).canProvidePower();
EnumFacing front = getBlockFront(player, world, pos);
IItemHandler inv = getChestInventory(te);
block = UpgradeRegistry.getDestTile(block);
Expand All @@ -126,7 +125,7 @@ private boolean updateChest(ChestType upgrade, TileEntity te, EntityPlayer playe
world.removeTileEntity(pos);
world.setBlockToAir(pos);
world.setTileEntity(pos, block.createTileEntity(world, state));
world.setBlockState(pos, state, 3); // mark for NBT update
world.setBlockState(pos, state, 3);

te = world.getTileEntity(pos);

Expand All @@ -136,7 +135,6 @@ private boolean updateChest(ChestType upgrade, TileEntity te, EntityPlayer playe
chest.setTrapped(trapped);
chest.setFront(front);
chest.setInventory(inv);
te.markDirty(); // mark for render update
return true;
}

Expand All @@ -154,7 +152,7 @@ public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPo
ItemStack stack = player.getHeldItem(hand);
ChestUpgrade upgrade = ChestUpgrade.byMetadata(stack.getItemDamage());

if (!canUpdateChest(te, upgrade) || !updateChest(upgrade.getUpgrade(), te, player, world, pos)) {
if (!canUpdateChest(te, upgrade.getBase()) || !updateChest(upgrade.getUpgrade(), te, player, world, pos)) {
return EnumActionResult.FAIL;
}

Expand Down

0 comments on commit 4f2a8dd

Please sign in to comment.