|
| 1 | +/* |
| 2 | +WebShooter Minecraft Mod |
| 3 | +Copyright (C) 2016 Joseph C. Sible |
| 4 | +
|
| 5 | +This program is free software; you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU General Public License as published by |
| 7 | +the Free Software Foundation; either version 2 of the License, or |
| 8 | +(at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU General Public License along |
| 16 | +with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | +*/ |
| 19 | + |
| 20 | +package josephcsible.webshooter; |
| 21 | + |
| 22 | +import net.minecraft.block.Block; |
| 23 | +import net.minecraft.block.state.IBlockState; |
| 24 | +import net.minecraft.entity.Entity; |
| 25 | +import net.minecraft.entity.monster.EntitySpider; |
| 26 | +import net.minecraft.entity.player.EntityPlayerMP; |
| 27 | +import net.minecraft.init.Blocks; |
| 28 | +import net.minecraft.util.math.BlockPos; |
| 29 | +import net.minecraft.world.World; |
| 30 | +import net.minecraftforge.common.MinecraftForge; |
| 31 | +import net.minecraftforge.common.config.Configuration; |
| 32 | +import net.minecraftforge.event.entity.living.LivingAttackEvent; |
| 33 | +import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent; |
| 34 | +import net.minecraftforge.fml.common.FMLCommonHandler; |
| 35 | +import net.minecraftforge.fml.common.Mod; |
| 36 | +import net.minecraftforge.fml.common.Mod.EventHandler; |
| 37 | +import net.minecraftforge.fml.common.event.FMLInitializationEvent; |
| 38 | +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; |
| 39 | +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; |
| 40 | +import net.minecraftforge.fml.common.network.NetworkRegistry; |
| 41 | +import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; |
| 42 | +import net.minecraftforge.fml.relauncher.Side; |
| 43 | + |
| 44 | +@Mod(modid = WebShooter.MODID, version = WebShooter.VERSION, guiFactory = "josephcsible.webshooter.WebShooterGuiFactory") |
| 45 | +public class WebShooter |
| 46 | +{ |
| 47 | + // XXX duplication with mcmod.info and build.gradle |
| 48 | + public static final String MODID = "webshooter"; |
| 49 | + public static final String VERSION = "1.0.0"; |
| 50 | + |
| 51 | + public static Configuration config; |
| 52 | + public static SimpleNetworkWrapper netWrapper; |
| 53 | + protected double webChance; |
| 54 | + protected boolean allowReplacement; |
| 55 | + |
| 56 | + @EventHandler |
| 57 | + public void preInit(FMLPreInitializationEvent event) { |
| 58 | + netWrapper = NetworkRegistry.INSTANCE.newSimpleChannel(MODID); |
| 59 | + netWrapper.registerMessage(PlayerInWebMessage.Handler.class, PlayerInWebMessage.class, 0, Side.CLIENT); |
| 60 | + config = new Configuration(event.getSuggestedConfigurationFile()); |
| 61 | + syncConfig(); |
| 62 | + } |
| 63 | + |
| 64 | + protected void syncConfig() { |
| 65 | + webChance = config.get(Configuration.CATEGORY_GENERAL, "webChance", 0.15, "The chance per attack that a spider will create a web on an entity it attacks", 0.0, 1.0).getDouble(); |
| 66 | + allowReplacement = config.get(Configuration.CATEGORY_GENERAL, "allowReplacement", true, "Whether webs are able to replace water, lava, fire, snow, vines, and any mod-added blocks declared as replaceable").getBoolean(); |
| 67 | + if(config.hasChanged()) |
| 68 | + config.save(); |
| 69 | + } |
| 70 | + |
| 71 | + @EventHandler |
| 72 | + public void init(FMLInitializationEvent event) |
| 73 | + { |
| 74 | + MinecraftForge.EVENT_BUS.register(this); |
| 75 | + } |
| 76 | + |
| 77 | + @SubscribeEvent |
| 78 | + public void onConfigChanged(OnConfigChangedEvent eventArgs) { |
| 79 | + if(eventArgs.getModID().equals(MODID)) |
| 80 | + syncConfig(); |
| 81 | + } |
| 82 | + |
| 83 | + @SubscribeEvent |
| 84 | + public void onLivingAttack(LivingAttackEvent event) { |
| 85 | + // Mod authors: If your mod adds custom spiders, and you want them to work with |
| 86 | + // this mod, make them subclass EntitySpider (like vanilla cave spiders do). |
| 87 | + if(!(event.getSource().getEntity() instanceof EntitySpider)) |
| 88 | + return; |
| 89 | + |
| 90 | + Entity target = event.getEntity(); |
| 91 | + World world = target.worldObj; |
| 92 | + BlockPos pos = new BlockPos(target.posX, target.posY, target.posZ); |
| 93 | + IBlockState state = world.getBlockState(pos); |
| 94 | + Block oldBlock = state.getBlock(); |
| 95 | + |
| 96 | + if(!oldBlock.isReplaceable(world, pos)) |
| 97 | + return; |
| 98 | + |
| 99 | + if(!allowReplacement && !oldBlock.isAir(state, world, pos)) |
| 100 | + return; |
| 101 | + |
| 102 | + if(webChance < world.rand.nextDouble()) |
| 103 | + return; |
| 104 | + |
| 105 | + world.setBlockState(pos, Blocks.WEB.getDefaultState()); |
| 106 | + target.setInWeb(); |
| 107 | + if(target instanceof EntityPlayerMP) { |
| 108 | + // If we don't tell the client about the web ourself, it won't get told until after the |
| 109 | + // attack resolves. This will result in the client thinking the player got knocked back |
| 110 | + // further than they really did, which in turn will result in a "player moved wrongly" |
| 111 | + // message on the server. |
| 112 | + netWrapper.sendTo(new PlayerInWebMessage(pos), (EntityPlayerMP)target); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments