forked from tonio-cartonio/thax-public
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created new selection service that will auto revert mod-set selections
- Loading branch information
Showing
4 changed files
with
175 additions
and
60 deletions.
There are no files selected for viewing
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
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
112 changes: 112 additions & 0 deletions
112
src/main/java/com/matt/forgehax/mods/services/HotbarSelectionService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.matt.forgehax.mods.services; | ||
|
||
import static com.matt.forgehax.Helper.getLocalPlayer; | ||
import static com.matt.forgehax.Helper.getWorld; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import com.google.common.base.Predicates; | ||
import com.matt.forgehax.util.entity.LocalPlayerInventory; | ||
import com.matt.forgehax.util.mod.ServiceMod; | ||
import com.matt.forgehax.util.mod.loader.RegisterMod; | ||
import java.util.function.Predicate; | ||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent; | ||
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase; | ||
|
||
@RegisterMod | ||
public class HotbarSelectionService extends ServiceMod { | ||
private static HotbarSelectionService instance = null; | ||
|
||
public static HotbarSelectionService getInstance() { | ||
return instance; | ||
} | ||
|
||
private int originalIndex = -1; | ||
private long ticksElapsed = -1; | ||
|
||
private int lastSetIndex = -1; | ||
private Predicate<Long> resetCondition = Predicates.alwaysTrue(); | ||
|
||
public HotbarSelectionService() { | ||
super("HotbarSelectionService"); | ||
instance = this; | ||
} | ||
|
||
public ResetFunction setSelected(final int index, boolean reset, Predicate<Long> condition) { | ||
if (index < 0 || index > LocalPlayerInventory.getHotbarSize() - 1) | ||
throw new IllegalArgumentException( | ||
"index must be between 0 and " + (LocalPlayerInventory.getHotbarSize() - 1)); | ||
|
||
final int orig = selected(); | ||
|
||
if (!reset) { | ||
select(index); | ||
if (originalIndex != -1) originalIndex = index; | ||
|
||
return () -> select(index); | ||
} else { | ||
if ((originalIndex == -1 ? orig : originalIndex) != index) { | ||
if (originalIndex == -1) originalIndex = orig; | ||
|
||
lastSetIndex = index; | ||
resetCondition = MoreObjects.firstNonNull(condition, Predicates.alwaysTrue()); | ||
|
||
select(index); | ||
} | ||
ticksElapsed = 0; | ||
|
||
return () -> { | ||
if(index == selected() && lastSetIndex == index) { | ||
select(orig); | ||
reset(); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
public void resetSelected() { | ||
if (originalIndex != -1 && selected() == lastSetIndex) select(originalIndex); | ||
reset(); | ||
} | ||
|
||
private void reset() { | ||
originalIndex = -1; | ||
ticksElapsed = -1; | ||
lastSetIndex = -1; | ||
resetCondition = Predicates.alwaysTrue(); | ||
} | ||
|
||
@SubscribeEvent | ||
public void onClientTick(ClientTickEvent event) { | ||
if (getWorld() == null || getLocalPlayer() == null) { | ||
reset(); | ||
return; | ||
} | ||
|
||
switch (event.phase) { | ||
case START: | ||
{ | ||
if (originalIndex != -1 && resetCondition.test(ticksElapsed)) resetSelected(); | ||
if(ticksElapsed != -1) ++ticksElapsed; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// | ||
// | ||
// | ||
|
||
private static void select(int index) { | ||
if (getLocalPlayer() == null) return; | ||
LocalPlayerInventory.getInventory().currentItem = index; | ||
} | ||
|
||
private static int selected() { | ||
return getLocalPlayer() == null ? -1 : LocalPlayerInventory.getSelected().getIndex(); | ||
} | ||
|
||
public interface ResetFunction { | ||
void revert(); | ||
} | ||
} |
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