Skip to content

Commit

Permalink
Merge pull request #42 from MCRcortex/fabric
Browse files Browse the repository at this point in the history
Added matthew lattice cracker
  • Loading branch information
Earthcomputer authored Aug 7, 2019
2 parents 3f300b0 + fdefe8e commit af230b9
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 36 deletions.
256 changes: 256 additions & 0 deletions src/main/java/net/cortex/clientAddon/cracker/Lattice_cracker.java

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions src/main/java/net/cortex/clientAddon/cracker/SeedCracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package net.cortex.clientAddon.cracker;

import net.earthcomputer.clientcommands.features.EnchantmentCracker;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.network.packet.EntitySpawnS2CPacket;
import net.minecraft.entity.EntityType;
import net.minecraft.network.MessageType;
import net.minecraft.server.network.packet.PlayerMoveC2SPacket;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;

import java.util.Random;
import static net.earthcomputer.clientcommands.features.EnchantmentCracker.MULTIPLIER;

public class SeedCracker {
public interface OnCrack {void callback(long seed); }


public static OnCrack callback;
public static long[] bits=new long[20];
public static int expectedItems=0;
public static boolean cracking=false;

//returns True on success or false on failer
private static boolean throwItems()
{
ClientPlayerEntity player = MinecraftClient.getInstance().player;
player.setPositionAndAngles(player.x, player.y, player.z, 0, 90);
MinecraftClient.getInstance().getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.LookOnly(0, 90, true)); // point to correct location
for (int i = 0; i < 20; i++) {
EnchantmentCracker.EnchantManipulationStatus status = EnchantmentCracker.throwItem();
if (status != EnchantmentCracker.EnchantManipulationStatus.OK && status != EnchantmentCracker.EnchantManipulationStatus.NOT_CRACKED) {
MinecraftClient.getInstance().inGameHud.addChatMessage(MessageType.GAME_INFO, new TranslatableText("itemCrack.notEnoughItems").formatted(Formatting.RED));
EnchantmentCracker.LOGGER.info("Unable to use rng SeedCracker |not enough items|");
return false;
}
}
return true;
}
public static void attemptCrack()
{
cracking=false;
long seed= Lattice_cracker.crack(SeedCracker.bits);

if(seed==0)//Basicaly if seed is zero it means it failed to try to crack again
{
SeedCracker.crack(SeedCracker.callback);
return;
}
//Else, got a seed

Random rand=new Random();
rand.setSeed(seed ^ MULTIPLIER);
rand.nextFloat();
rand.nextFloat();
//rand.nextFloat();

/*
for(int i=0;i<13;i++) {
long x = (((long) (rand.nextFloat() * ((float) (1 << 24)))) >> (24 - 4))&0xFL;
System.out.print("Expected: "+padLeftZeros(Long.toBinaryString(x), 4)+" ");
System.out.print(padLeftZeros(Long.toBinaryString((((long) (rand.nextFloat() * ((float) (1 << 24)))) >> (24 - 4))&0xFL), 4)+" ");
System.out.print(padLeftZeros(Long.toBinaryString((((long) (rand.nextFloat() * ((float) (1 << 24)))) >> (24 - 4))&0xFL), 4)+" ");
System.out.print(padLeftZeros(Long.toBinaryString((((long) (rand.nextFloat() * ((float) (1 << 24)))) >> (24 - 4))&0xFL), 4)+" \n");
}*/

callback.callback(EnchantmentCracker.getSeed(rand));//extract seed and call callback
}
public static void crack(OnCrack Callback){
callback=Callback;
if(throwItems())
{
cracking=true;
expectedItems=20;
}
}

public static void onEntityCreation(EntitySpawnS2CPacket packet) {
if (packet.getEntityTypeId() == EntityType.ITEM && SeedCracker.expectedItems>0) {

long rand_val = (long) ((Math.atan2(packet.getVelocityz(), packet.getVelocityX()) + Math.PI) / (Math.PI * 2) * ((float) (1 << 24)));
long top_bits = rand_val;
short value = (short) (((top_bits >> (24 - 4)) ^ 0x8L )&0xFL);//INSTEAD OF ^0x8L MAYBE DO +math.pi OR SOMETHING ELSE
SeedCracker.bits[20-SeedCracker.expectedItems]=(long)value;//could be improved
SeedCracker.expectedItems--;
}
if(SeedCracker.expectedItems==0&&SeedCracker.cracking)//if its the last item
{
SeedCracker.attemptCrack();
}
/*
else
{
long rand_val = (long) ((Math.atan2(this.getVelocityz(), this.getVelocityX()) + Math.PI) / (Math.PI * 2) * ((float) (1 << 24)));
long top_bits = rand_val;
short value = (short) ((top_bits >> (24 - 4)) ^ 0x8L);
System.out.println("Entity item spawn: Top 4 bits of direction: "+padLeftZeros(Long.toBinaryString(value), 4));
}*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa
WikiCommand.register(dispatcher);
CEnchantCommand.register(dispatcher);

CrackRNGCommand.register(dispatcher);

if (MinecraftClient.getInstance().isIntegratedServerRunning()) {
CrackPlayerRNGCommand.register(dispatcher);
CheatCrackRNGCommand.register(dispatcher);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
import static net.earthcomputer.clientcommands.command.ClientCommandManager.*;
import static net.minecraft.server.command.CommandManager.*;

public class CrackPlayerRNGCommand {
public class CheatCrackRNGCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
addClientSideCommand("ccrackplayerrng");
addClientSideCommand("ccheatcrackrng");

dispatcher.register(literal("ccrackplayerrng")
dispatcher.register(literal("ccheatcrackrng")
.executes(ctx -> crackPlayerRNG(ctx.getSource())));
}

private static int crackPlayerRNG(ServerCommandSource source) {
long seed = EnchantmentCracker.singlePlayerCrackRNG();
sendFeedback(new TranslatableText("commands.ccrackplayerrng.success", Long.toHexString(seed)));
sendFeedback(new TranslatableText("commands.ccrackrng.success", Long.toHexString(seed)));
return (int) seed;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.earthcomputer.clientcommands.command;

import com.mojang.brigadier.CommandDispatcher;
import net.cortex.clientAddon.cracker.SeedCracker;
import net.earthcomputer.clientcommands.TempRules;
import net.earthcomputer.clientcommands.features.EnchantmentCracker;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;

import static net.earthcomputer.clientcommands.command.ClientCommandManager.addClientSideCommand;
import static net.earthcomputer.clientcommands.command.ClientCommandManager.sendFeedback;
import static net.earthcomputer.clientcommands.features.EnchantmentCracker.EnumCrackState.CRACKED_PLAYER_SEED;
import static net.earthcomputer.clientcommands.features.EnchantmentCracker.MULTIPLIER;
import static net.minecraft.server.command.CommandManager.literal;

public class CrackRNGCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
addClientSideCommand("ccrackrng");

dispatcher.register(literal("ccrackrng")
.executes(ctx -> crackPlayerRNG(ctx.getSource())));
}

private static int crackPlayerRNG(ServerCommandSource source) {
SeedCracker.crack(seed -> {
sendFeedback(new TranslatableText("commands.ccrackrng.success", Long.toHexString(seed)));
EnchantmentCracker.playerRand.setSeed(seed ^ MULTIPLIER);
TempRules.enchCrackState=CRACKED_PLAYER_SEED;
});
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class EnchantmentCracker {
* we want and determine n.
*/

private static final Logger LOGGER = LogManager.getLogger("EnchantmentCracker");
public static final Logger LOGGER = LogManager.getLogger("EnchantmentCracker");

// RNG CHECK
/*
Expand Down Expand Up @@ -287,14 +287,14 @@ public static void drawEnchantmentGUIOverlay() {
* This section is in charge of the logic of the cracking
*/

private static final long MULTIPLIER = 0x5deece66dL;
private static final long ADDEND = 0xbL;
private static final long MASK = (1L << 48) - 1;
public static final long MULTIPLIER = 0x5deece66dL;
public static final long ADDEND = 0xbL;
public static final long MASK = (1L << 48) - 1;

private static Set<Integer> possibleXPSeeds = new HashSet<>(1 << 20);
private static boolean onFirstXPSeed = true;
private static Set<Long> possiblePlayerRandSeeds = new HashSet<>(1 << 16);
private static Random playerRand = new Random();
public static Random playerRand = new Random();
private static boolean doneEnchantment = false;
public static BlockPos enchantingTablePos = null;

Expand Down Expand Up @@ -558,13 +558,8 @@ public void body() {

@Override
public void onCompleted() {
Slot matchingSlot = player.container.slotList.stream()
.filter(Slot::hasStack).findAny().orElse(null);
assert matchingSlot != null;
expectedThrows++;
for (int i = 0; i < 4; i++) playerRand.nextInt();

MinecraftClient.getInstance().interactionManager.method_2906(player.container.syncId, matchingSlot.id, 0, SlotActionType.THROW, player);
EnchantManipulationStatus status = throwItem();
assert status == EnchantManipulationStatus.OK;

scheduleDelay();
}
Expand Down Expand Up @@ -615,12 +610,11 @@ public static EnchantManipulationStatus throwItemsUntil(Predicate<Random> condit
}
*/

/*
public static EnchantManipulationStatus throwItemsUntil(Predicate<Random> condition, int max) {
if (TempRules.enchCrackState != EnumCrackState.CRACKED)
return EnchantManipulationStatus.NOT_CRACKED;

long seed = ReflectionHelper.<AtomicLong, Random>getPrivateValue(Random.class, playerRand, "seed").get();
long seed = getSeed(playerRand);
Random rand = new Random(seed ^ MULTIPLIER);

int itemsNeeded = 0;
Expand All @@ -632,28 +626,37 @@ public static EnchantManipulationStatus throwItemsUntil(Predicate<Random> condit
if (itemsNeeded > max)
return EnchantManipulationStatus.IMPOSSIBLE;

EntityPlayerSP player = Minecraft.getMinecraft().player;
for (int i = 0; i < itemsNeeded; i++) {
EnchantManipulationStatus status = manipulateEnchantmentsSanityCheck(player);
EnchantManipulationStatus status = throwItem();
if (status != EnchantManipulationStatus.OK)
return status;
Slot matchingSlot = player.inventoryContainer.inventorySlots.stream()
.filter(Slot::getHasStack).findAny().orElse(null);
if (matchingSlot == null) {
return EnchantManipulationStatus.EMPTY_INVENTORY;
}
}

return EnchantManipulationStatus.OK;
}

public static EnchantManipulationStatus throwItem() {
ClientPlayerEntity player = MinecraftClient.getInstance().player;

EnchantManipulationStatus status = manipulateEnchantmentsSanityCheck(player);
if (status != EnchantManipulationStatus.OK && status != EnchantManipulationStatus.NOT_CRACKED)
return status;
Slot matchingSlot = player.container.slotList.stream()
.filter(Slot::hasStack).findAny().orElse(null);
if (matchingSlot == null) {
return EnchantManipulationStatus.EMPTY_INVENTORY;
}
if (status != EnchantManipulationStatus.NOT_CRACKED) {
expectedThrows++;
for (int j = 0; j < 4; j++) {
playerRand.nextInt();
}
Minecraft.getMinecraft().playerController.windowClick(player.inventoryContainer.windowId,
matchingSlot.slotNumber, 0, ClickType.THROW, player);
}
MinecraftClient.getInstance().interactionManager.method_2906(player.container.syncId,
matchingSlot.id, 0, SlotActionType.THROW, player);

return EnchantManipulationStatus.OK;
return status;
}
*/

public static long singlePlayerCrackRNG() {
ServerPlayerEntity serverPlayer = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
Expand Down Expand Up @@ -763,7 +766,7 @@ public static List<InfoEnchantment> getEnchantmentsInTable(int slot) {
}
RANDOM_SEED.setAccessible(true);
}
private static long getSeed(Random rand) {
public static long getSeed(Random rand) {
try {
return ((AtomicLong) RANDOM_SEED.get(rand)).get();
} catch (ReflectiveOperationException e) {
Expand All @@ -787,4 +790,4 @@ public String asString() {
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.earthcomputer.clientcommands.mixin;

import com.mojang.brigadier.CommandDispatcher;
import net.cortex.clientAddon.cracker.SeedCracker;
import net.earthcomputer.clientcommands.ClientCommands;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.packet.CommandTreeS2CPacket;
import net.minecraft.client.network.packet.EntitySpawnS2CPacket;
import net.minecraft.server.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -24,4 +26,9 @@ public void onOnCommandTree(CommandTreeS2CPacket packet, CallbackInfo ci) {
ClientCommands.registerCommands((CommandDispatcher<ServerCommandSource>) (Object) commandDispatcher);
}

@Inject(method = "onEntitySpawn", at = @At("RETURN"))
public void onOnEntitySpawn(EntitySpawnS2CPacket packet, CallbackInfo ci) {
SeedCracker.onEntityCreation(packet);
}

}
6 changes: 4 additions & 2 deletions src/main/resources/assets/clientcommands/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"commands.ccalc.expected": "Expected %s",
"commands.ccalc.invalidArgumentCount": "Function \"%s\" cannot take %d arguments",

"commands.ccrackplayerrng.success": "Player RNG cracked: %d",
"commands.ccrackrng.success": "Player RNG cracked: %d",

"commands.cenchant.expectedWithWithout": "Expected \"with\"/\"without\"",
"commands.cenchant.incompatible": "Incompatible enchantments",
Expand Down Expand Up @@ -93,6 +93,8 @@

"enchCrack.addInfo": "Add Info",

"enchCrack.toolBreakWarning": "Warning: tool has %d durability left"
"enchCrack.toolBreakWarning": "Warning: tool has %d durability left",

"itemCrack.notEnoughItems": "Unable to use RNG SeedCracker: Not Enough Items in Player Hand"

}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"fabric": "*"
},
"mixins": {
"client": "mixins.clientcommands.json"
"client": ["mixins.clientcommands.json"]
}
}

0 comments on commit af230b9

Please sign in to comment.