Speed up forge event registration (#186)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Alexdoru <[email protected]>
This commit is contained in:
authored and GitHub committed 2025-07-17 23:45:05 +02:00
1 parent 121a0ce23b
commit 94022962b9
11 files changed
+375 -349

No files matched your search

-3
View File
@@ -12,7 +12,6 @@ import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
@@ -193,8 +192,6 @@ public class TConstruct {
AoEExclusionList.init(new File(event.getModConfigurationDirectory(), "TConstruct_AOEExclusions.cfg"));
playerTracker = new TPlayerHandler();
FMLCommonHandler.instance().bus().register(playerTracker);
MinecraftForge.EVENT_BUS.register(playerTracker);
NetworkRegistry.INSTANCE.registerGuiHandler(TConstruct.instance, proxy);
if (PHConstruct.globalDespawn != 6000 && PHConstruct.globalDespawn != 0) {
@@ -60,9 +60,7 @@ public class ArmorProxyClient extends ArmorProxyCommon {
@Override
public void preInit() {
controlInstance = new ArmorControls();
FMLCommonHandler.instance().bus().register(controlInstance);
MinecraftForge.EVENT_BUS.register(controlInstance);
MinecraftForge.EVENT_BUS.register(new TabRegistry());
MinecraftForge.EVENT_BUS.register(new TabRegistry.EventHandler());
}
@Override
@@ -91,7 +91,7 @@ public class TinkerArmor {
public void preInit(FMLPreInitializationEvent event) {
proxy.preInit();
MinecraftForge.EVENT_BUS.register(new TinkerArmorEvents());
MinecraftForge.EVENT_BUS.register(new TinkerArmorEvents.EventHandler());
FMLCommonHandler.instance().bus().register(new ArmorAbilities());
TinkerArmor.dryingRack = new DryingRack().setBlockName("Armor.DryingRack");
@@ -35,46 +35,6 @@ import tconstruct.util.network.ArmourGuiSyncPacket;
@Optional.Interface(iface = "com.kuba6000.mobsinfo.api.IMobExtraInfoProvider", modid = "mobsinfo")
public class TinkerArmorEvents implements IMobExtraInfoProvider {
@SubscribeEvent
public void onLivingDrop(LivingDropsEvent event) {
// ANY CHANGE MADE IN HERE MUST ALSO BE MADE IN provideDropsInformation!
if (event.entityLiving == null) return;
if (!event.entityLiving.worldObj.getGameRules().getGameRuleBooleanValue("doMobLoot")) return;
if (TConstruct.random.nextInt(200) == 0 && event.entityLiving instanceof IMob
&& event.source.damageType.equals("player")) {
ItemStack dropStack = new ItemStack(TinkerArmor.heartCanister, 1, 1);
EntityItem entityitem = new EntityItem(
event.entityLiving.worldObj,
event.entityLiving.posX,
event.entityLiving.posY,
event.entityLiving.posZ,
dropStack);
entityitem.delayBeforeCanPickup = 10;
event.drops.add(entityitem);
}
if (event.entityLiving instanceof IBossDisplayData) {
String entityName = event.entityLiving.getClass().getSimpleName().toLowerCase();
for (String name : PHConstruct.heartDropBlacklist)
if (name.toLowerCase(Locale.US).equals(entityName)) return;
int count = event.entityLiving instanceof EntityDragon ? 5 : 1;
for (int i = 0; i < count; i++) {
ItemStack dropStack = new ItemStack(TinkerArmor.heartCanister, 1, 3);
EntityItem entityitem = new EntityItem(
event.entityLiving.worldObj,
event.entityLiving.posX,
event.entityLiving.posY,
event.entityLiving.posZ,
dropStack);
entityitem.delayBeforeCanPickup = 10;
event.drops.add(entityitem);
}
}
}
@Optional.Method(modid = "mobsinfo")
@Override
public void provideExtraDropsInformation(@Nonnull String entityString, @Nonnull ArrayList<MobDrop> drops,
@@ -108,76 +68,120 @@ public class TinkerArmorEvents implements IMobExtraInfoProvider {
}
}
/* Abilities */
@SubscribeEvent
public void armorMineSpeed(net.minecraftforge.event.entity.player.PlayerEvent.BreakSpeed event) {
if (event.entityPlayer == null) return;
public static class EventHandler {
ItemStack glove = TPlayerStats.get(event.entityPlayer).armor.getStackInSlot(1);
if (event.entityPlayer.worldObj.isRemote) { // todo: sync extended inventory with clients so this stuff and
// rendering is done
// properly...
if (ArmorProxyClient.armorExtended != null) glove = ArmorProxyClient.armorExtended.getStackInSlot(1);
else glove = null;
@SubscribeEvent
public void onLivingDrop(LivingDropsEvent event) {
// ANY CHANGE MADE IN HERE MUST ALSO BE MADE IN provideDropsInformation!
if (event.entityLiving == null) return;
if (!event.entityLiving.worldObj.getGameRules().getGameRuleBooleanValue("doMobLoot")) return;
if (TConstruct.random.nextInt(200) == 0 && event.entityLiving instanceof IMob
&& event.source.damageType.equals("player")) {
ItemStack dropStack = new ItemStack(TinkerArmor.heartCanister, 1, 1);
EntityItem entityitem = new EntityItem(
event.entityLiving.worldObj,
event.entityLiving.posX,
event.entityLiving.posY,
event.entityLiving.posZ,
dropStack);
entityitem.delayBeforeCanPickup = 10;
event.drops.add(entityitem);
}
if (event.entityLiving instanceof IBossDisplayData) {
String entityName = event.entityLiving.getClass().getSimpleName().toLowerCase();
for (String name : PHConstruct.heartDropBlacklist)
if (name.toLowerCase(Locale.US).equals(entityName)) return;
int count = event.entityLiving instanceof EntityDragon ? 5 : 1;
for (int i = 0; i < count; i++) {
ItemStack dropStack = new ItemStack(TinkerArmor.heartCanister, 1, 3);
EntityItem entityitem = new EntityItem(
event.entityLiving.worldObj,
event.entityLiving.posX,
event.entityLiving.posY,
event.entityLiving.posZ,
dropStack);
entityitem.delayBeforeCanPickup = 10;
event.drops.add(entityitem);
}
}
}
if (glove == null || !glove.hasTagCompound()) return;
// ok, we got a glove. bonus mining speeeeed
NBTTagCompound tags = glove.getTagCompound().getCompoundTag(TinkerArmor.travelGlove.getBaseTagName());
float mineSpeed = tags.getInteger("MiningSpeed");
/* Abilities */
@SubscribeEvent
public void armorMineSpeed(net.minecraftforge.event.entity.player.PlayerEvent.BreakSpeed event) {
if (event.entityPlayer == null) return;
float modifier = 1f + mineSpeed / 1000f;
float base = mineSpeed / 250f;
event.newSpeed = (event.originalSpeed + base) * modifier;
}
ItemStack glove = TPlayerStats.get(event.entityPlayer).armor.getStackInSlot(1);
if (event.entityPlayer.worldObj.isRemote) { // todo: sync extended inventory with clients so this stuff and
// rendering is done
// properly...
if (ArmorProxyClient.armorExtended != null) glove = ArmorProxyClient.armorExtended.getStackInSlot(1);
else glove = null;
}
if (glove == null || !glove.hasTagCompound()) return;
@SubscribeEvent
public void jumpHeight(LivingJumpEvent event) {
ItemStack stack = event.entityLiving.getEquipmentInSlot(2);
if (stack != null && stack.getItem() instanceof TravelWings) {
event.entityLiving.motionY += 0.2;
// ok, we got a glove. bonus mining speeeeed
NBTTagCompound tags = glove.getTagCompound().getCompoundTag(TinkerArmor.travelGlove.getBaseTagName());
float mineSpeed = tags.getInteger("MiningSpeed");
float modifier = 1f + mineSpeed / 1000f;
float base = mineSpeed / 250f;
event.newSpeed = (event.originalSpeed + base) * modifier;
}
}
@SubscribeEvent
public void slimefall(LivingFallEvent event) {
ItemStack boots = event.entityLiving.getEquipmentInSlot(1);
if (boots != null && boots.getItem() instanceof IModifyable) {
NBTTagCompound tag = boots.getTagCompound()
.getCompoundTag(((IModifyable) boots.getItem()).getBaseTagName());
int sole = tag.getInteger("Slimy Soles");
if (sole > 0) {
event.distance /= (1 + sole);
event.entityLiving.fallDistance /= (1 + sole);
@SubscribeEvent
public void jumpHeight(LivingJumpEvent event) {
ItemStack stack = event.entityLiving.getEquipmentInSlot(2);
if (stack != null && stack.getItem() instanceof TravelWings) {
event.entityLiving.motionY += 0.2;
}
}
@SubscribeEvent
public void slimefall(LivingFallEvent event) {
ItemStack boots = event.entityLiving.getEquipmentInSlot(1);
if (boots != null && boots.getItem() instanceof IModifyable) {
NBTTagCompound tag = boots.getTagCompound()
.getCompoundTag(((IModifyable) boots.getItem()).getBaseTagName());
int sole = tag.getInteger("Slimy Soles");
if (sole > 0) {
event.distance /= (1 + sole);
event.entityLiving.fallDistance /= (1 + sole);
}
}
}
@SubscribeEvent
public void perfectDodge(LivingAttackEvent event) {
if (!event.source.isProjectile()) return;
// perfect dodge?
if (!(event.entityLiving instanceof EntityPlayer)) return;
EntityPlayer player = (EntityPlayer) event.entityLiving;
ItemStack chest = player.getCurrentArmor(2);
if (chest == null || !(chest.getItem() instanceof IModifyable) || !chest.hasTagCompound()) return;
NBTTagCompound tags = chest.getTagCompound()
.getCompoundTag(((IModifyable) chest.getItem()).getBaseTagName());
int dodge = tags.getInteger("Perfect Dodge");
if (dodge > TConstruct.random.nextInt(10)) event.setCanceled(true);
}
@SubscribeEvent
public void joinWorld(EntityJoinWorldEvent event) {
if (event.entity instanceof EntityPlayerMP) {
EntityPlayerMP player = (EntityPlayerMP) event.entity;
TPlayerStats stats = TPlayerStats.get(player);
NBTTagCompound tag = new NBTTagCompound();
stats.saveNBTData(tag);
ArmourGuiSyncPacket syncPacket = new ArmourGuiSyncPacket(tag);
TConstruct.packetPipeline.sendTo(syncPacket, player);
}
}
}
@SubscribeEvent
public void perfectDodge(LivingAttackEvent event) {
if (!event.source.isProjectile()) return;
// perfect dodge?
if (!(event.entityLiving instanceof EntityPlayer)) return;
EntityPlayer player = (EntityPlayer) event.entityLiving;
ItemStack chest = player.getCurrentArmor(2);
if (chest == null || !(chest.getItem() instanceof IModifyable) || !chest.hasTagCompound()) return;
NBTTagCompound tags = chest.getTagCompound().getCompoundTag(((IModifyable) chest.getItem()).getBaseTagName());
int dodge = tags.getInteger("Perfect Dodge");
if (dodge > TConstruct.random.nextInt(10)) event.setCanceled(true);
}
@SubscribeEvent
public void joinWorld(EntityJoinWorldEvent event) {
if (event.entity instanceof EntityPlayerMP) {
EntityPlayerMP player = (EntityPlayerMP) event.entity;
TPlayerStats stats = TPlayerStats.get(player);
NBTTagCompound tag = new NBTTagCompound();
stats.saveNBTData(tag);
ArmourGuiSyncPacket syncPacket = new ArmourGuiSyncPacket(tag);
TConstruct.packetPipeline.sendTo(syncPacket, player);
}
}
}
@@ -18,6 +18,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
@@ -42,21 +43,10 @@ public class TPlayerHandler {
private final ConcurrentHashMap<UUID, TPlayerStats> playerStats = new ConcurrentHashMap<>();
@SubscribeEvent
public void PlayerLoggedInEvent(PlayerLoggedInEvent event) {
onPlayerLogin(event.player);
}
@SubscribeEvent
public void onPlayerRespawn(PlayerRespawnEvent event) {
onPlayerRespawn(event.player);
}
@SubscribeEvent
public void onEntityConstructing(EntityEvent.EntityConstructing event) {
if (event.entity instanceof EntityPlayer && TPlayerStats.get((EntityPlayer) event.entity) == null) {
TPlayerStats.register((EntityPlayer) event.entity);
}
public TPlayerHandler() {
EventHandler handler = new EventHandler();
FMLCommonHandler.instance().bus().register(handler);
MinecraftForge.EVENT_BUS.register(handler);
}
public void onPlayerLogin(EntityPlayer player) {
@@ -210,52 +200,6 @@ public class TPlayerHandler {
}
}
@SubscribeEvent
public void livingFall(LivingFallEvent evt) // Only for negating fall damage
{
if (evt.entityLiving instanceof EntityPlayer) {
evt.distance -= 1;
}
}
@SubscribeEvent
public void playerDeath(LivingDeathEvent event) {
if (!(event.entity instanceof EntityPlayer)) return;
if (!event.entity.worldObj.isRemote) {
TPlayerStats properties = (TPlayerStats) event.entity.getExtendedProperties(TPlayerStats.PROP_NAME);
properties.hunger = ((EntityPlayer) event.entity).getFoodStats().getFoodLevel();
playerStats.put(event.entity.getPersistentID(), properties);
}
}
@SubscribeEvent
public void playerDrops(PlayerDropsEvent evt) {
// After playerDeath event. Modifying saved data.
TPlayerStats stats = playerStats.get(evt.entityPlayer.getPersistentID());
stats.level = evt.entityPlayer.experienceLevel / 2;
// stats.health = 20;
int hunger = evt.entityPlayer.getFoodStats().getFoodLevel();
if (hunger < 6) stats.hunger = 6;
else stats.hunger = evt.entityPlayer.getFoodStats().getFoodLevel();
if (evt.entityPlayer.capturedDrops != evt.drops) {
evt.entityPlayer.capturedDrops.clear();
}
evt.entityPlayer.captureDrops = true;
stats.armor.dropItems();
stats.knapsack.dropItems();
evt.entityPlayer.captureDrops = false;
if (evt.entityPlayer.capturedDrops != evt.drops) {
evt.drops.addAll(evt.entityPlayer.capturedDrops);
}
playerStats.put(evt.entityPlayer.getPersistentID(), stats);
}
/* Modify Player */
public void updateSize(String user, float offset) {
/*
@@ -320,4 +264,70 @@ public class TPlayerHandler {
TConstruct.logger.error(e.getMessage() != null ? e.getMessage() : "UNKOWN DL ERROR", e);
}
}
public class EventHandler {
@SubscribeEvent
public void PlayerLoggedInEvent(PlayerLoggedInEvent event) {
onPlayerLogin(event.player);
}
@SubscribeEvent
public void onPlayerRespawn(PlayerRespawnEvent event) {
TPlayerHandler.this.onPlayerRespawn(event.player);
}
@SubscribeEvent
public void onEntityConstructing(EntityEvent.EntityConstructing event) {
if (event.entity instanceof EntityPlayer && TPlayerStats.get((EntityPlayer) event.entity) == null) {
TPlayerStats.register((EntityPlayer) event.entity);
}
}
@SubscribeEvent
public void livingFall(LivingFallEvent evt) // Only for negating fall damage
{
if (evt.entityLiving instanceof EntityPlayer) {
evt.distance -= 1;
}
}
@SubscribeEvent
public void playerDeath(LivingDeathEvent event) {
if (!(event.entity instanceof EntityPlayer)) return;
if (!event.entity.worldObj.isRemote) {
TPlayerStats properties = (TPlayerStats) event.entity.getExtendedProperties(TPlayerStats.PROP_NAME);
properties.hunger = ((EntityPlayer) event.entity).getFoodStats().getFoodLevel();
playerStats.put(event.entity.getPersistentID(), properties);
}
}
@SubscribeEvent
public void playerDrops(PlayerDropsEvent evt) {
// After playerDeath event. Modifying saved data.
TPlayerStats stats = playerStats.get(evt.entityPlayer.getPersistentID());
stats.level = evt.entityPlayer.experienceLevel / 2;
// stats.health = 20;
int hunger = evt.entityPlayer.getFoodStats().getFoodLevel();
if (hunger < 6) stats.hunger = 6;
else stats.hunger = evt.entityPlayer.getFoodStats().getFoodLevel();
if (evt.entityPlayer.capturedDrops != evt.drops) {
evt.entityPlayer.capturedDrops.clear();
}
evt.entityPlayer.captureDrops = true;
stats.armor.dropItems();
stats.knapsack.dropItems();
evt.entityPlayer.captureDrops = false;
if (evt.entityPlayer.capturedDrops != evt.drops) {
evt.drops.addAll(evt.entityPlayer.capturedDrops);
}
playerStats.put(evt.entityPlayer.getPersistentID(), stats);
}
}
}
@@ -5,10 +5,12 @@ import net.minecraft.client.settings.KeyBinding;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraftforge.common.MinecraftForge;
import org.lwjgl.input.Keyboard;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
@@ -55,6 +57,9 @@ public class ArmorControls {
public ArmorControls() {
getVanillaKeyBindings();
keys = new KeyBinding[] { armorKey, toggleGoggles, beltSwap, zoomKey, null, null };
EventHandler handler = new EventHandler();
FMLCommonHandler.instance().bus().register(handler);
MinecraftForge.EVENT_BUS.register(handler);
}
public void registerKeys() {
@@ -79,21 +84,6 @@ public class ArmorControls {
invKey = mc.gameSettings.keyBindInventory;
}
@SubscribeEvent
public void keyEvent(KeyInputEvent event) {
if (!isNotEnoughKeysLoaded) {
checkAndPerformKeyActions(null, false);
}
}
@Optional.Method(modid = "notenoughkeys")
@SubscribeEvent
public void keyEventSpecial(KeyBindingPressedEvent event) {
if (event.keyBinding != null && event.isKeyBindingPressed) {
checkAndPerformKeyActions(event.keyBinding, true);
}
}
private void checkAndPerformKeyActions(KeyBinding keyBinding, boolean inputFromNotEnoughKeys) {
if (inputFromNotEnoughKeys ? keyBinding == armorKey : armorKey.isPressed()) {
openArmorGui();
@@ -208,4 +198,22 @@ public class ArmorControls {
static void updateServer(AbstractPacket abstractPacket) {
TConstruct.packetPipeline.sendToServer(abstractPacket);
}
public class EventHandler {
@SubscribeEvent
public void keyEvent(KeyInputEvent event) {
if (!isNotEnoughKeysLoaded) {
checkAndPerformKeyActions(null, false);
}
}
@Optional.Method(modid = "notenoughkeys")
@SubscribeEvent
public void keyEventSpecial(KeyBindingPressedEvent event) {
if (event.keyBinding != null && event.isKeyBindingPressed) {
checkAndPerformKeyActions(event.keyBinding, true);
}
}
}
}
@@ -26,21 +26,6 @@ public class TabRegistry {
return tabList;
}
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void guiPostInit(GuiScreenEvent.InitGuiEvent.Post event) {
if ((event.gui instanceof GuiInventory)) {
int xSize = 176;
int ySize = 166;
int guiLeft = (event.gui.width - xSize) / 2;
int guiTop = (event.gui.height - ySize) / 2;
guiLeft += getPotionOffset();
updateTabValues(guiLeft, guiTop, InventoryTabVanilla.class);
addTabsToList(event.gui.buttonList);
}
}
private static final Minecraft mc = FMLClientHandler.instance().getClient();
public static void openInventoryGui() {
@@ -95,4 +80,22 @@ public class TabRegistry {
// No potions, no offset needed
return 0;
}
public static class EventHandler {
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void guiPostInit(GuiScreenEvent.InitGuiEvent.Post event) {
if ((event.gui instanceof GuiInventory)) {
int xSize = 176;
int ySize = 166;
int guiLeft = (event.gui.width - xSize) / 2;
int guiTop = (event.gui.height - ySize) / 2;
guiLeft += getPotionOffset();
updateTabValues(guiLeft, guiTop, InventoryTabVanilla.class);
addTabsToList(event.gui.buttonList);
}
}
}
}
@@ -21,6 +21,7 @@ import net.minecraft.util.StatCollector;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.common.ISpecialArmor;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraftforge.event.entity.living.LivingFallEvent;
@@ -48,7 +49,7 @@ public class ItemSlimeBoots extends ItemArmor implements ISpecialArmor {
armorPart = ArmorPart.Feet;
textureFolder = "armor";
textureName = "slime";
SlimeBounceHandler.registerEvent(this);
MinecraftForge.EVENT_BUS.register(new EventHandler());
}
@Override
@@ -71,64 +72,6 @@ public class ItemSlimeBoots extends ItemArmor implements ISpecialArmor {
return itemStackIn;
}
/** Called when an entity lands to handle the event */
@SubscribeEvent
// RUBBERY BOUNCY BOUNCERY WOOOOO
public void onFall(LivingFallEvent event) {
final EntityLivingBase living = event.entityLiving;
// using fall distance as the event distance could be reduced by jump boost
if (living == null || living.fallDistance <= 2f) {
return;
}
// can the entity bounce?
if (!SlimeBounceHandler.hasSlimeBoots(living)) {
return;
}
// reduced fall damage when crouching
if (living.isSneaking()) {
event.distance = 1;
return;
}
// thing is wearing slime boots. let's get bouncyyyyy
event.setCanceled(true);
// skip further client processing on players
if (living.worldObj.isRemote) {
living.playSound("mob.slime.small", 1f, 1f);
SlimeBounceHandler.addBounceHandler(living);
return;
}
// server players behave differently than non-server players, they have no
// velocity during the event, so we need to reverse engineer it
Vec3 motion = SlimeBounceHandler.getMotion(living);
if (living instanceof EntityPlayerMP) {
// velocity is lost on server players, but we don't have to defer the bounce
double gravity = 0.2353455252;
double time = Math.sqrt(living.fallDistance / gravity);
double velocity = gravity * time / 2;
living.motionX = motion.xCoord / 0.95f;
living.motionY = velocity;
living.motionZ = motion.zCoord / 0.95f;
living.velocityChanged = true;
// preserve momentum
SlimeBounceHandler.addBounceHandler(living);
} else {
// for non-players, need to defer the bounce
// only slow down half as much when bouncing
living.motionX = motion.xCoord / 0.95f;
living.motionY = motion.yCoord * -0.9;
living.motionZ = motion.zCoord / 0.95f;
SlimeBounceHandler.addBounceHandler(living, SlimeBounceHandler.getMotion(living).yCoord);
}
// update airborn status
living.isAirBorne = true;
living.onGround = false;
event.distance = 0f;
living.playSound("mob.slime.small", 1f, 1f);
}
@Override
public int getArmorDisplay(EntityPlayer player, ItemStack armor, int slot) {
return 0;
@@ -231,4 +174,66 @@ public class ItemSlimeBoots extends ItemArmor implements ISpecialArmor {
public Entity createEntity(World world, Entity location, ItemStack itemstack) {
return new FancyEntityItem(world, location, itemstack);
}
public static class EventHandler {
/** Called when an entity lands to handle the event */
@SubscribeEvent
// RUBBERY BOUNCY BOUNCERY WOOOOO
public void onFall(LivingFallEvent event) {
final EntityLivingBase living = event.entityLiving;
// using fall distance as the event distance could be reduced by jump boost
if (living == null || living.fallDistance <= 2f) {
return;
}
// can the entity bounce?
if (!SlimeBounceHandler.hasSlimeBoots(living)) {
return;
}
// reduced fall damage when crouching
if (living.isSneaking()) {
event.distance = 1;
return;
}
// thing is wearing slime boots. let's get bouncyyyyy
event.setCanceled(true);
// skip further client processing on players
if (living.worldObj.isRemote) {
living.playSound("mob.slime.small", 1f, 1f);
SlimeBounceHandler.addBounceHandler(living);
return;
}
// server players behave differently than non-server players, they have no
// velocity during the event, so we need to reverse engineer it
Vec3 motion = SlimeBounceHandler.getMotion(living);
if (living instanceof EntityPlayerMP) {
// velocity is lost on server players, but we don't have to defer the bounce
double gravity = 0.2353455252;
double time = Math.sqrt(living.fallDistance / gravity);
double velocity = gravity * time / 2;
living.motionX = motion.xCoord / 0.95f;
living.motionY = velocity;
living.motionZ = motion.zCoord / 0.95f;
living.velocityChanged = true;
// preserve momentum
SlimeBounceHandler.addBounceHandler(living);
} else {
// for non-players, need to defer the bounce
// only slow down half as much when bouncing
living.motionX = motion.xCoord / 0.95f;
living.motionY = motion.yCoord * -0.9;
living.motionZ = motion.zCoord / 0.95f;
SlimeBounceHandler.addBounceHandler(living, SlimeBounceHandler.getMotion(living).yCoord);
}
// update airborn status
living.isAirBorne = true;
living.onGround = false;
event.distance = 0f;
living.playSound("mob.slime.small", 1f, 1f);
}
}
}
@@ -5,11 +5,9 @@ import java.util.IdentityHashMap;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import tconstruct.gadgets.item.ItemSlimeBoots;
@@ -73,11 +71,6 @@ public class SlimeBounceHandler {
}
}
public static void registerEvent(Object o) {
MinecraftForge.EVENT_BUS.register(o);
FMLCommonHandler.instance().bus().register(o);
}
/** Called on living tick to preserve momentum and bounce */
@SubscribeEvent
public void onLivingTick(LivingUpdateEvent event) {
@@ -149,7 +149,7 @@ public class TinkerWorld {
@Handler
public void preInit(FMLPreInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(new TinkerWorldEvents());
MinecraftForge.EVENT_BUS.register(new TinkerWorldEvents.EventHandler());
// Blocks
TinkerWorld.meatBlock = new MeatBlock().setBlockName("tconstruct.meatblock");
@@ -40,25 +40,6 @@ import tconstruct.util.config.PHConstruct;
@Optional.Interface(iface = "com.kuba6000.mobsinfo.api.IMobExtraInfoProvider", modid = "mobsinfo")
public class TinkerWorldEvents implements IMobExtraInfoProvider {
@SubscribeEvent
public void onLivingSpawn(LivingSpawnEvent.SpecialSpawn event) {
EntityLivingBase living = event.entityLiving;
if (living.getClass() == EntitySpider.class && TConstruct.random.nextInt(100) == 0) {
EntityCreeper creeper = new EntityCreeper(living.worldObj);
spawnEntityLiving(living.posX, living.posY + 1, living.posZ, creeper, living.worldObj);
if (living.riddenByEntity != null) creeper.mountEntity(living.riddenByEntity);
else creeper.mountEntity(living);
EntityXPOrb orb = new EntityXPOrb(
living.worldObj,
living.posX,
living.posY,
living.posZ,
TConstruct.random.nextInt(20) + 20);
orb.mountEntity(creeper);
}
}
public static void spawnEntityLiving(double x, double y, double z, EntityLiving entity, World world) {
if (!world.isRemote) {
entity.setPosition(x, y, z);
@@ -67,69 +48,6 @@ public class TinkerWorldEvents implements IMobExtraInfoProvider {
}
}
/* Bonemeal */
@SubscribeEvent
public void bonemealEvent(BonemealEvent event) {
if (!event.world.isRemote) {
if (event.block == TinkerWorld.slimeSapling) {
if (TinkerWorld.slimeSapling
.boneFertilize(event.world, event.x, event.y, event.z, event.world.rand, event.entityPlayer))
event.setResult(Event.Result.ALLOW);
else event.setCanceled(true);
}
}
}
/* Damage */
@SubscribeEvent
public void onHurt(LivingHurtEvent event) {
EntityLivingBase reciever = event.entityLiving;
if (reciever instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) event.entityLiving;
// Cutlass
ItemStack stack = player.getCurrentEquippedItem();
if (stack != null && player.isUsingItem()) {
Item item = stack.getItem();
if (item == TinkerTools.cutlass) {
player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 3 * 20, 1));
} else if (item == TinkerTools.battlesign) {
event.ammount *= 1.5; // Puts battlesign blocking at 3/4 instead of 1/2
}
}
} else if (reciever instanceof EntityCreeper) {
Entity attacker = event.source.getEntity();
if (attacker instanceof EntityLivingBase) {
Entity target = ((EntityCreeper) reciever).getAttackTarget();
if (target != null) {
float d1 = reciever.getDistanceToEntity(((EntityCreeper) reciever).getAttackTarget());
float d2 = reciever.getDistanceToEntity(attacker);
if (d2 < d1) {
((EntityCreeper) event.entityLiving)
.setAttackTarget((EntityLivingBase) event.source.getEntity());
}
}
}
}
}
@SubscribeEvent
public void onLivingDrop(LivingDropsEvent event) {
// ANY CHANGE MADE IN HERE MUST ALSO BE MADE IN provideExtraDropsInformation!
if (event.entityLiving == null) return;
if (event.entityLiving.getClass() == EntityGhast.class) {
if (PHConstruct.uhcGhastDrops) {
for (EntityItem o : event.drops) {
if (o.getEntityItem().getItem() == Items.ghast_tear) {
o.setEntityItemStack(new ItemStack(Items.gold_ingot, 1));
}
}
} else {
ItemHelper.addDrops(event, new ItemStack(Items.ghast_tear, 1));
}
}
}
@Optional.Method(modid = "mobsinfo")
@Override
public void provideExtraDropsInformation(@Nonnull String entityString, @Nonnull ArrayList<MobDrop> drops,
@@ -152,4 +70,94 @@ public class TinkerWorldEvents implements IMobExtraInfoProvider {
}
}
}
public static class EventHandler {
@SubscribeEvent
public void onLivingSpawn(LivingSpawnEvent.SpecialSpawn event) {
EntityLivingBase living = event.entityLiving;
if (living.getClass() == EntitySpider.class && TConstruct.random.nextInt(100) == 0) {
EntityCreeper creeper = new EntityCreeper(living.worldObj);
spawnEntityLiving(living.posX, living.posY + 1, living.posZ, creeper, living.worldObj);
if (living.riddenByEntity != null) creeper.mountEntity(living.riddenByEntity);
else creeper.mountEntity(living);
EntityXPOrb orb = new EntityXPOrb(
living.worldObj,
living.posX,
living.posY,
living.posZ,
TConstruct.random.nextInt(20) + 20);
orb.mountEntity(creeper);
}
}
/* Bonemeal */
@SubscribeEvent
public void bonemealEvent(BonemealEvent event) {
if (!event.world.isRemote) {
if (event.block == TinkerWorld.slimeSapling) {
if (TinkerWorld.slimeSapling.boneFertilize(
event.world,
event.x,
event.y,
event.z,
event.world.rand,
event.entityPlayer))
event.setResult(Event.Result.ALLOW);
else event.setCanceled(true);
}
}
}
/* Damage */
@SubscribeEvent
public void onHurt(LivingHurtEvent event) {
EntityLivingBase reciever = event.entityLiving;
if (reciever instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) event.entityLiving;
// Cutlass
ItemStack stack = player.getCurrentEquippedItem();
if (stack != null && player.isUsingItem()) {
Item item = stack.getItem();
if (item == TinkerTools.cutlass) {
player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 3 * 20, 1));
} else if (item == TinkerTools.battlesign) {
event.ammount *= 1.5; // Puts battlesign blocking at 3/4 instead of 1/2
}
}
} else if (reciever instanceof EntityCreeper) {
Entity attacker = event.source.getEntity();
if (attacker instanceof EntityLivingBase) {
Entity target = ((EntityCreeper) reciever).getAttackTarget();
if (target != null) {
float d1 = reciever.getDistanceToEntity(((EntityCreeper) reciever).getAttackTarget());
float d2 = reciever.getDistanceToEntity(attacker);
if (d2 < d1) {
((EntityCreeper) event.entityLiving)
.setAttackTarget((EntityLivingBase) event.source.getEntity());
}
}
}
}
}
@SubscribeEvent
public void onLivingDrop(LivingDropsEvent event) {
// ANY CHANGE MADE IN HERE MUST ALSO BE MADE IN provideExtraDropsInformation!
if (event.entityLiving == null) return;
if (event.entityLiving.getClass() == EntityGhast.class) {
if (PHConstruct.uhcGhastDrops) {
for (EntityItem o : event.drops) {
if (o.getEntityItem().getItem() == Items.ghast_tear) {
o.setEntityItemStack(new ItemStack(Items.gold_ingot, 1));
}
}
} else {
ItemHelper.addDrops(event, new ItemStack(Items.ghast_tear, 1));
}
}
}
}
}