-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make /cfinditem much more powerful when the player is opped
- Loading branch information
1 parent
18cf23a
commit 337cb78
Showing
4 changed files
with
307 additions
and
29 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/net/earthcomputer/clientcommands/ClientcommandsDataQueryHandler.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,65 @@ | ||
package net.earthcomputer.clientcommands; | ||
|
||
import net.minecraft.client.network.ClientPlayNetworkHandler; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.network.packet.c2s.play.QueryBlockNbtC2SPacket; | ||
import net.minecraft.network.packet.c2s.play.QueryEntityNbtC2SPacket; | ||
import net.minecraft.util.math.BlockPos; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
public class ClientcommandsDataQueryHandler { | ||
private final ClientPlayNetworkHandler networkHandler; | ||
private final List<CallbackEntry> callbacks = new ArrayList<>(); | ||
|
||
public ClientcommandsDataQueryHandler(ClientPlayNetworkHandler networkHandler) { | ||
this.networkHandler = networkHandler; | ||
} | ||
|
||
public static ClientcommandsDataQueryHandler get(ClientPlayNetworkHandler networkHandler) { | ||
return ((IClientPlayNetworkHandler) networkHandler).clientcommands_getCCDataQueryHandler(); | ||
} | ||
|
||
public boolean handleQueryResponse(int transactionId, @Nullable NbtCompound nbt) { | ||
Iterator<CallbackEntry> callbacks = this.callbacks.iterator(); | ||
while (callbacks.hasNext()) { | ||
CallbackEntry callback = callbacks.next(); | ||
if (callback.transactionId - transactionId > 0) { | ||
break; | ||
} | ||
callbacks.remove(); | ||
if (callback.transactionId == transactionId) { | ||
callback.callback.accept(nbt); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private int nextQuery(Consumer<@Nullable NbtCompound> callback) { | ||
int transactionId = ++networkHandler.getDataQueryHandler().expectedTransactionId; | ||
callbacks.add(new CallbackEntry(transactionId, callback)); | ||
return transactionId; | ||
} | ||
|
||
public void queryEntityNbt(int entityNetworkId, Consumer<@Nullable NbtCompound> callback) { | ||
int transactionId = nextQuery(callback); | ||
networkHandler.sendPacket(new QueryEntityNbtC2SPacket(transactionId, entityNetworkId)); | ||
} | ||
|
||
public void queryBlockNbt(BlockPos pos, Consumer<@Nullable NbtCompound> callback) { | ||
int transactionId = nextQuery(callback); | ||
networkHandler.sendPacket(new QueryBlockNbtC2SPacket(transactionId, pos)); | ||
} | ||
|
||
private record CallbackEntry(int transactionId, Consumer<@Nullable NbtCompound> callback) { | ||
} | ||
|
||
public interface IClientPlayNetworkHandler { | ||
ClientcommandsDataQueryHandler clientcommands_getCCDataQueryHandler(); | ||
} | ||
} |
Oops, something went wrong.