Skip to content

Commit

Permalink
Add recent morph keybind
Browse files Browse the repository at this point in the history
  • Loading branch information
asanetargoss committed Oct 18, 2020
1 parent c623756 commit 479a4c6
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/main/java/mchorse/metamorph/api/MorphAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public static boolean selectMorph(AbstractMorph morph)
if (morph != null)
{
EntityPlayer player = Minecraft.getMinecraft().player;
IMorphing cap = Morphing.get(player);
List<AbstractMorph> acquiredMorphs = cap.getAcquiredMorphs();
IMorphing morphing = Morphing.get(player);
List<AbstractMorph> acquiredMorphs = morphing.getAcquiredMorphs();
for (int i = 0; i < acquiredMorphs.size(); i++)
{
AbstractMorph acquiredMorph = acquiredMorphs.get(i);
Expand All @@ -117,6 +117,8 @@ public static boolean selectMorph(AbstractMorph morph)
{
return false;
}

morphing.setLastSelectedMorph(morph);
}

Dispatcher.sendToServer(new PacketSelectMorph(index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ private void sendAcquiredMorphs(IMorphing cap, EntityPlayer player)
EntityPlayerMP mp = (EntityPlayerMP) player;

Dispatcher.sendTo(new PacketMorph(cap.getCurrentMorph()), mp);
Dispatcher.sendTo(new PacketAcquiredMorphs(cap.getAcquiredMorphs()), mp);
Dispatcher.sendTo(new PacketAcquiredMorphs(cap.getAcquiredMorphs(), cap.getLastSelectedMorph()), mp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public interface IMorphing
*/
public boolean setCurrentMorph(AbstractMorph morph, EntityPlayer player, boolean force);

/**
* Get morph of last survival morph attempt
*/
public AbstractMorph getLastSelectedMorph();

/**
* Set morph of last survival morph attempt
*/
public void setLastSelectedMorph(AbstractMorph morph);

/**
* Demorph this capability
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class Morphing implements IMorphing
*/
private AbstractMorph previousMorph;

/**
* Used for recent morph hotkey
*/
private AbstractMorph lastSelectedMorph;

/**
* Animation timer
*/
Expand Down Expand Up @@ -287,6 +292,22 @@ public boolean setCurrentMorph(AbstractMorph morph, EntityPlayer player, boolean
return false;
}

/**
* Get morph of last survival morph attempt
*/
public AbstractMorph getLastSelectedMorph()
{
return lastSelectedMorph;
}

/**
* Set morph of last survival morph attempt
*/
public void setLastSelectedMorph(AbstractMorph morph)
{
this.lastSelectedMorph = morph;
}

@Override
public void demorph(EntityPlayer player)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public NBTBase writeNBT(Capability<IMorphing> capability, IMorphing instance, En
tag.setTag("Morph", morph);
}

if (instance.getLastSelectedMorph() != null)
{
NBTTagCompound selectedMorph = new NBTTagCompound();
instance.getLastSelectedMorph().toNBT(selectedMorph);

tag.setTag("RecentMorph", selectedMorph);
}

tag.setTag("Morphs", acquired);

for (AbstractMorph acquiredMorph : instance.getAcquiredMorphs())
Expand All @@ -66,6 +74,7 @@ public void readNBT(Capability<IMorphing> capability, IMorphing instance, EnumFa
NBTTagCompound tag = (NBTTagCompound) nbt;
NBTTagList acquired = tag.getTagList("Morphs", 10);
NBTTagCompound morphTag = tag.getCompoundTag("Morph");
NBTTagCompound selectedMorphTag = tag.getCompoundTag("RecentMorph");

instance.setLastHealthRatio(tag.getFloat("LastHealthRatio"));
instance.setHasSquidAir(tag.getBoolean("HasSquidAir"));
Expand All @@ -75,6 +84,7 @@ public void readNBT(Capability<IMorphing> capability, IMorphing instance, EnumFa
if (!tag.hasNoTags())
{
instance.setCurrentMorph(MorphManager.INSTANCE.morphFromNBT(morphTag), null, true);
instance.setLastSelectedMorph(MorphManager.INSTANCE.morphFromNBT(selectedMorphTag));
}

List<AbstractMorph> acquiredMorphs = new ArrayList<AbstractMorph>();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/mchorse/metamorph/client/KeyboardHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class KeyboardHandler
public KeyBinding keySurvivalMenu;

/* Morph related keys */
public KeyBinding keyMorphRecent;
public KeyBinding keyDemorph;

public KeyboardHandler()
Expand All @@ -49,6 +50,7 @@ public KeyboardHandler()
keySelectorMenu = new KeyBinding("key.metamorph.selector_menu", Keyboard.KEY_MINUS, category);
keySurvivalMenu = new KeyBinding("key.metamorph.survival_menu", Keyboard.KEY_X, category);

keyMorphRecent = new KeyBinding("key.metamorph.morph_recent", Keyboard.KEY_RETURN, category);
keyDemorph = new KeyBinding("key.metamorph.demorph", Keyboard.KEY_PERIOD, category);

/* Register them in the client registry */
Expand All @@ -57,6 +59,7 @@ public KeyboardHandler()
ClientRegistry.registerKeyBinding(keySelectorMenu);
ClientRegistry.registerKeyBinding(keySurvivalMenu);

ClientRegistry.registerKeyBinding(keyMorphRecent);
ClientRegistry.registerKeyBinding(keyDemorph);
}

Expand Down Expand Up @@ -100,6 +103,16 @@ public void onKey(InputEvent.KeyInputEvent event)
mc.displayGuiScreen(ClientProxy.getSurvivalScreen().open());
wasUsed = true;
}

/* Morph into the most recent attempted morph */
if (this.keyMorphRecent.isPressed() && !spectator)
{
if (morphing != null && morphing.getLastSelectedMorph() != null)
{
MorphAPI.selectMorph(morphing.getLastSelectedMorph());
wasUsed = true;
}
}

/* Demorph from current morph */
if (this.keyDemorph.isPressed() && !spectator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public void run(EntityPlayerSP player, PacketAcquiredMorphs message)
IMorphing morphing = Morphing.get(player);

morphing.setAcquiredMorphs(message.morphs);
morphing.setLastSelectedMorph(message.lastSelectedMorph);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
public class PacketAcquiredMorphs implements IMessage
{
public List<AbstractMorph> morphs;
public AbstractMorph lastSelectedMorph;

public PacketAcquiredMorphs()
{
this.morphs = new ArrayList<AbstractMorph>();
}

public PacketAcquiredMorphs(List<AbstractMorph> morphs)
public PacketAcquiredMorphs(List<AbstractMorph> morphs, AbstractMorph lastSelectedMorph)
{
this.morphs = morphs;
this.lastSelectedMorph = lastSelectedMorph;
}

@Override
Expand All @@ -37,6 +39,8 @@ public void fromBytes(ByteBuf buf)
this.morphs.add(morph);
}
}

this.lastSelectedMorph = MorphUtils.morphFromBuf(buf);
}

@Override
Expand All @@ -48,5 +52,7 @@ public void toBytes(ByteBuf buf)
{
MorphUtils.morphToBuf(buf, morph);
}

MorphUtils.morphToBuf(buf, lastSelectedMorph);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void run(EntityPlayerMP player, PacketSelectMorph message)
morph = morphs.get(index);
}

if (morph != null)
{
capability.setLastSelectedMorph(morph);
}
MorphAPI.morph(player, MorphUtils.copy(morph), false);
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/metamorph/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ key.metamorph.action=Use action
key.metamorph.creative_menu=Open creative morph menu
key.metamorph.selector_menu=Open entity selectors menu
key.metamorph.survival_menu=Open survival morph menu
key.metamorph.morph_recent=Morph most recent
key.metamorph.demorph=Demorph (turn back to player)

# GUI
Expand Down

0 comments on commit 479a4c6

Please sign in to comment.