Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed Feb 8, 2020
1 parent 0eed26f commit 43773ac
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 79 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.wode490390.nukkit</groupId>
<artifactId>scatteredbuildingpopulator</artifactId>
<packaging>jar</packaging>
<version>1.0.1</version>
<version>1.0.2</version>
<name>ScatteredBuildingPopulator</name>
<description>Scattered building populator plugin for Nukkit</description>
<url>http://wode490390.cn/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import cn.nukkit.level.generator.Generator;
import cn.nukkit.level.generator.populator.type.Populator;
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.Logger;
import cn.wode490390.nukkit.scatteredbuilding.populator.PopulatorDesertPyramid;
import cn.wode490390.nukkit.scatteredbuilding.populator.PopulatorJungleTemple;
import cn.wode490390.nukkit.scatteredbuilding.populator.PopulatorSwampHut;
Expand All @@ -23,14 +22,15 @@

public class ScatteredBuildingPopulator extends PluginBase implements Listener {

private static Logger LOGGER;
private static boolean DEBUG = false;

private static ScatteredBuildingPopulator INSTANCE;

private final Map<Level, List<Populator>> populators = Maps.newHashMap();

@Override
public void onLoad() {
LOGGER = this.getLogger();
INSTANCE = this;
}

@Override
Expand Down Expand Up @@ -75,13 +75,17 @@ public void onLevelUnload(LevelUnloadEvent event) {
this.populators.remove(event.getLevel());
}

public static ScatteredBuildingPopulator getInstance() {
return INSTANCE;
}

public static void debug(Object... objs) {
if (DEBUG) {
StringJoiner joiner = new StringJoiner(" ");
for (Object obj : objs) {
joiner.add(String.valueOf(obj));
}
LOGGER.warning(joiner.toString());
INSTANCE.getLogger().warning(joiner.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package cn.wode490390.nukkit.scatteredbuilding.loot;

import cn.nukkit.inventory.InventoryType;
import cn.nukkit.item.Item;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;

public class DesertPyramidChest extends RandomizableContainer {

private static final Map<List<ItemEntry>, RollEntry> pools = Maps.newHashMap();
private static final DesertPyramidChest INSTANCE = new DesertPyramidChest();

public static DesertPyramidChest get() {
return INSTANCE;
}

private DesertPyramidChest() {
super(Maps.newHashMap(), InventoryType.CHEST.getDefaultSize());

static {
PoolBuilder pool1 = new PoolBuilder()
.register(new ItemEntry(Item.DIAMOND, 0, 3, 1, 5))
.register(new ItemEntry(Item.IRON_INGOT, 0, 5, 1, 15))
Expand All @@ -26,25 +31,14 @@ public class DesertPyramidChest extends RandomizableContainer {
.register(new ItemEntry(Item.GOLDEN_APPLE, 20))
.register(new ItemEntry(Item.GOLDEN_APPLE_ENCHANTED, 2))
.register(new ItemEntry(Item.AIR, 15));
pools.put(pool1.build(), new RollEntry(4, 2, pool1.getTotalWeight()));
this.pools.put(pool1.build(), new RollEntry(4, 2, pool1.getTotalWeight()));

PoolBuilder pool2 = new PoolBuilder()
.register(new ItemEntry(Item.BONE, 0, 8, 1, 10))
.register(new ItemEntry(Item.GUNPOWDER, 0, 8, 1, 10))
.register(new ItemEntry(Item.ROTTEN_FLESH, 0, 8, 1, 10))
.register(new ItemEntry(Item.STRING, 0, 8, 1, 10))
.register(new ItemEntry(Item.SAND, 0, 8, 1, 10));
pools.put(pool2.build(), new RollEntry(4, pool2.getTotalWeight()));
this.pools.put(pool2.build(), new RollEntry(4, pool2.getTotalWeight()));
}

private DesertPyramidChest() {

}

@Override
public Map<List<ItemEntry>, RollEntry> getPools() {
return pools;
}

public static final DesertPyramidChest INSTANCE = new DesertPyramidChest();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package cn.wode490390.nukkit.scatteredbuilding.loot;

import cn.nukkit.inventory.InventoryType;
import cn.nukkit.item.Item;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;

public class JungleTempleChest extends RandomizableContainer {

private static final Map<List<ItemEntry>, RollEntry> pools = Maps.newHashMap();
private static final JungleTempleChest INSTANCE = new JungleTempleChest();

public static JungleTempleChest get() {
return INSTANCE;
}

private JungleTempleChest() {
super(Maps.newHashMap(), InventoryType.CHEST.getDefaultSize());

static {
PoolBuilder pool1 = new PoolBuilder()
.register(new ItemEntry(Item.DIAMOND, 0, 3, 1, 15))
.register(new ItemEntry(Item.IRON_INGOT, 0, 5, 1, 50))
Expand All @@ -23,17 +28,6 @@ public class JungleTempleChest extends RandomizableContainer {
.register(new ItemEntry(Item.GOLD_HORSE_ARMOR, 5))
.register(new ItemEntry(Item.DIAMOND_HORSE_ARMOR, 5))
.register(new ItemEntry(Item.ENCHANTED_BOOK, 6)); //TODO: ench nbt
pools.put(pool1.build(), new RollEntry(6, 2, pool1.getTotalWeight()));
this.pools.put(pool1.build(), new RollEntry(6, 2, pool1.getTotalWeight()));
}

private JungleTempleChest() {

}

@Override
public Map<List<ItemEntry>, RollEntry> getPools() {
return pools;
}

public static final JungleTempleChest INSTANCE = new JungleTempleChest();
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package cn.wode490390.nukkit.scatteredbuilding.loot;

import cn.nukkit.inventory.Inventory;
import cn.nukkit.inventory.InventoryType;
import cn.nukkit.item.Item;
import cn.nukkit.math.NukkitRandom;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Maps;

public class JungleTempleDispenser extends RandomizableContainer {

private JungleTempleDispenser() {
private static final JungleTempleDispenser INSTANCE = new JungleTempleDispenser();

public static JungleTempleDispenser get() {
return INSTANCE;
}

@Override
public void create(Inventory inventory, NukkitRandom random) {
for (int i = 0; i < 2; i++) {
inventory.setItem(random.nextBoundedInt(inventory.getSize()), Item.get(Item.ARROW, 0, random.nextRange(2, 7)), false);
}
}
private JungleTempleDispenser() {
super(Maps.newHashMap(), 9); //InventoryType.DISPENSER.getDefaultSize()

@Override
public Map<List<ItemEntry>, RollEntry> getPools() {
return Collections.emptyMap();
PoolBuilder pool1 = new PoolBuilder()
.register(new ItemEntry(Item.ARROW, 0, 7, 2, 1));
this.pools.put(pool1.build(), new RollEntry(2, 0));
}

public static final JungleTempleDispenser INSTANCE = new JungleTempleDispenser();
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
package cn.wode490390.nukkit.scatteredbuilding.loot;

import cn.nukkit.inventory.Inventory;
import cn.nukkit.item.Item;
import cn.nukkit.math.NukkitRandom;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.ListTag;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Map;

public abstract class RandomizableContainer {
public class RandomizableContainer {

public void create(Inventory inventory, NukkitRandom random) {
this.getPools().forEach((pool, roll) -> {
protected final Map<List<ItemEntry>, RollEntry> pools;
protected final int size;

public RandomizableContainer(Map<List<ItemEntry>, RollEntry> pools, int size) {
Preconditions.checkNotNull(pools);
this.pools = pools;
this.size = size;
}

public void create(ListTag<CompoundTag> list, NukkitRandom random) {
CompoundTag[] tags = new CompoundTag[this.size];

this.pools.forEach((pool, roll) -> {
for (int i = roll.getMin() == -1 ? roll.getMax() : random.nextRange(roll.getMin(), roll.getMax()); i > 0; --i) {
int result = random.nextBoundedInt(roll.getTotalWeight());
for (ItemEntry entry : pool) {
result -= entry.getWeight();
if (result < 0) {
inventory.setItem(random.nextBoundedInt(inventory.getSize()), Item.get(entry.getId(), entry.getMeta(), random.nextRange(entry.getMinCount(), entry.getMaxCount())), false);
int index = random.nextBoundedInt(tags.length);
tags[index] = NBTIO.putItemHelper(Item.get(entry.getId(), entry.getMeta(), random.nextRange(entry.getMinCount(), entry.getMaxCount())), index);
break;
}
}
}
});
}

public abstract Map<List<ItemEntry>, RollEntry> getPools();
for (int i = 0; i < tags.length; i++) {
if (tags[i] == null) {
list.add(i, NBTIO.putItemHelper(Item.get(Item.AIR), i));
} else {
list.add(i, tags[i]);
}
}
}

protected static class RollEntry {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cn.wode490390.nukkit.scatteredbuilding.scheduler;

import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.scheduler.PluginTask;
import cn.wode490390.nukkit.scatteredbuilding.ScatteredBuildingPopulator;

public class TileSyncTask extends PluginTask<ScatteredBuildingPopulator> {

public final String type;
public final FullChunk chunk;
public final CompoundTag nbt;

public TileSyncTask(String type, FullChunk chunk, CompoundTag nbt) {
super(ScatteredBuildingPopulator.getInstance());
this.type = type;
this.chunk = chunk;
this.nbt = nbt;
}

@Override
public void onRun(int currentTick) {
BlockEntity.createBlockEntity(this.type, this.chunk, this.nbt);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.wode490390.nukkit.scatteredbuilding.structure;

import cn.nukkit.Server;
import cn.nukkit.block.Block;
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.level.ChunkManager;
Expand All @@ -8,6 +9,8 @@
import cn.nukkit.math.NukkitRandom;
import cn.nukkit.math.Vector3;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.wode490390.nukkit.scatteredbuilding.ScatteredBuildingPopulator;
import cn.wode490390.nukkit.scatteredbuilding.scheduler.TileSyncTask;
import cn.wode490390.nukkit.scatteredbuilding.structure.piece.StructurePiece;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -63,10 +66,9 @@ public int getRandomBlock(NukkitRandom random, Map<Integer, Integer> blocks) {
*
* @param pos a point relative to this structure's root point
* @param id the new block entity id
* @return the new block entity
*/
public BlockEntity setTile(BlockVector3 pos, String id) {
return this.setTile(pos, id, null);
public void setTile(BlockVector3 pos, String id) {
this.setTile(pos, id, null);
}

/**
Expand All @@ -75,18 +77,15 @@ public BlockEntity setTile(BlockVector3 pos, String id) {
* @param pos a point relative to this structure's root point
* @param id the new block entity id
* @param data extra nbt data
* @return the new block entity
*/
public BlockEntity setTile(BlockVector3 pos, String id, CompoundTag data) {
public void setTile(BlockVector3 pos, String id, CompoundTag data) {
BlockVector3 vec = this.translate(pos);
BaseFullChunk chunk = this.level.getChunk(vec.x >> 4, vec.z >> 4);
CompoundTag nbt = BlockEntity.getDefaultCompound(new Vector3(vec.x, vec.y, vec.z), id);
if (data != null) {
data.getTags().forEach((key, value) -> nbt.put(key, value));
}
BlockEntity tile = BlockEntity.createBlockEntity(id, chunk, nbt);
chunk.addBlockEntity(tile);
return tile;
Server.getInstance().getScheduler().scheduleTask(ScatteredBuildingPopulator.getInstance(), new TileSyncTask(id, chunk, nbt));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import cn.nukkit.block.Block;
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.blockentity.BlockEntityChest;
import cn.nukkit.level.ChunkManager;
import cn.nukkit.math.BlockVector3;
import cn.nukkit.math.NukkitRandom;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.wode490390.nukkit.scatteredbuilding.ScatteredBuildingPopulator;
import cn.wode490390.nukkit.scatteredbuilding.loot.DesertPyramidChest;
import cn.wode490390.nukkit.scatteredbuilding.structure.StructureBuilder;
Expand Down Expand Up @@ -178,13 +179,21 @@ public void generate(ChunkManager level, NukkitRandom random) {
builder.setBlock(new BlockVector3(10, 3, 10), Block.STONE_PRESSURE_PLATE);

builder.setBlock(new BlockVector3(10, 3, 12), Block.CHEST, 2); // S
DesertPyramidChest.INSTANCE.create(((BlockEntityChest) builder.setTile(new BlockVector3(10, 3, 12), BlockEntity.CHEST)).getRealInventory(), random);
ListTag<CompoundTag> chestS = new ListTag<>("Items");
DesertPyramidChest.get().create(chestS, random);
builder.setTile(new BlockVector3(10, 3, 12), BlockEntity.CHEST, new CompoundTag().putList(chestS));
builder.setBlock(new BlockVector3(8, 3, 10), Block.CHEST, 5); // W
DesertPyramidChest.INSTANCE.create(((BlockEntityChest) builder.setTile(new BlockVector3(8, 3, 10), BlockEntity.CHEST)).getRealInventory(), random);
ListTag<CompoundTag> chestW = new ListTag<>("Items");
DesertPyramidChest.get().create(chestW, random);
builder.setTile(new BlockVector3(8, 3, 10), BlockEntity.CHEST, new CompoundTag().putList(chestW));
builder.setBlock(new BlockVector3(10, 3, 8), Block.CHEST, 3); // N
DesertPyramidChest.INSTANCE.create(((BlockEntityChest) builder.setTile(new BlockVector3(10, 3, 8), BlockEntity.CHEST)).getRealInventory(), random);
ListTag<CompoundTag> chestN = new ListTag<>("Items");
DesertPyramidChest.get().create(chestN, random);
builder.setTile(new BlockVector3(10, 3, 8), BlockEntity.CHEST, new CompoundTag().putList(chestN));
builder.setBlock(new BlockVector3(12, 3, 10), Block.CHEST, 4); // E
DesertPyramidChest.INSTANCE.create(((BlockEntityChest) builder.setTile(new BlockVector3(12, 3, 10), BlockEntity.CHEST)).getRealInventory(), random);
ListTag<CompoundTag> chestE = new ListTag<>("Items");
DesertPyramidChest.get().create(chestE, random);
builder.setTile(new BlockVector3(12, 3, 10), BlockEntity.CHEST, new CompoundTag().putList(chestE));

ScatteredBuildingPopulator.debug(getClass().getSimpleName(), this.boundingBox.getMin().x, this.boundingBox.getMin().y, this.boundingBox.getMin().z);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import cn.nukkit.block.Block;
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.blockentity.BlockEntityChest;
import cn.nukkit.level.ChunkManager;
import cn.nukkit.math.BlockVector3;
import cn.nukkit.math.NukkitRandom;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.wode490390.nukkit.scatteredbuilding.ScatteredBuildingPopulator;
import cn.wode490390.nukkit.scatteredbuilding.loot.JungleTempleChest;
import cn.wode490390.nukkit.scatteredbuilding.structure.StructureBuilder;
Expand Down Expand Up @@ -177,9 +178,13 @@ public void generate(ChunkManager level, NukkitRandom random) {
builder.fill(new BlockVector3(8, 2, 9), new BlockVector3(8, 2, 10), Block.REDSTONE_WIRE);
builder.setBlock(new BlockVector3(10, 2, 10), Block.UNPOWERED_REPEATER, 0 + 4); // N
builder.setBlock(new BlockVector3(8, 1, 3), Block.CHEST, 4); // E
JungleTempleChest.INSTANCE.create(((BlockEntityChest) builder.setTile(new BlockVector3(8, 1, 3), BlockEntity.CHEST)).getRealInventory(), random);
ListTag<CompoundTag> chestE = new ListTag<>("Items");
JungleTempleChest.get().create(chestE, random);
builder.setTile(new BlockVector3(8, 1, 3), BlockEntity.CHEST, new CompoundTag().putList(chestE));
builder.setBlock(new BlockVector3(9, 1, 10), Block.CHEST, 2); // S
JungleTempleChest.INSTANCE.create(((BlockEntityChest) builder.setTile(new BlockVector3(9, 1, 10), BlockEntity.CHEST)).getRealInventory(), random);
ListTag<CompoundTag> chestS = new ListTag<>("Items");
JungleTempleChest.get().create(chestS, random);
builder.setTile(new BlockVector3(9, 1, 10), BlockEntity.CHEST, new CompoundTag().putList(chestS));

// 2nd floor inside
for (int i = 0; i < 4; i++) {
Expand Down
Loading

0 comments on commit 43773ac

Please sign in to comment.