From 9f951f261df9684c83ed9633e111b8a3c156d6a5 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Tue, 23 Jul 2019 15:39:19 -0700 Subject: [PATCH] fixes --- .../api/utils/ExampleBaritoneControl.java | 24 ++------ .../mixins/MixinClientChunkProvider.java | 39 ------------- .../launch/mixins/MixinLootContext.java | 58 +++++++++++++++++++ .../launch/mixins/MixinMinecraft.java | 1 - src/launch/resources/mixins.baritone.json | 2 + .../java/baritone/process/MineProcess.java | 42 ++++++++++++-- .../baritone/utils/BlockStateInterface.java | 23 +++----- .../baritone/utils/PlayerMovementInput.java | 3 +- .../utils/accessor/IChunkProviderClient.java | 25 -------- 9 files changed, 112 insertions(+), 105 deletions(-) delete mode 100644 src/launch/java/baritone/launch/mixins/MixinClientChunkProvider.java create mode 100644 src/launch/java/baritone/launch/mixins/MixinLootContext.java delete mode 100644 src/main/java/baritone/utils/accessor/IChunkProviderClient.java diff --git a/src/api/java/baritone/api/utils/ExampleBaritoneControl.java b/src/api/java/baritone/api/utils/ExampleBaritoneControl.java index 269033e5e..9b63d0b82 100644 --- a/src/api/java/baritone/api/utils/ExampleBaritoneControl.java +++ b/src/api/java/baritone/api/utils/ExampleBaritoneControl.java @@ -35,6 +35,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientChunkProvider; import net.minecraft.crash.CrashReport; import net.minecraft.entity.Entity; +import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; @@ -219,23 +220,6 @@ public class ExampleBaritoneControl implements Helper, AbstractGameEventListener } return true; } - /*if (msg.equals("fullpath")) { - if (pathingBehavior.getGoal() == null) { - logDirect("No goal."); - } else { - logDirect("Started segmented calculator"); - SegmentedCalculator.calculateSegmentsThreaded(pathingBehavior.pathStart(), pathingBehavior.getGoal(), new CalculationContext(baritone, true), ipath -> { - logDirect("Found a path"); - logDirect("Ends at " + ipath.getDest()); - logDirect("Length " + ipath.length()); - logDirect("Estimated time " + ipath.ticksRemainingFrom(0)); - pathingBehavior.secretCursedFunctionDoNotCall(ipath); // it's okay when *I* do it - }, () -> { - logDirect("Path calculation failed, no path"); - }); - } - return true; - }*/ if (msg.equals("proc")) { Optional proc = baritone.getPathingControlManager().mostRecentInControl(); if (!proc.isPresent()) { @@ -418,10 +402,10 @@ public class ExampleBaritoneControl implements Helper, AbstractGameEventListener logDirect("Following any players"); return true; } - /*if (msg.startsWith("followentity")) { + if (msg.startsWith("followentity")) { String name = msg.substring(12).trim(); Optional toFollow = Optional.empty(); - for (Entity entity : ctx.world().loadedEntityList) { + for (Entity entity : ctx.entities()) { String entityName = entity.getName().getFormattedText().trim().toLowerCase(); if ((entityName.contains(name) || name.contains(entityName)) && !(entity instanceof ItemEntity || entity instanceof PlayerEntity)) { // We dont want it following players while `#follow` exists. toFollow = Optional.of(entity); @@ -435,7 +419,7 @@ public class ExampleBaritoneControl implements Helper, AbstractGameEventListener baritone.getFollowProcess().follow(effectivelyFinal::equals); logDirect("Following entity " + toFollow.get()); return true; - }*/ + } if (msg.startsWith("follow")) { String name = msg.substring(6).trim(); Optional toFollow = Optional.empty(); diff --git a/src/launch/java/baritone/launch/mixins/MixinClientChunkProvider.java b/src/launch/java/baritone/launch/mixins/MixinClientChunkProvider.java deleted file mode 100644 index cdfd1cf56..000000000 --- a/src/launch/java/baritone/launch/mixins/MixinClientChunkProvider.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of Baritone. - * - * Baritone is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Baritone is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Baritone. If not, see . - */ - -package baritone.launch.mixins; - -import baritone.utils.accessor.IChunkProviderClient; -import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -import net.minecraft.client.multiplayer.ClientChunkProvider; -import net.minecraft.world.chunk.Chunk; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -@Mixin(ClientChunkProvider.class) -public class MixinClientChunkProvider implements IChunkProviderClient { - - @Shadow - @Final - private Long2ObjectMap loadedChunks; - - @Override - public Long2ObjectMap loadedChunks() { - return this.loadedChunks; - } -} diff --git a/src/launch/java/baritone/launch/mixins/MixinLootContext.java b/src/launch/java/baritone/launch/mixins/MixinLootContext.java new file mode 100644 index 000000000..86beea446 --- /dev/null +++ b/src/launch/java/baritone/launch/mixins/MixinLootContext.java @@ -0,0 +1,58 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.launch.mixins; + +import baritone.process.MineProcess; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.server.ServerWorld; +import net.minecraft.world.storage.loot.LootContext; +import net.minecraft.world.storage.loot.LootTableManager; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(LootContext.Builder.class) +public class MixinLootContext { + @Redirect( + method = "build", + at = @At( + value = "INVOKE", + target = "net/minecraft/world/server/ServerWorld.getServer()Lnet/minecraft/server/MinecraftServer;" + ) + ) + private MinecraftServer getServer(ServerWorld world) { + if (world == null) { + return null; + } + return world.getServer(); + } + + @Redirect( + method = "build", + at = @At( + value = "INVOKE", + target = "net/minecraft/server/MinecraftServer.getLootTableManager()Lnet/minecraft/world/storage/loot/LootTableManager;" + ) + ) + private LootTableManager getLootTableManager(MinecraftServer server) { + if (server == null) { + return MineProcess.getManager(); + } + return server.getLootTableManager(); + } +} diff --git a/src/launch/java/baritone/launch/mixins/MixinMinecraft.java b/src/launch/java/baritone/launch/mixins/MixinMinecraft.java index 0c2861135..557c184bd 100644 --- a/src/launch/java/baritone/launch/mixins/MixinMinecraft.java +++ b/src/launch/java/baritone/launch/mixins/MixinMinecraft.java @@ -32,7 +32,6 @@ import net.minecraft.client.world.ClientWorld; import net.minecraft.item.ItemStack; import net.minecraft.util.ActionResultType; import net.minecraft.util.Hand; -import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockRayTraceResult; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.Mixin; diff --git a/src/launch/resources/mixins.baritone.json b/src/launch/resources/mixins.baritone.json index 171ff4561..c908ccafd 100644 --- a/src/launch/resources/mixins.baritone.json +++ b/src/launch/resources/mixins.baritone.json @@ -12,8 +12,10 @@ "MixinClientPlayerEntity", "MixinClientPlayNetHandler", "MixinEntity", + "MixinEntityRenderManager", "MixinGameRenderer", "MixinLivingEntity", + "MixinLootContext", "MixinMinecraft", "MixinNetworkManager" ] diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index b7982ea76..0edbdf4e2 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -39,12 +39,15 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.item.ItemEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.resources.*; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.Unit; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import net.minecraft.world.storage.loot.LootContext; -import net.minecraft.world.storage.loot.LootParameters; +import net.minecraft.world.storage.loot.*; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import static baritone.api.pathing.movement.ActionCosts.COST_INF; @@ -66,6 +69,9 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro private int desiredQuantity; private int tickCount; + private static LootTableManager manager; + private static Map> drops; + public MineProcess(Baritone baritone) { super(baritone); } @@ -143,6 +149,35 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro return command; } + public static LootTableManager getManager() { + if (manager == null) { + ResourcePackList rpl = new ResourcePackList<>(ResourcePackInfo::new); + rpl.addPackFinder(new ServerPackFinder()); + rpl.reloadPacksFromFinders(); + IResourcePack thePack = ((ResourcePackInfo) rpl.getAllPacks().iterator().next()).getResourcePack(); + IReloadableResourceManager resourceManager = new SimpleReloadableResourceManager(ResourcePackType.SERVER_DATA, null); + manager = new LootTableManager(); + resourceManager.addReloadListener(manager); + try { + resourceManager.reloadResourcesAndThen(Baritone.getExecutor(), Baritone.getExecutor(), Collections.singletonList(thePack), CompletableFuture.completedFuture(Unit.INSTANCE)).get(); + } catch (Exception exception) { + throw new RuntimeException(exception); + } + } + return manager; + } + + private static List drops(Block b) { + return drops.computeIfAbsent(b, block -> { + ResourceLocation lootTableLocation = block.getLootTable(); + if (lootTableLocation == LootTables.EMPTY) { + return Collections.emptyList(); + } else { + return getManager().getLootTableFromLocation(lootTableLocation).generate(new LootContext.Builder(null).withRandom(new Random()).withParameter(LootParameters.POSITION, BlockPos.ZERO).withParameter(LootParameters.TOOL, ItemStack.EMPTY).withNullableParameter(LootParameters.BLOCK_ENTITY, null).withParameter(LootParameters.BLOCK_STATE, block.getDefaultState()).build(LootParameterSets.BLOCK)).stream().map(ItemStack::getItem).collect(Collectors.toList()); + } + }); + } + @Override public void onLostControl() { mine(0, (Block[]) null); @@ -283,9 +318,6 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro } } - public static List drops(Block block) { - return block.getDefaultState().getDrops(new LootContext.Builder(null).withRandom(new Random()).withParameter(LootParameters.POSITION, BlockPos.ZERO).withParameter(LootParameters.TOOL, ItemStack.EMPTY).withNullableParameter(LootParameters.BLOCK_ENTITY, null)).stream().map(ItemStack::getItem).collect(Collectors.toList()); - } public static List droppedItemsScan(List mining, World world) { if (!Baritone.settings().mineScanDroppedItems.value) { diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index 1273eb7b2..7dfd153e3 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -21,18 +21,16 @@ import baritone.Baritone; import baritone.api.utils.IPlayerContext; import baritone.cache.CachedRegion; import baritone.cache.WorldData; -import baritone.utils.accessor.IChunkProviderClient; -import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientChunkProvider; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.ChunkPos; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.ChunkSection; +import net.minecraft.world.chunk.ChunkStatus; /** * Wraps get for chuck caching capability @@ -41,7 +39,7 @@ import net.minecraft.world.chunk.ChunkSection; */ public class BlockStateInterface { - private final Long2ObjectMap loadedChunks; + private final ClientChunkProvider provider; private final WorldData worldData; private Chunk prev = null; @@ -61,11 +59,9 @@ public class BlockStateInterface { public BlockStateInterface(World world, WorldData worldData, boolean copyLoadedChunks) { this.worldData = worldData; - Long2ObjectMap worldLoaded = ((IChunkProviderClient) world.getChunkProvider()).loadedChunks(); - if (copyLoadedChunks) { - this.loadedChunks = new Long2ObjectOpenHashMap<>(worldLoaded); // make a copy that we can safely access from another thread - } else { - this.loadedChunks = worldLoaded; // this will only be used on the main thread + this.provider = (ClientChunkProvider) world.getChunkProvider(); + if (copyLoadedChunks) { // todo + System.out.println("Really gotta do this"); } this.useTheRealWorld = !Baritone.settings().pathThroughCachedOnly.value; if (!Minecraft.getInstance().isOnExecutionThread()) { @@ -74,7 +70,7 @@ public class BlockStateInterface { } public boolean worldContainsLoadedChunk(int blockX, int blockZ) { - return loadedChunks.containsKey(ChunkPos.asLong(blockX >> 4, blockZ >> 4)); + return provider.chunkExists(blockX >> 4, blockZ >> 4); } public static Block getBlock(IPlayerContext ctx, BlockPos pos) { // won't be called from the pathing thread because the pathing thread doesn't make a single blockpos pog @@ -109,8 +105,7 @@ public class BlockStateInterface { if (cached != null && cached.getPos().x == x >> 4 && cached.getPos().z == z >> 4) { return getFromChunk(cached, x, y, z); } - Chunk chunk = loadedChunks.get(ChunkPos.asLong(x >> 4, z >> 4)); - + Chunk chunk = provider.getChunk(x >> 4, z >> 4, ChunkStatus.FULL, false); if (chunk != null && !chunk.isEmpty()) { prev = chunk; return getFromChunk(chunk, x, y, z); @@ -142,7 +137,7 @@ public class BlockStateInterface { if (prevChunk != null && prevChunk.getPos().x == x >> 4 && prevChunk.getPos().z == z >> 4) { return true; } - prevChunk = loadedChunks.get(ChunkPos.asLong(x >> 4, z >> 4)); + prevChunk = provider.getChunk(x >> 4, z >> 4, ChunkStatus.FULL, false); if (prevChunk != null && !prevChunk.isEmpty()) { prev = prevChunk; return true; diff --git a/src/main/java/baritone/utils/PlayerMovementInput.java b/src/main/java/baritone/utils/PlayerMovementInput.java index f41e0f629..df89dc789 100644 --- a/src/main/java/baritone/utils/PlayerMovementInput.java +++ b/src/main/java/baritone/utils/PlayerMovementInput.java @@ -27,7 +27,8 @@ public class PlayerMovementInput extends MovementInput { this.handler = handler; } - public void updatePlayerMoveState() { + @Override + public void tick(boolean p_217607_1_, boolean p_217607_2_) { this.moveStrafe = 0.0F; this.moveForward = 0.0F; diff --git a/src/main/java/baritone/utils/accessor/IChunkProviderClient.java b/src/main/java/baritone/utils/accessor/IChunkProviderClient.java deleted file mode 100644 index 19f146854..000000000 --- a/src/main/java/baritone/utils/accessor/IChunkProviderClient.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * This file is part of Baritone. - * - * Baritone is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Baritone is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Baritone. If not, see . - */ - -package baritone.utils.accessor; - -import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -import net.minecraft.world.chunk.Chunk; - -public interface IChunkProviderClient { - Long2ObjectMap loadedChunks(); -}