From 5c0482e97278a4c9fa2bf18ec7ee0a35adc72351 Mon Sep 17 00:00:00 2001 From: dfsek Date: Wed, 24 Feb 2021 01:33:19 -0700 Subject: [PATCH] more fabric stuff --- .../loot/functions/DamageFunction.java | 1 + .../loot/functions/EnchantFunction.java | 2 ++ .../structures/script/StructureScript.java | 1 + .../script/functions/BlockFunction.java | 4 +++- .../structure/buffer/StructureBuffer.java | 6 +++--- .../structure/buffer/items/BufferedBlock.java | 12 ++++++++++-- .../world/population/StructurePopulator.java | 1 + gradle/wrapper/gradle-wrapper.properties | 2 +- platforms/fabric/build.gradle.kts | 2 +- .../dfsek/terra/fabric/TerraFabricPlugin.java | 19 +++++++++++++------ .../fabric/inventory/FabricInventory.java | 1 - .../fabric/mixin/GeneratorTypeAccessor.java | 19 +++++++++++++++++++ .../world/block/state/FabricBlockState.java | 1 - .../fabric/world/handles/FabricWorld.java | 5 +++-- .../world/FabricSeededWorldAccess.java | 6 ++++-- .../handles/world/FabricWorldAccess.java | 11 +++++++++++ .../handles/world/FabricWorldChunkRegion.java | 8 +++++--- 17 files changed, 78 insertions(+), 23 deletions(-) diff --git a/common/src/main/java/com/dfsek/terra/api/structures/loot/functions/DamageFunction.java b/common/src/main/java/com/dfsek/terra/api/structures/loot/functions/DamageFunction.java index 704fdcfa8..158c2248d 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/loot/functions/DamageFunction.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/loot/functions/DamageFunction.java @@ -33,6 +33,7 @@ public class DamageFunction implements LootFunction { */ @Override public ItemStack apply(ItemStack original, Random r) { + if(!(original instanceof Damageable)) return original; double itemDurability = (r.nextDouble() * (max - min)) + min; Damageable damage = (Damageable) original.getItemMeta(); damage.setDamage((int) (original.getType().getMaxDurability() - (itemDurability / 100) * original.getType().getMaxDurability())); diff --git a/common/src/main/java/com/dfsek/terra/api/structures/loot/functions/EnchantFunction.java b/common/src/main/java/com/dfsek/terra/api/structures/loot/functions/EnchantFunction.java index ff19b24c1..50818d871 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/loot/functions/EnchantFunction.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/loot/functions/EnchantFunction.java @@ -35,6 +35,8 @@ public class EnchantFunction implements LootFunction { */ @Override public ItemStack apply(ItemStack original, Random r) { + if(original.getItemMeta() == null) return original; + double enchant = (r.nextDouble() * (max - min)) + min; List possible = new GlueList<>(); for(Enchantment ench : main.getItemHandle().getEnchantments()) { diff --git a/common/src/main/java/com/dfsek/terra/api/structures/script/StructureScript.java b/common/src/main/java/com/dfsek/terra/api/structures/script/StructureScript.java index 8d6795294..573ac5451 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/script/StructureScript.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/script/StructureScript.java @@ -123,6 +123,7 @@ public class StructureScript { private StructureBuffer computeBuffer(Location location, Random random, Rotation rotation) { try { return cache.get(location, () -> { + System.out.println("{" + FastMath.floorDiv(location.getBlockX(), 16) + ", " + FastMath.floorDiv(location.getBlockZ(), 16) + "} : " + cache.size() + " : " + location.hashCode()); StructureBuffer buf = new StructureBuffer(location); buf.setSucceeded(applyBlock(new TerraImplementationArguments(buf, rotation, random, 0))); return buf; diff --git a/common/src/main/java/com/dfsek/terra/api/structures/script/functions/BlockFunction.java b/common/src/main/java/com/dfsek/terra/api/structures/script/functions/BlockFunction.java index 0373fe5fe..cf9c07ef5 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/script/functions/BlockFunction.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/script/functions/BlockFunction.java @@ -23,9 +23,11 @@ public class BlockFunction implements Function { private final Returnable x, y, z; private final Position position; private final Returnable overwrite; + private final TerraPlugin main; public BlockFunction(Returnable x, Returnable y, Returnable z, Returnable data, Returnable overwrite, TerraPlugin main, Position position) throws ParseException { this.position = position; + this.main = main; if(!(data instanceof ConstantExpression)) throw new ParseException("Block data must be constant", data.getPosition()); try { @@ -49,7 +51,7 @@ public class BlockFunction implements Function { RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse()); - arguments.getBuffer().addItem(new BufferedBlock(rot, overwrite.apply(implementationArguments, variableMap)), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(), FastMath.roundToInt(xz.getZ())).toLocation(arguments.getBuffer().getOrigin().getWorld())); + arguments.getBuffer().addItem(new BufferedBlock(rot, overwrite.apply(implementationArguments, variableMap), main), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(), FastMath.roundToInt(xz.getZ())).toLocation(arguments.getBuffer().getOrigin().getWorld())); return null; } diff --git a/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/StructureBuffer.java b/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/StructureBuffer.java index 6ff3029c6..c95b9555e 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/StructureBuffer.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/StructureBuffer.java @@ -22,11 +22,11 @@ public class StructureBuffer implements Buffer { } public void paste(Chunk chunk) { - bufferedItemMap.forEach(((vector3, item) -> { - Location current = origin.clone().add(vector3); + bufferedItemMap.forEach(((location, item) -> { + Location current = origin.clone().add(location); if(FastMath.floorDiv(current.getBlockX(), 16) != chunk.getX() || FastMath.floorDiv(current.getBlockZ(), 16) != chunk.getZ()) return; - item.paste(origin.clone().add(vector3)); + item.paste(current); })); } diff --git a/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/items/BufferedBlock.java b/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/items/BufferedBlock.java index 932efbd15..efb706ea9 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/items/BufferedBlock.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/items/BufferedBlock.java @@ -1,5 +1,6 @@ package com.dfsek.terra.api.structures.structure.buffer.items; +import com.dfsek.terra.api.TerraPlugin; import com.dfsek.terra.api.math.vector.Location; import com.dfsek.terra.api.platform.block.Block; import com.dfsek.terra.api.platform.block.BlockData; @@ -7,15 +8,22 @@ import com.dfsek.terra.api.platform.block.BlockData; public class BufferedBlock implements BufferedItem { private final BlockData data; private final boolean overwrite; + private final TerraPlugin main; - public BufferedBlock(BlockData data, boolean overwrite) { + public BufferedBlock(BlockData data, boolean overwrite, TerraPlugin main) { this.data = data; this.overwrite = overwrite; + this.main = main; } @Override public void paste(Location origin) { Block block = origin.getBlock(); - if(overwrite || block.isEmpty()) block.setBlockData(data, false); + try { + if(overwrite || block.isEmpty()) block.setBlockData(data, false); + } catch(RuntimeException e) { + main.logger().severe("Failed to place block at location " + origin + ": " + e.getMessage()); + main.getDebugLogger().stack(e); + } } } diff --git a/common/src/main/java/com/dfsek/terra/world/population/StructurePopulator.java b/common/src/main/java/com/dfsek/terra/world/population/StructurePopulator.java index ed46b15e3..2af261696 100644 --- a/common/src/main/java/com/dfsek/terra/world/population/StructurePopulator.java +++ b/common/src/main/java/com/dfsek/terra/world/population/StructurePopulator.java @@ -42,6 +42,7 @@ public class StructurePopulator implements TerraBlockPopulator { if(!((UserDefinedBiome) provider.getBiome(spawn)).getConfig().getStructures().contains(conf)) continue; Random random = new FastRandom(MathUtil.getCarverChunkSeed(FastMath.floorDiv(spawn.getBlockX(), 16), FastMath.floorDiv(spawn.getBlockZ(), 16), world.getSeed())); + System.out.println("chunk: {" + chunk.getX() + ", " + chunk.getZ() + "}"); conf.getStructure().get(random).execute(spawn.setY(conf.getSpawnStart().get(random)), chunk, random, Rotation.fromDegrees(90 * random.nextInt(4))); } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 6ca79a242..aec6aa17c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -org.gradle.jvmargs=-Xmx2048m \ No newline at end of file +org.gradle.jvmargs=-Xmx4096m \ No newline at end of file diff --git a/platforms/fabric/build.gradle.kts b/platforms/fabric/build.gradle.kts index 78d279159..78ec35ede 100644 --- a/platforms/fabric/build.gradle.kts +++ b/platforms/fabric/build.gradle.kts @@ -31,7 +31,7 @@ configure { } tasks.register("remapShadedJar") { - setProperty("input", file("build/libs/fabric-${version}-shaded.jar")) + setProperty("input", file("build/libs/Terra-fabric-${version}-shaded.jar")) setProperty("addNestedDependencies", false) setProperty("remapAccessWidener", true) } diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/TerraFabricPlugin.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/TerraFabricPlugin.java index 41291e2e1..37a201fed 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/TerraFabricPlugin.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/TerraFabricPlugin.java @@ -46,6 +46,7 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.block.Blocks; import net.minecraft.client.world.GeneratorType; +import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; import net.minecraft.util.registry.BuiltinRegistries; import net.minecraft.util.registry.Registry; @@ -286,17 +287,23 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer { Registry.register(Registry.BIOME_SOURCE, new Identifier("terra:terra"), TerraBiomeSource.CODEC); if(FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - GeneratorTypeAccessor.getVALUES().add(new GeneratorType("terra") { - @Override - protected ChunkGenerator getChunkGenerator(Registry biomeRegistry, Registry chunkGeneratorSettingsRegistry, long seed) { - ConfigPack pack = registry.get("DEFAULT"); - return new FabricChunkGeneratorWrapper(new TerraBiomeSource(biomeRegistry, seed, pack), seed, pack); - } + registry.forEach(pack -> { + System.out.println(pack.getTemplate().getID()); + pack.getBiomeRegistry().forEach(b -> System.out.println(b.getID())); + final GeneratorType generatorType = new GeneratorType("terra." + pack.getTemplate().getID()) { + @Override + protected ChunkGenerator getChunkGenerator(Registry biomeRegistry, Registry chunkGeneratorSettingsRegistry, long seed) { + return new FabricChunkGeneratorWrapper(new TerraBiomeSource(biomeRegistry, seed, pack), seed, pack); + } + }; + ((GeneratorTypeAccessor) generatorType).setTranslationKey(new LiteralText("Terra:" + pack.getTemplate().getID())); + GeneratorTypeAccessor.getVALUES().add(generatorType); }); } } + @Override public EventManager getEventManager() { return eventManager; diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/inventory/FabricInventory.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/inventory/FabricInventory.java index 3463ce06a..43fa95d9c 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/inventory/FabricInventory.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/inventory/FabricInventory.java @@ -31,7 +31,6 @@ public class FabricInventory implements Inventory { @Override public void setItem(int slot, ItemStack newStack) { - System.out.println("item @ " + slot + ": " + newStack.getHandle()); delegate.setStack(slot, FabricAdapter.adapt(newStack)); } } diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/mixin/GeneratorTypeAccessor.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/mixin/GeneratorTypeAccessor.java index 707ad6f1e..74fec5920 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/mixin/GeneratorTypeAccessor.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/mixin/GeneratorTypeAccessor.java @@ -1,10 +1,14 @@ package com.dfsek.terra.fabric.mixin; import net.minecraft.client.world.GeneratorType; +import net.minecraft.text.Text; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.gen.Accessor; import java.util.List; +import java.util.Map; +import java.util.Optional; @Mixin(GeneratorType.class) public interface GeneratorTypeAccessor { @@ -12,4 +16,19 @@ public interface GeneratorTypeAccessor { static List getVALUES() { throw new AssertionError(); } + + @Accessor + static Map, GeneratorType.ScreenProvider> getSCREEN_PROVIDERS() { + throw new UnsupportedOperationException(); + } + + @Mutable + @Accessor + static void setSCREEN_PROVIDERS(Map, GeneratorType.ScreenProvider> SCREEN_PROVIDERS) { + throw new UnsupportedOperationException(); + } + + @Mutable + @Accessor + void setTranslationKey(Text translationKey); } diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/block/state/FabricBlockState.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/block/state/FabricBlockState.java index 333c7cde5..2347f452f 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/block/state/FabricBlockState.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/block/state/FabricBlockState.java @@ -36,7 +36,6 @@ public class FabricBlockState implements BlockState { return new FabricMobSpawner(mobSpawnerBlockEntity, worldAccess); } else if(block1 instanceof AbstractChestBlock) { BlockEntity abstractChestBlock = worldAccess.getBlockEntity(FabricAdapter.adapt(block.getLocation().toVector())); - System.out.println("inventory: " + block1); return new FabricContainer(abstractChestBlock, worldAccess); } return null; diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/FabricWorld.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/FabricWorld.java index 5318be6ca..3c470f0a7 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/FabricWorld.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/FabricWorld.java @@ -12,6 +12,7 @@ import com.dfsek.terra.fabric.world.handles.chunk.FabricChunk; import com.dfsek.terra.fabric.world.handles.world.FabricWorldHandle; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.ServerWorldAccess; import net.minecraft.world.WorldAccess; import java.io.File; @@ -73,13 +74,13 @@ public class FabricWorld implements World, FabricWorldHandle { @Override public int hashCode() { - return delegate.generator.hashCode(); + return ((ServerWorldAccess) delegate.world).toServerWorld().hashCode(); } @Override public boolean equals(Object obj) { if(!(obj instanceof FabricWorld)) return false; - return ((FabricWorld) obj).delegate.generator.equals(delegate.generator); + return ((ServerWorldAccess) ((FabricWorld) obj).delegate.world).toServerWorld().equals(((ServerWorldAccess) delegate.world).toServerWorld()); } @Override diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricSeededWorldAccess.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricSeededWorldAccess.java index 9446c1c79..106d1e2a4 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricSeededWorldAccess.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricSeededWorldAccess.java @@ -10,6 +10,7 @@ import com.dfsek.terra.api.platform.world.generator.ChunkGenerator; import com.dfsek.terra.fabric.world.block.FabricBlock; import com.dfsek.terra.fabric.world.generator.FabricChunkGenerator; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.ServerWorldAccess; import net.minecraft.world.WorldAccess; import java.io.File; @@ -81,12 +82,13 @@ public class FabricSeededWorldAccess implements World, FabricWorldHandle { @Override public int hashCode() { - return handle.worldAccess.hashCode(); + return ((ServerWorldAccess) handle.worldAccess).toServerWorld().hashCode(); } @Override public boolean equals(Object obj) { - return super.equals(obj); + if(!(obj instanceof FabricSeededWorldAccess)) return false; + return ((ServerWorldAccess) ((FabricSeededWorldAccess) obj).handle.worldAccess).toServerWorld().equals(((ServerWorldAccess) handle.worldAccess).toServerWorld()); } @Override diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricWorldAccess.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricWorldAccess.java index d6bea52e0..b7780f837 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricWorldAccess.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricWorldAccess.java @@ -89,4 +89,15 @@ public class FabricWorldAccess implements World, FabricWorldHandle { public WorldAccess getWorld() { return delegate; } + + @Override + public int hashCode() { + return ((ServerWorldAccess) delegate).toServerWorld().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof FabricWorldAccess)) return false; + return ((ServerWorldAccess) ((FabricWorldAccess) obj).delegate).toServerWorld().equals(((ServerWorldAccess) delegate).toServerWorld()); + } } diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricWorldChunkRegion.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricWorldChunkRegion.java index cbe973289..66c61e0d5 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricWorldChunkRegion.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/world/handles/world/FabricWorldChunkRegion.java @@ -11,6 +11,7 @@ import com.dfsek.terra.fabric.world.block.FabricBlock; import com.dfsek.terra.fabric.world.generator.FabricChunkGenerator; import net.minecraft.util.math.BlockPos; import net.minecraft.world.ChunkRegion; +import net.minecraft.world.ServerWorldAccess; import net.minecraft.world.WorldAccess; import java.io.File; @@ -71,13 +72,14 @@ public class FabricWorldChunkRegion implements World, FabricWorldHandle { @Override public int hashCode() { - return delegate.generator.hashCode(); + return ((ServerWorldAccess) delegate.chunk).toServerWorld().hashCode(); } @Override public boolean equals(Object obj) { if(!(obj instanceof FabricWorldChunkRegion)) return false; - return ((FabricWorldChunkRegion) obj).delegate.generator.equals(delegate.generator); + return super.equals(obj); + //return ((ServerWorldAccess) ((FabricWorldChunkRegion) obj).delegate.chunk).toServerWorld().equals(((ServerWorldAccess) delegate.chunk).toServerWorld()); } @Override @@ -92,7 +94,7 @@ public class FabricWorldChunkRegion implements World, FabricWorldHandle { @Override public Object getHandle() { - return null; + return delegate; } @Override