-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We now rethrow items when the server rejects an item throw. Also added a config option for item throw speed.
- Loading branch information
1 parent
b4b3ee2
commit e7b9ec1
Showing
9 changed files
with
212 additions
and
73 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
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
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
125 changes: 125 additions & 0 deletions
125
src/main/java/net/earthcomputer/clientcommands/task/ItemThrowTask.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,125 @@ | ||
package net.earthcomputer.clientcommands.task; | ||
|
||
import com.mojang.logging.LogUtils; | ||
import net.earthcomputer.clientcommands.Configs; | ||
import net.earthcomputer.clientcommands.features.PlayerRandCracker; | ||
import net.earthcomputer.clientcommands.features.SuggestionsHook; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.player.LocalPlayer; | ||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; | ||
import org.slf4j.Logger; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.Set; | ||
|
||
public class ItemThrowTask extends SimpleTask { | ||
private static final Logger LOGGER = LogUtils.getLogger(); | ||
|
||
private static final Set<Object> MUTEX_KEYS = Set.of(ItemThrowTask.class); | ||
|
||
public static final int FLAG_URGENT = 1; | ||
public static final int FLAG_WAIT_FOR_ITEMS = 2; | ||
|
||
private static WeakReference<ItemThrowTask> currentThrowTask = null; | ||
|
||
private final int totalItemsToThrow; | ||
private final int flags; | ||
|
||
private int confirmedItemThrows; | ||
private int sentItemThrows; | ||
private float itemThrowsAllowedThisTick; | ||
private boolean waitingFence = false; | ||
private boolean failed = false; | ||
|
||
public ItemThrowTask(int itemsToThrow) { | ||
this(itemsToThrow, 0); | ||
} | ||
|
||
public ItemThrowTask(int itemsToThrow, int flags) { | ||
this.totalItemsToThrow = itemsToThrow; | ||
this.flags = flags; | ||
} | ||
|
||
@Override | ||
public boolean condition() { | ||
return waitingFence || sentItemThrows != totalItemsToThrow || sentItemThrows > confirmedItemThrows; | ||
} | ||
|
||
@Override | ||
protected void onTick() { | ||
itemThrowsAllowedThisTick += Configs.itemThrowsPerTick; | ||
|
||
while (((flags & FLAG_URGENT) != 0 || itemThrowsAllowedThisTick >= 1) && sentItemThrows < totalItemsToThrow) { | ||
itemThrowsAllowedThisTick--; | ||
if (!PlayerRandCracker.throwItem()) { | ||
if ((flags & FLAG_WAIT_FOR_ITEMS) != 0) { | ||
return; | ||
} | ||
failed = true; | ||
_break(); | ||
onFailedToThrowItem(); | ||
return; | ||
} | ||
sentItemThrows++; | ||
} | ||
|
||
if (!waitingFence && sentItemThrows == totalItemsToThrow && confirmedItemThrows < sentItemThrows) { | ||
waitingFence = true; | ||
SuggestionsHook.fence().thenAccept(v -> { | ||
if (sentItemThrows > confirmedItemThrows) { | ||
LOGGER.info("Server rejected {} item throws. Rethrowing them.", sentItemThrows - confirmedItemThrows); | ||
while (sentItemThrows > confirmedItemThrows) { | ||
PlayerRandCracker.unthrowItem(); | ||
sentItemThrows--; | ||
} | ||
} | ||
waitingFence = false; | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
currentThrowTask = new WeakReference<>(this); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
if (!failed) { | ||
onSuccess(); | ||
} | ||
currentThrowTask = null; | ||
} | ||
|
||
@Override | ||
public Set<Object> getMutexKeys() { | ||
return MUTEX_KEYS; | ||
} | ||
|
||
protected void onFailedToThrowItem() { | ||
} | ||
|
||
protected void onSuccess() { | ||
} | ||
|
||
protected void onItemSpawn(ClientboundAddEntityPacket packet) { | ||
} | ||
|
||
public static void handleItemSpawn(ClientboundAddEntityPacket packet) { | ||
ItemThrowTask task = currentThrowTask == null ? null : currentThrowTask.get(); | ||
if (task == null) { | ||
return; | ||
} | ||
|
||
LocalPlayer player = Minecraft.getInstance().player; | ||
if (player == null) { | ||
return; | ||
} | ||
if (player.getEyePosition().distanceToSqr(packet.getX(), packet.getY(), packet.getZ()) > 1) { | ||
return; | ||
} | ||
|
||
task.confirmedItemThrows++; | ||
task.onItemSpawn(packet); | ||
} | ||
} |
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
Oops, something went wrong.