-
-
Save izzyaxel/ebd1a16d6146e317d0f5f6435164ff8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ActionHandler | |
{ | |
Map<String, List<IKeyAction>> actions = new HashMap<String, List<IKeyAction>>(); | |
/** | |
* | |
* @param alias | |
* @param action Implementation of {@link IKeyAction}, or a lambda expression of {@link IKeyAction#run(EntityPlayer)} | |
*/ | |
public void registerKeyAction(String alias, IKeyAction action) | |
{ | |
if(this.actions.containsKey(alias)) | |
{ | |
this.actions.get(alias).add(action); | |
} | |
else | |
{ | |
List<IKeyAction> newKeybind = new ArrayList<IKeyAction>(); | |
newKeybind.add(action); | |
this.actions.put(alias, newKeybind); | |
} | |
} | |
protected void doAction(String alias, EntityPlayer player) | |
{ | |
if(this.actions.containsKey(alias)) | |
{ | |
List<IKeyAction> list = this.actions.get(alias); | |
if(!list.isEmpty()) | |
{ | |
for(IKeyAction action : list) | |
{ | |
action.run(player); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@FunctionalInterface | |
public interface IKeyAction | |
{ | |
void run(EntityPlayer player); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class KeyHandler | |
{ | |
private static KeyHandler INSTANCE = new KeyHandler(); | |
HashMap<KeyBinding, String> aliases = new HashMap<KeyBinding, String>(); | |
public KeyHandler() | |
{ | |
GameSettings gs = Minecraft.getMinecraft().gameSettings; | |
this.aliases.put(gs.keyBindAttack, ATTACK); | |
this.aliases.put(gs.keyBindUseItem, PLACE); | |
this.aliases.put(gs.keyBindPickBlock, PICK); | |
this.aliases.put(gs.keyBindDrop, DROP); | |
this.aliases.put(gs.keyBindsHotbar[0], SLOT1); | |
this.aliases.put(gs.keyBindsHotbar[1], SLOT2); | |
this.aliases.put(gs.keyBindsHotbar[2], SLOT3); | |
this.aliases.put(gs.keyBindsHotbar[3], SLOT4); | |
this.aliases.put(gs.keyBindsHotbar[4], SLOT5); | |
this.aliases.put(gs.keyBindsHotbar[5], SLOT6); | |
this.aliases.put(gs.keyBindsHotbar[6], SLOT7); | |
this.aliases.put(gs.keyBindsHotbar[7], SLOT8); | |
this.aliases.put(gs.keyBindsHotbar[8], SLOT9); | |
this.aliases.put(gs.keyBindInventory, INVENTORY); | |
this.aliases.put(gs.keyBindSwapHands, SWAPHANDS); | |
this.aliases.put(gs.keyBindSpectatorOutlines, HIGHLIGHT); | |
this.aliases.put(gs.keyBindScreenshot, SCREENSHOT); | |
this.aliases.put(gs.keyBindSmoothCamera, CINEMATIC); | |
this.aliases.put(gs.keyBindFullscreen, FULLSCREEN); | |
this.aliases.put(gs.keyBindTogglePerspective, PERSPECTIVE); | |
this.aliases.put(gs.keyBindJump, JUMP); | |
this.aliases.put(gs.keyBindSneak, SNEAK); | |
this.aliases.put(gs.keyBindSprint, SPRINT); | |
this.aliases.put(gs.keyBindLeft, LEFT); | |
this.aliases.put(gs.keyBindRight, RIGHT); | |
this.aliases.put(gs.keyBindBack, BACK); | |
this.aliases.put(gs.keyBindForward, FORWARD); | |
this.aliases.put(gs.keyBindPlayerList, LIST); | |
this.aliases.put(gs.keyBindChat, CHAT); | |
this.aliases.put(gs.keyBindCommand, COMMAND); | |
} | |
public static KeyHandler getHandler() | |
{ | |
return INSTANCE; | |
} | |
/** | |
* Register any keybinds your mod adds with Sapphire's key handling system | |
* @param alias Alias for your keybind, for example keyBindJump is aliased with "jump" | |
* @param keyBinding Keybind to associate this alias with | |
*/ | |
public void registerKeybindAlias(String alias, KeyBinding keyBinding) | |
{ | |
if(!this.aliases.containsValue(alias)) | |
{ | |
this.aliases.put(keyBinding, alias); | |
} | |
} | |
@SubscribeEvent | |
public void tick(TickEvent.PlayerTickEvent event) | |
{ | |
for(KeyBinding k : Minecraft.getMinecraft().gameSettings.keyBindings) | |
{ | |
if(k.isKeyDown()) | |
{ | |
Sapphire.SNW.sendToServer(new PacketDoAction(event.player.getEntityId(), this.aliases.get(k))); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class KeyReference | |
{ | |
public static final String | |
ATTACK = "attack", | |
PLACE = "place", | |
PICK = "pick", | |
DROP = "drop", | |
SLOT1 = "slot1", | |
SLOT2 = "slot2", | |
SLOT3 = "slot3", | |
SLOT4 = "slot4", | |
SLOT5 = "slot5", | |
SLOT6 = "slot6", | |
SLOT7 = "slot7", | |
SLOT8 = "slot8", | |
SLOT9 = "slot9", | |
INVENTORY = "inventory", | |
SWAPHANDS = "swaphands", | |
HIGHLIGHT = "highlight", | |
SCREENSHOT = "screenshot", | |
CINEMATIC = "cinematic", | |
FULLSCREEN = "fullscreen", | |
PERSPECTIVE = "perspective", | |
JUMP = "jump", | |
SNEAK = "sneak", | |
SPRINT = "sprint", | |
LEFT = "left", | |
RIGHT = "right", | |
BACK = "back", | |
FORWARD = "forward", | |
LIST = "list", | |
CHAT = "chat", | |
COMMAND = "command"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PacketDoAction implements IMessage | |
{ | |
String alias; | |
int entityId; | |
public PacketDoAction(){} | |
public PacketDoAction(int entityId, String alias) | |
{ | |
this.entityId = entityId; | |
this.alias = alias; | |
} | |
@Override | |
public void toBytes(ByteBuf buf) | |
{ | |
buf.writeInt(this.alias.getBytes().length); | |
buf.writeBytes(this.alias.getBytes()); | |
buf.writeInt(this.entityId); | |
} | |
@Override | |
public void fromBytes(ByteBuf buf) | |
{ | |
int length = buf.readInt(); | |
this.alias = buf.readBytes(length).toString(StandardCharsets.UTF_8); | |
this.entityId = buf.readInt(); | |
} | |
public static final class DoActionHandler implements IMessageHandler<PacketDoAction, IMessage> | |
{ | |
@Override | |
public IMessage onMessage(PacketDoAction message, MessageContext ctx) | |
{ | |
if(ctx.getServerHandler().playerEntity.worldObj.getEntityByID(message.entityId) != null && ctx.getServerHandler().playerEntity.worldObj.getEntityByID(message.entityId) instanceof EntityPlayer) | |
{ | |
EntityPlayer player = (EntityPlayer)ctx.getServerHandler().playerEntity.worldObj.getEntityByID(message.entityId); | |
Sapphire.ACTIONHANDLER.doAction(message.alias, player); | |
} | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RegisterKeybinds | |
{ | |
public static void register() | |
{ | |
Sapphire.ACTIONHANDLER.registerKeyAction(KeyReference.JUMP, (player) -> | |
{ | |
if(FMLCommonHandler.instance().getMinecraftServerInstance().isFlightAllowed()) | |
{ | |
player.motionY += 0.115; | |
if(player.motionY > 0.4) | |
{ | |
player.motionY = 0.4; | |
} | |
player.fallDistance = 0f; | |
player.velocityChanged = true; | |
} | |
}); | |
//Sapphire.ACTIONHANDLER.registerKeyAction(... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment