|
|
|
|
@@ -19,30 +19,26 @@ package baritone.api.utils;
|
|
|
|
|
|
|
|
|
|
import baritone.api.utils.accessor.IItemStack;
|
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
|
|
|
import io.netty.util.concurrent.ThreadPerTaskExecutor;
|
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
|
import net.minecraft.SharedConstants;
|
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
|
import net.minecraft.resources.ResourceKey;
|
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
|
import net.minecraft.server.level.progress.ChunkProgressListener;
|
|
|
|
|
import net.minecraft.server.packs.*;
|
|
|
|
|
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
|
|
|
|
import net.minecraft.server.packs.repository.PackRepository;
|
|
|
|
|
import net.minecraft.server.packs.repository.ServerPacksSource;
|
|
|
|
|
import net.minecraft.server.packs.resources.MultiPackResourceManager;
|
|
|
|
|
import net.minecraft.server.packs.resources.ReloadableResourceManager;
|
|
|
|
|
import net.minecraft.util.RandomSource;
|
|
|
|
|
import net.minecraft.util.Unit;
|
|
|
|
|
import net.minecraft.world.flag.FeatureFlagSet;
|
|
|
|
|
import net.minecraft.world.flag.FeatureFlags;
|
|
|
|
|
import net.minecraft.world.item.Item;
|
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
|
import net.minecraft.world.level.CustomSpawner;
|
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
|
import net.minecraft.world.level.block.Block;
|
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
|
import net.minecraft.world.level.dimension.LevelStem;
|
|
|
|
|
import net.minecraft.world.level.storage.LevelStorageSource;
|
|
|
|
|
import net.minecraft.world.level.storage.ServerLevelData;
|
|
|
|
|
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
|
|
|
|
import net.minecraft.world.level.storage.loot.LootContext;
|
|
|
|
|
import net.minecraft.world.level.storage.loot.LootTables;
|
|
|
|
|
@@ -50,14 +46,13 @@ import net.minecraft.world.level.storage.loot.PredicateManager;
|
|
|
|
|
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
|
|
|
|
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
|
|
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
|
import sun.misc.Unsafe;
|
|
|
|
|
import org.apache.logging.log4j.core.jmx.Server;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
import java.util.regex.MatchResult;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
@@ -69,7 +64,7 @@ public final class BlockOptionalMeta {
|
|
|
|
|
private final ImmutableSet<Integer> stateHashes;
|
|
|
|
|
private final ImmutableSet<Integer> stackHashes;
|
|
|
|
|
private static final Pattern pattern = Pattern.compile("^(.+?)(?::(\\d+))?$");
|
|
|
|
|
private static LootTables lootTables;
|
|
|
|
|
private static LootTables manager;
|
|
|
|
|
private static PredicateManager predicate = new PredicateManager();
|
|
|
|
|
private static Map<Block, List<Item>> drops = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
@@ -173,11 +168,11 @@ public final class BlockOptionalMeta {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static LootTables getManager() {
|
|
|
|
|
if (lootTables == null) {
|
|
|
|
|
if (manager == null) {
|
|
|
|
|
MultiPackResourceManager resources = new MultiPackResourceManager(PackType.SERVER_DATA, List.of(getVanillaServerPack()));
|
|
|
|
|
ReloadableResourceManager resourceManager = new ReloadableResourceManager(PackType.SERVER_DATA);
|
|
|
|
|
lootTables = new LootTables(predicate);
|
|
|
|
|
resourceManager.registerReloadListener(lootTables);
|
|
|
|
|
manager = new LootTables(predicate);
|
|
|
|
|
resourceManager.registerReloadListener(manager);
|
|
|
|
|
try {
|
|
|
|
|
resourceManager.createReload(new ThreadPerTaskExecutor(Thread::new), new ThreadPerTaskExecutor(Thread::new), CompletableFuture.completedFuture(Unit.INSTANCE), resources.listPacks().toList()).done().get();
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
@@ -185,7 +180,7 @@ public final class BlockOptionalMeta {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return lootTables;
|
|
|
|
|
return manager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PredicateManager getPredicateManager() {
|
|
|
|
|
@@ -199,55 +194,20 @@ public final class BlockOptionalMeta {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
} else {
|
|
|
|
|
List<Item> items = new ArrayList<>();
|
|
|
|
|
try {
|
|
|
|
|
getManager().get(lootTableLocation).getRandomItems(
|
|
|
|
|
new LootContext.Builder(ServerLevelStub.fastCreate())
|
|
|
|
|
.withRandom(RandomSource.create())
|
|
|
|
|
.withParameter(LootContextParams.ORIGIN, Vec3.atLowerCornerOf(BlockPos.ZERO))
|
|
|
|
|
.withParameter(LootContextParams.TOOL, ItemStack.EMPTY)
|
|
|
|
|
.withOptionalParameter(LootContextParams.BLOCK_ENTITY, null)
|
|
|
|
|
.withParameter(LootContextParams.BLOCK_STATE, block.defaultBlockState())
|
|
|
|
|
.create(LootContextParamSets.BLOCK),
|
|
|
|
|
|
|
|
|
|
// the other overload for generate doesnt work in forge because forge adds code that requires a non null world
|
|
|
|
|
getManager().get(lootTableLocation).getRandomItems(
|
|
|
|
|
new LootContext.Builder((ServerLevel null)
|
|
|
|
|
.withRandom(RandomSource.create())
|
|
|
|
|
.withParameter(LootContextParams.ORIGIN, Vec3.atLowerCornerOf(BlockPos.ZERO))
|
|
|
|
|
.withParameter(LootContextParams.TOOL, ItemStack.EMPTY)
|
|
|
|
|
.withOptionalParameter(LootContextParams.BLOCK_ENTITY, null)
|
|
|
|
|
.withParameter(LootContextParams.BLOCK_STATE, block.defaultBlockState())
|
|
|
|
|
.create(LootContextParamSets.BLOCK),
|
|
|
|
|
stack -> items.add(stack.getItem())
|
|
|
|
|
);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class ServerLevelStub extends ServerLevel {
|
|
|
|
|
private static Minecraft client = Minecraft.getInstance();
|
|
|
|
|
private static Unsafe unsafe = getUnsafe();
|
|
|
|
|
public ServerLevelStub(MinecraftServer $$0, Executor $$1, LevelStorageSource.LevelStorageAccess $$2, ServerLevelData $$3, ResourceKey<Level> $$4, LevelStem $$5, ChunkProgressListener $$6, boolean $$7, long $$8, List<CustomSpawner> $$9, boolean $$10) {
|
|
|
|
|
super($$0, $$1, $$2, $$3, $$4, $$5, $$6, $$7, $$8, $$9, $$10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public FeatureFlagSet enabledFeatures() {
|
|
|
|
|
assert client.level != null;
|
|
|
|
|
return client.level.enabledFeatures();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ServerLevelStub fastCreate() {
|
|
|
|
|
try {
|
|
|
|
|
return (ServerLevelStub) unsafe.allocateInstance(ServerLevelStub.class);
|
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Unsafe getUnsafe() {
|
|
|
|
|
try {
|
|
|
|
|
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
|
|
|
|
theUnsafe.setAccessible(true);
|
|
|
|
|
return (Unsafe) theUnsafe.get(null);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|