Skip to content

Commit

Permalink
Added alwaysOnGround loot property
Browse files Browse the repository at this point in the history
  • Loading branch information
Arckenver committed Sep 28, 2016
1 parent 10a690e commit 96d8244
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 34 deletions.
37 changes: 35 additions & 2 deletions src/main/java/com/arckenver/mightyloot/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public static void load()
MightyLootPlugin.getLogger().error("Could not write in the config file !");
}

ensureBoolean(config.getNode("options", "placeGlowstoneBelowLoot"), true);
ensureBoolean(config.getNode("options", "placeAlwaysOnGround"), true);

lootConfigs = new ArrayList<LootConfig>();
if (!config.getNode("lootTypes").hasMapChildren())
{
Expand Down Expand Up @@ -162,6 +165,7 @@ else if (type.equals("cyl"))
MightyLootPlugin.getLogger().warn(worldName + " is not a valid world name !");
}
}
saveConfig();
}

public static void saveConfig()
Expand Down Expand Up @@ -255,11 +259,40 @@ public static void setToDefaultConfig(CommentedConfigurationNode conf)
conf.getNode("worlds", "world", "lootTypes", "goldish").setValue(25);
conf.getNode("worlds", "world", "lootTypes", "food").setValue(15);
}

// LOOTCONFIG

public static ArrayList<LootConfig> getLootConfigs()
{
return lootConfigs;
}

public static CommentedConfigurationNode getOptions()
{
return config.getNode("options");
}

/*
private static void ensureString(CommentedConfigurationNode node, String def)
{
if (node.getString() == null)
{
node.setValue(def);
}
}
private static void ensurePositiveNumber(CommentedConfigurationNode node, Number def)
{
if (!(node.getValue() instanceof Number) || node.getDouble(-1) < 0)
{
node.setValue(def);
}
}
*/

private static void ensureBoolean(CommentedConfigurationNode node, boolean def)
{
if (!(node.getValue() instanceof Boolean))
{
node.setValue(def);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/arckenver/mightyloot/area/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
public interface Area
{
public Location<World> getRandomLoc(World world);
public int getMaxY();
public int getMinY();
}
4 changes: 2 additions & 2 deletions src/main/java/com/arckenver/mightyloot/area/CylArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public Location<World> getRandomLoc(World world)
return world.getLocation(x, y, z);
}

public int getMaxY()
public int getMinY()
{
return maxY;
return minY;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/arckenver/mightyloot/area/RectArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Location<World> getRandomLoc(World world)
return world.getLocation(x, y, z);
}

public int getMaxY()
public int getMinY()
{
return maxY;
return minY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public CommandResult execute(CommandSource src, CommandContext ctx) throws Comma

MessageChannel.TO_ALL.send(Text.builder()
.append(Text.of(TextColors.GOLD, (s2.length > 0) ? s2[0] : ""))
.append(Text.of(TextColors.YELLOW, lootConfig.getWorldName()))
.append(Text.of(TextColors.YELLOW, (s2.length > 1) ? lootConfig.getWorldName() : ""))
.append(Text.of(TextColors.GOLD, (s2.length > 1) ? s2[1] : ""))
.append(loot.getType().getDisplay())
.append(Text.of(TextColors.GOLD, (s3.length > 0) ? s3[0] : ""))
.append(Text.of(TextColors.YELLOW, lootConfig.getWorldName()))
.append(Text.of(TextColors.YELLOW, (s3.length > 1) ? lootConfig.getWorldName() : ""))
.append(Text.of(TextColors.GOLD, (s3.length > 1) ? s3[1] : ""))
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public void onInteract(InteractBlockEvent event, @First Player player)

MessageChannel.TO_ALL.send(Text.builder()
.append(Text.of(TextColors.GOLD, (s2.length > 0) ? s2[0] : ""))
.append(Text.of(TextColors.YELLOW, player.getName()))
.append(Text.of(TextColors.YELLOW, (s2.length > 1) ? player.getName() : ""))
.append(Text.of(TextColors.GOLD, (s2.length > 1) ? s2[1] : ""))
.append(loot.getType().getDisplay())
.append(Text.of(TextColors.GOLD, (s3.length > 0) ? s3[0] : ""))
.append(Text.of(TextColors.YELLOW, player.getName()))
.append(Text.of(TextColors.YELLOW, (s3.length > 1) ? player.getName() : ""))
.append(Text.of(TextColors.GOLD, (s3.length > 1) ? s3[1] : ""))
.build());
}
Expand Down
58 changes: 35 additions & 23 deletions src/main/java/com/arckenver/mightyloot/task/SpawnLootRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Map.Entry;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.text.Text;
Expand All @@ -13,6 +12,7 @@
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import com.arckenver.mightyloot.ConfigHandler;
import com.arckenver.mightyloot.DataHandler;
import com.arckenver.mightyloot.LanguageHandler;
import com.arckenver.mightyloot.MightyLootPlugin;
Expand All @@ -23,7 +23,7 @@

public class SpawnLootRunnable implements Runnable
{
public static final int MAX_ATTEMPTS = 200;
public static final int MAX_ATTEMPTS = 1000;

private LootConfig lootConfig;

Expand All @@ -47,11 +47,11 @@ public void run()

MessageChannel.TO_ALL.send(Text.builder()
.append(Text.of(TextColors.GOLD, (s2.length > 0) ? s2[0] : ""))
.append(Text.of(TextColors.YELLOW, world.getName()))
.append(Text.of(TextColors.YELLOW, (s2.length > 1) ? world.getName() : ""))
.append(Text.of(TextColors.GOLD, (s2.length > 1) ? s2[1] : ""))
.append(loot.getType().getDisplay())
.append(Text.of(TextColors.GOLD, (s3.length > 0) ? s3[0] : ""))
.append(Text.of(TextColors.YELLOW, world.getName()))
.append(Text.of(TextColors.YELLOW, (s3.length > 1) ? world.getName() : ""))
.append(Text.of(TextColors.GOLD, (s3.length > 1) ? s3[1] : ""))
.build());
}
Expand All @@ -62,42 +62,53 @@ public void run()
{
attempt++;
Location<World> randomLoc = lootConfig.getArea().getRandomLoc(world);
BlockType lastLastBlockType = randomLoc.getBlockType();
randomLoc.add(0, 1, 0);
BlockType lastBlockType = randomLoc.getBlockType();
BlockType blockType;
while (randomLoc.getBlockY() < lootConfig.getArea().getMaxY())
if (!randomLoc.getBlockType().equals(BlockTypes.AIR) ||
!randomLoc.add(0, 1, 0).getBlockType().equals(BlockTypes.AIR))
{
randomLoc = randomLoc.add(0, 1, 0);
blockType = randomLoc.getBlockType();
if (lastLastBlockType.equals(BlockTypes.AIR) && lastBlockType.equals(BlockTypes.AIR) && blockType.equals(BlockTypes.AIR))
MightyLootPlugin.getLogger().warn("1");
continue;
}
if (ConfigHandler.getOptions().getNode("placeGlowstoneBelowLoot").getBoolean())
{
randomLoc = randomLoc.add(0, -1, 0);
if (!randomLoc.getBlockType().equals(BlockTypes.AIR))
{
MightyLootPlugin.getLogger().warn("2");
continue;
}
}
if (ConfigHandler.getOptions().getNode("placeAlwaysOnGround").getBoolean())
{
while (randomLoc.add(0, -1, 0).getBlockType().equals(BlockTypes.AIR) && randomLoc.getBlockY() >= lootConfig.getArea().getMinY())
{
loc = randomLoc.add(0, -2, 0);
break;
randomLoc = randomLoc.add(0, -1, 0);
}
else
if (randomLoc.add(0, -1, 0).getBlockType().equals(BlockTypes.AIR))
{
lastLastBlockType = lastBlockType;
lastBlockType = blockType;
MightyLootPlugin.getLogger().warn("3");
continue;
}
}
loc = randomLoc;
}
if (loc == null)
{
MightyLootPlugin.getLogger().info("After trying " + MAX_ATTEMPTS + " times, we could not find any good spawn location for the loot");
return;
}

loc.setBlockType(BlockTypes.GLOWSTONE);

loc = loc.add(0, 1, 0);
if (ConfigHandler.getOptions().getNode("placeGlowstoneBelowLoot").getBoolean())
{
loc.setBlockType(BlockTypes.GLOWSTONE);
loc = loc.add(0, 1, 0);
}

// TODO below we use setblock command, it shall be replaced by code using inventory api
/*
loc.setBlockType(BlockTypes.CHEST);
Chest chest = (Chest) loc.getTileEntity().get();
lootType.fillChest(chest.getInventory().parent());
*/
// TODO below we use setblock command, it shall be replaced by code using inventory api
String setblockCmd = "setblock " + loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ() + " chest 0 replace {Items:[";
int slot = 0;
for (Entry<ItemType, Interval> e : lootType.getItems().entrySet())
Expand Down Expand Up @@ -126,12 +137,13 @@ public void run()

MessageChannel.TO_ALL.send(Text.builder()
.append(Text.of(TextColors.GOLD, (s2.length > 0) ? s2[0] : ""))
.append(Text.of(TextColors.YELLOW, world.getName()))
.append(Text.of(TextColors.YELLOW, (s2.length > 1) ? world.getName() : ""))
.append(Text.of(TextColors.GOLD, (s2.length > 1) ? s2[1] : ""))
.append(lootType.getDisplay())
.append(Text.of(TextColors.GOLD, (s3.length > 0) ? s3[0] : ""))
.append(Text.of(TextColors.YELLOW, world.getName()))
.append(Text.of(TextColors.YELLOW, (s3.length > 1) ? world.getName() : ""))
.append(Text.of(TextColors.GOLD, (s3.length > 1) ? s3[1] : ""))
.append(Text.of(" "))

.append(Text.of(TextColors.GOLD, (s.length > 0) ? s[0] : ""))
.append(Text.builder("/ml hunt").color(TextColors.YELLOW).onClick(TextActions.runCommand("/mightyloot hunt")).build())
Expand Down

0 comments on commit 96d8244

Please sign in to comment.