Added Keybind for carrying

This commit is contained in:
Tschipp 2017-08-27 21:30:24 +02:00
parent 2b4a0a7b4d
commit 7509e52c8c
13 changed files with 178 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import tschipp.carryon.common.CommonProxy;
@EventBusSubscriber
@ -31,7 +32,7 @@ public class CarryOn {
public static final String UPDATE_JSON = "https://gist.githubusercontent.com/Tschipp/dccadee7c90d7a34e6e76a35d9d6fa2e/raw/";
public static final Logger LOGGER = LogManager.getFormatterLogger("CarryOn");
//public static SimpleNetworkWrapper network;
public static SimpleNetworkWrapper network;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {

View File

@ -3,6 +3,7 @@ package tschipp.carryon.client;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import tschipp.carryon.client.keybinds.CarryOnKeybinds;
import tschipp.carryon.common.CommonProxy;
import tschipp.carryon.common.handler.RegistrationHandler;
@ -19,6 +20,7 @@ public class ClientProxy extends CommonProxy {
@Override
public void init(FMLInitializationEvent event)
{
CarryOnKeybinds.init();
super.init(event);
}

View File

@ -0,0 +1,44 @@
package tschipp.carryon.client.keybinds;
import org.lwjgl.input.Keyboard;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class CarryOnKeybinds
{
public static final String KEYBIND_KEY = "carryOnKeyPressed";
public static KeyBinding carryKey;
@SideOnly(Side.CLIENT)
public static void init()
{
carryKey = new KeyBinding("key.carry.desc", Keyboard.KEY_LSHIFT, "key.carry.category");
ClientRegistry.registerKeyBinding(carryKey);
}
public static boolean isKeyPressed(EntityPlayer player)
{
NBTTagCompound tag = player.getEntityData();
if(tag != null && tag.hasKey(KEYBIND_KEY))
{
return tag.getBoolean(KEYBIND_KEY);
}
return false;
}
public static void setKeyPressed(EntityPlayer player, boolean pressed)
{
NBTTagCompound tag = player.getEntityData();
tag.setBoolean(KEYBIND_KEY, pressed);
}
}

View File

@ -4,8 +4,12 @@ import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.relauncher.Side;
import tschipp.carryon.CarryOn;
import tschipp.carryon.common.handler.RegistrationHandler;
import tschipp.carryon.network.server.SyncKeybindPacket;
import tschipp.carryon.network.server.SyncKeybindPacketHandler;
public class CommonProxy
{
@ -13,6 +17,10 @@ public class CommonProxy
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
CarryOn.network = NetworkRegistry.INSTANCE.newSimpleChannel("CarryOn");
CarryOn.network.registerMessage(SyncKeybindPacketHandler.class, SyncKeybindPacket.class, 0, Side.SERVER);
RegistrationHandler.regItems();
RegistrationHandler.regCommonEvents();
}

View File

@ -36,6 +36,12 @@ public class Configs {
@Comment("Larger Entities slow down the player more")
public boolean heavyEntities = true;
@Comment("Slowness multiplier for blocks")
public double blockSlownessMultiplier = 1.0;
@Comment("Slowness multiplier for entities")
public double entitySlownessMultiplier = 1.0;
}
public static class Blacklist
@ -62,6 +68,8 @@ public class Configs {
"bigreactors:*",
"forestry:*",
"tconstruct:*",
"refinedstorage:*",
"rustic:*",
"immersiveengineering:*",
"embers:block_furnace",
"embers:ember_bore",
@ -87,7 +95,8 @@ public class Configs {
"minecraft:ender_dragon",
"minecraft:ghast",
"minecraft:shulker",
"animania:textures/entity/pigs/hamster_tarou.png"
"animania:textures/entity/pigs/hamster_tarou.png",
"mynko:*"
};
}
@ -109,6 +118,7 @@ public class Configs {
"minecraft:tallgrass;2->(item)minecraft:tallgrass;2",
"minecraft:flower_pot->(block)minecraft:flower_pot",
"minecraft:leaves2->(item)minecraft:leaves2",
"minecraft:reeds->(block)minecraft:reeds",
"quark:custom_chest{type:\"spruce\"}->quark:custom_chest;0",
"quark:custom_chest{type:\"birch\"}->quark:custom_chest;1",
"quark:custom_chest{type:\"jungle\"}->quark:custom_chest;2",

View File

@ -1,19 +1,24 @@
package tschipp.carryon.common.event;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import tschipp.carryon.client.keybinds.CarryOnKeybinds;
import tschipp.carryon.common.handler.PickupHandler;
import tschipp.carryon.common.handler.RegistrationHandler;
import tschipp.carryon.common.item.ItemEntity;
@ -56,7 +61,7 @@ public class ItemEntityEvents
}
}
@SubscribeEvent(priority = EventPriority.NORMAL)
@SubscribeEvent(priority = EventPriority.HIGH)
public void onEntityRightClick(PlayerInteractEvent.EntityInteract event)
{
EntityPlayer player = event.getEntityPlayer();
@ -69,7 +74,7 @@ public class ItemEntityEvents
Entity entity = event.getTarget();
BlockPos pos = entity.getPosition();
if (main.isEmpty() && off.isEmpty() && player.isSneaking())
if (main.isEmpty() && off.isEmpty() && CarryOnKeybinds.isKeyPressed(player))
{
ItemStack stack = new ItemStack(RegistrationHandler.itemEntity);
@ -88,4 +93,29 @@ public class ItemEntityEvents
}
@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event)
{
EntityLivingBase entity = event.getEntityLiving();
World world = entity.world;
ItemStack main = entity.getHeldItemMainhand();
if (!main.isEmpty() && main.getItem() == RegistrationHandler.itemEntity && ItemEntity.hasEntityData(main))
{
BlockPos pos = entity.getPosition();
BlockPos below = pos.offset(EnumFacing.DOWN);
if (world.getBlockState(pos).getMaterial() == Material.WATER || world.getBlockState(below).getMaterial() == Material.WATER)
{
Entity contained = ItemEntity.getEntity(main, world);
if (contained != null)
{
float height = contained.height;
float width = contained.width;
entity.addVelocity(0, -0.01 * height * width, 0);
}
}
}
}
}

View File

@ -2,6 +2,7 @@ package tschipp.carryon.common.event;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
@ -19,6 +20,7 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import tschipp.carryon.client.keybinds.CarryOnKeybinds;
import tschipp.carryon.common.config.CarryOnConfig;
import tschipp.carryon.common.handler.ForbiddenTileHandler;
import tschipp.carryon.common.handler.PickupHandler;
@ -92,7 +94,7 @@ public class ItemEvents
Block block = world.getBlockState(pos).getBlock();
IBlockState state = world.getBlockState(pos);
if (main.isEmpty() && off.isEmpty() && player.isSneaking() && !ForbiddenTileHandler.isForbidden(block))
if (main.isEmpty() && off.isEmpty() && CarryOnKeybinds.isKeyPressed(player) && !ForbiddenTileHandler.isForbidden(block))
{
ItemStack stack = new ItemStack(RegistrationHandler.itemTile);

View File

@ -11,6 +11,7 @@ import net.darkhax.gamestages.capabilities.PlayerDataHandler;
import net.darkhax.gamestages.capabilities.PlayerDataHandler.IStageData;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.passive.EntityTameable;
@ -35,6 +36,7 @@ public class PickupHandler
player.closeScreen();
if ((block.getBlockHardness(state, world, pos) != -1 || player.isCreative()))
{
double distance = pos.distanceSqToCenter(player.posX, player.posY + 0.5, player.posZ);

View File

@ -212,6 +212,6 @@ public class ItemEntity extends Item
if (!CarryOnConfig.settings.heavyEntities)
i = 1;
return i;
return (int) (i * CarryOnConfig.settings.entitySlownessMultiplier);
}
}

View File

@ -295,6 +295,6 @@ public class ItemTile extends Item
if (!CarryOnConfig.settings.heavyTiles)
i = 1;
return i;
return (int) (i * CarryOnConfig.settings.blockSlownessMultiplier);
}
}

View File

@ -0,0 +1,35 @@
package tschipp.carryon.network.server;
import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
public class SyncKeybindPacket implements IMessage
{
private int p;
public boolean pressed;
public SyncKeybindPacket()
{
}
public SyncKeybindPacket(boolean pressed)
{
this.p = pressed ? 1 : 0;
}
@Override
public void fromBytes(ByteBuf buf)
{
this.p = ByteBufUtils.readVarInt(buf, 4);
this.pressed = p == 1 ? true : false;
}
@Override
public void toBytes(ByteBuf buf)
{
ByteBufUtils.writeVarInt(buf, p, 4);
}
}

View File

@ -0,0 +1,34 @@
package tschipp.carryon.network.server;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.IThreadListener;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import tschipp.carryon.client.keybinds.CarryOnKeybinds;
public class SyncKeybindPacketHandler implements IMessageHandler<SyncKeybindPacket, IMessage>
{
@Override
public IMessage onMessage(final SyncKeybindPacket message, final MessageContext ctx)
{
IThreadListener mainThread = (WorldServer) ctx.getServerHandler().playerEntity.world;
mainThread.addScheduledTask(new Runnable()
{
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
@Override
public void run()
{
CarryOnKeybinds.setKeyPressed(player, message.pressed);
}
});
return null;
}
}

View File

@ -8,3 +8,6 @@ carryon.general.blacklist.forbiddenentities=Entities that the Player cannot pick
carryon.general.blacklist.forbiddentiles=Blocks that the Player cannot pick up
carryon.category.custompickupconditions.custompickupconditionsblocks=Custom Block Pickup Conditions
carryon.category.custompickupconditions.custompickupconditionsentities=Custom Entity Pickup Conditions
key.carry.desc=Carry
key.carry.category=Carry On