Skip to content

Commit

Permalink
Features update!
Browse files Browse the repository at this point in the history
 - Added the ability to make the structure spawn in a flat only zone. (#51).
 - Added the ability to replace blocks with other blocks when the structure spawns. (#58, #59).
 - Players can now create schematics using the plugin so it can be used on servers with limited FTP access. (#50).
 - Removed debug messages from the custom item system.
  • Loading branch information
ryandw11 committed Jun 28, 2020
1 parent 381c7a1 commit f3417a1
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 138 deletions.
17 changes: 4 additions & 13 deletions src/com/ryandw11/structure/CustomStructures.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.*;

import com.ryandw11.structure.loottables.customitems.CustomItemManager;
import com.ryandw11.structure.structure.Structure;
import com.ryandw11.structure.structure.StructureBuilder;
import com.ryandw11.structure.structure.StructureHandler;
import com.ryandw11.structure.structure.properties.StructureLimitations;
import com.ryandw11.structure.structure.properties.StructureLocation;
import com.ryandw11.structure.structure.properties.StructureProperties;
import com.ryandw11.structure.structure.properties.StructureYSpawning;
import com.ryandw11.structure.structure.properties.*;
import com.ryandw11.structure.utils.RandomCollection;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -228,9 +219,9 @@ private void updateConfig(){
builder.setChance(section.getInt("Chance.Number"), section.getInt("Chance.OutOf"));

if(section.contains("whitelistSpawnBlocks"))
builder.setStructureLimitations(new StructureLimitations(section.getStringList("whitelistSpawnBlocks")));
builder.setStructureLimitations(new StructureLimitations(section.getStringList("whitelistSpawnBlocks"), new BlockLevelLimit(), new HashMap<>()));
else
builder.setStructureLimitations(new StructureLimitations(new ArrayList<>()));
builder.setStructureLimitations(new StructureLimitations(new ArrayList<>(), new BlockLevelLimit(), new HashMap<>()));

StructureLocation structLocation = new StructureLocation();
if(section.contains("AllowedWorlds") && section.contains("AllWorlds")){
Expand Down
86 changes: 66 additions & 20 deletions src/com/ryandw11/structure/SchematicHandeler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ryandw11.structure.structure.Structure;
import com.ryandw11.structure.utils.GetBlocksInArea;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Sign;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.io.*;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.internal.annotation.Selection;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import org.bukkit.*;
import org.bukkit.block.*;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.Inventory;
Expand All @@ -33,9 +36,6 @@
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
Expand Down Expand Up @@ -109,15 +109,52 @@ public void schemHandle(Location loc, String filename, boolean useAir, RandomCol

//Schedule the signs & containers replacement task
int finalRotY = rotY;
List<Location> containersAndSignsLocations = getContainersAndSignsLocations(ch.getClipboard(), loc, finalRotY);
List<Location> containersAndSignsLocations = getContainersAndSignsLocations(ch.getClipboard(), loc, finalRotY, structure);
for (Location location : containersAndSignsLocations) {
if (location.getBlock().getState() instanceof Container) {
replaceContainerContent(lootTables, location);
} else if (location.getBlock().getState() instanceof Sign) {
replaceSignWithMob(location);
}
}
}

/**
* Create a schematic and save it to the schematics folder in the CustomStructures plugin.
* @param name The name of the schematic.
* @param player The player.
* @param w The world
* @return If the operation was successful.
*/
public boolean createSchematic(String name, Player player, World w) {
try{
WorldEditPlugin worldEditPlugin = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
assert worldEditPlugin != null;
Region selection = worldEditPlugin.getSession(player).getSelection(BukkitAdapter.adapt(w));
CuboidRegion region = new CuboidRegion(selection.getWorld(), selection.getMinimumPoint(), selection.getMaximumPoint());
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);

try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(selection.getWorld(), -1)) {
ForwardExtentCopy forwardExtentCopy = new ForwardExtentCopy(
editSession, region, clipboard, region.getMinimumPoint()
);
// configure here
Operations.complete(forwardExtentCopy);
} catch (WorldEditException e) {
e.printStackTrace();
}

File file = new File(plugin.getDataFolder() + File.separator + "schematics" + File.separator + name + ".schem");

try (ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) {
writer.write(clipboard);
} catch (IOException e) {
e.printStackTrace();
}
return true;
} catch (IncompleteRegionException ex){
return false;
}
}

/**
Expand All @@ -128,7 +165,7 @@ public void schemHandle(Location loc, String filename, boolean useAir, RandomCol
* @param rotation The rotate value (in degrees).
* @return The list of locations
*/
private List<Location> getContainersAndSignsLocations(Clipboard clipboard, Location pasteLocation, int rotation) {
private List<Location> getContainersAndSignsLocations(Clipboard clipboard, Location pasteLocation, int rotation, Structure structure) {

BlockVector3 originalOrigin = clipboard.getOrigin();
BlockVector3 originalMinimumPoint = clipboard.getRegion().getMinimumPoint();
Expand All @@ -150,6 +187,7 @@ private List<Location> getContainersAndSignsLocations(Clipboard clipboard, Locat
List<Location> locations = new ArrayList<>();
for (Location location : GetBlocksInArea.getLocationListBetween(minLoc, maxLoc)) {

Block block = location.getBlock();
BlockState blockState = location.getBlock().getState();

if (blockState instanceof Container) {
Expand Down Expand Up @@ -184,6 +222,14 @@ private List<Location> getContainersAndSignsLocations(Clipboard clipboard, Locat
}
} else if (blockState instanceof Sign) {
locations.add(location);
}else{
// For the block replacement system.
if(!structure.getStructureLimitations().getBlockReplacement().isEmpty()){
if(structure.getStructureLimitations().getBlockReplacement().containsKey(block.getType())){
block.setType(structure.getStructureLimitations().getBlockReplacement().get(block.getType()));
block.getState().update();
}
}
}
}//
return locations;
Expand All @@ -192,9 +238,9 @@ private List<Location> getContainersAndSignsLocations(Clipboard clipboard, Locat
/**
* Checks to see if a location is not already inside of a list of locations.
*
* @param locations
* @param location
* @return
* @param locations The list of locations.
* @param location The location to check
* @return If it is not already in.
*/
private boolean isNotAlreadyIn(List<Location> locations, Location location) {
for (Location auxLocation : locations) {
Expand All @@ -208,8 +254,8 @@ private boolean isNotAlreadyIn(List<Location> locations, Location location) {
/**
* Replace the contents of a container with the loottable.
*
* @param lootTables
* @param location
* @param lootTables The loot table
* @param location The location of the container.
*/
private void replaceContainerContent(RandomCollection<LootTable> lootTables, Location location) {
if(lootTables.isEmpty()) return;
Expand All @@ -234,7 +280,7 @@ private void replaceContainerContent(RandomCollection<LootTable> lootTables, Loc
/**
* Spawn a mob with the signs.
*
* @param location
* @param location The location of the sign.
*/
private void replaceSignWithMob(Location location) {
Sign sign = (Sign) location.getBlock().getState();
Expand Down
30 changes: 29 additions & 1 deletion src/com/ryandw11/structure/commands/SCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ryandw11.structure.commands;;

import com.ryandw11.structure.SchematicHandeler;
import com.ryandw11.structure.structure.Structure;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand Down Expand Up @@ -133,6 +134,31 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a
}
p.getInventory().addItem(item);
p.sendMessage(ChatColor.GREEN + "Successfully retrieved that item!");
}else if(args.length == 1 && args[0].equalsIgnoreCase("createschem")){
if(!sender.hasPermission("customstructures.createschematic")){
sender.sendMessage(ChatColor.RED + "You do not have permission for this command!");
return true;
}
sender.sendMessage(ChatColor.RED + "You must specify the name in order to create a schematic.");
}
else if(args.length == 2 && args[0].equalsIgnoreCase("createschem")){
if(!sender.hasPermission("customstructures.createschematic")){
sender.sendMessage(ChatColor.RED + "You do not have permission for this command!");
return true;
}
if(!(sender instanceof Player)){
sender.sendMessage(ChatColor.RED + "This command is for players only!");
return true;
}
Player p = (Player) sender;
String name = args[1];
SchematicHandeler handeler = new SchematicHandeler();
if(handeler.createSchematic(name, p, p.getWorld())){
p.sendMessage(ChatColor.GREEN + "Successfully created a schematic with the name of " + ChatColor.GOLD + name + ChatColor.GREEN + "!");
p.sendMessage(ChatColor.GREEN + "You can now use " + ChatColor.GOLD + name + ".schem" + ChatColor.GREEN + " in a structure.");
}else{
p.sendMessage(ChatColor.RED + "The world edit region seems to be incomplete! Try making a selection first!");
}
}
else {
if (sender.hasPermission("customstructures.info")) {
Expand All @@ -144,7 +170,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
"&3Github wiki:&2 https://github.com/ryandw11/CustomStructures/wiki"));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&3Commands:"));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&3/cs reload - &2Reload the plugin."));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&3/cstructure reload - &2Reload the plugin."));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
"&3/cstructure test (name) - &2Paste the defined structure."));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
Expand All @@ -155,6 +181,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a
"&3/cstructure checkKey - &2Get the key of an item you are holding in your hand."));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
"&3/cstructure getItem {key} - &2Get the item of the key specified."));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
"&3/cstructure createschem {name} - &2Create a schematic from the current worldedit selection (This is automatically save to the CustomStructures schematic folder)."));
} else {
sender.sendMessage(ChatColor.RED + "You do not have permission for this command.");
}
Expand Down
2 changes: 0 additions & 2 deletions src/com/ryandw11/structure/loottables/LootTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.List;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -50,7 +49,6 @@ private void loadItems() {
if(lootTablesFC.getString("Items." + itemID + ".Type").equalsIgnoreCase("CUSTOM")){
int amount = this.lootTablesFC.getInt("Items." + itemID + ".Amount");
int weight = this.lootTablesFC.getInt("Items." + itemID + ".Weight");
Bukkit.getLogger().info(itemID);
ItemStack item = CustomStructures.getInstance().getCustomItemManager().getItem(this.lootTablesFC.getString("Items." + itemID + ".Key"));
if(item == null){
CustomStructures.getInstance().getLogger().warning("Cannot find a custom item with the id of " + itemID +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.ryandw11.structure.structure.properties;

import org.bukkit.configuration.file.FileConfiguration;

public class BlockLevelLimit {
private String mode;

private int x1, z1, x2, z2;

public BlockLevelLimit(String mode, int x1, int z1, int x2, int z2){
this.mode = mode;
this.x1 = x1;
this.z1 = z1;
this.x2 = x2;
this.z2 = z2;
}

public BlockLevelLimit(){
mode = "NONE";
}

public BlockLevelLimit(FileConfiguration fileConfiguration){
if(!fileConfiguration.contains("StructureLimitations.BlockLevelLimit")){
mode = "NONE";
return;
}

this.mode = fileConfiguration.getString("StructureLimitations.BlockLevelLimit.mode");
this.x1 = fileConfiguration.getInt("StructureLimitations.BlockLevelLimit.cornerOne.x");
this.z1 = fileConfiguration.getInt("StructureLimitations.BlockLevelLimit.cornerOne.z");
this.x2 = fileConfiguration.getInt("StructureLimitations.BlockLevelLimit.cornerTwo.x");
this.z2 = fileConfiguration.getInt("StructureLimitations.BlockLevelLimit.cornerTwo.z");
}

public boolean isEnabled(){
return mode.equalsIgnoreCase("none");
}

public String getMode(){
return mode;
}

public int getX1(){
return x1;
}

public int getX2(){
return x2;
}

public int getZ1(){
return z1;
}

public int getZ2(){
return z2;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
package com.ryandw11.structure.structure.properties;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.*;

public class StructureLimitations {

private List<String> whitelistSpawnBlocks;
private BlockLevelLimit blockLevelLimit;
private Map<Material, Material> blockReplacement;

public StructureLimitations(FileConfiguration configuration){
if(!configuration.contains("StructureLimitations.whitelistSpawnBlocks"))
whitelistSpawnBlocks = new ArrayList<>();
else
whitelistSpawnBlocks = configuration.getStringList("StructureLimitations.whitelistSpawnBlocks");

this.blockLevelLimit = new BlockLevelLimit(configuration);

blockReplacement = new HashMap<>();
if(configuration.contains("StructureLimitations.replacement_blocks")){
for(String s : Objects.requireNonNull(configuration.getConfigurationSection("StructureLimitations.replacement_blocks")).getKeys(false)){
Material firstMaterial = Material.getMaterial(s);
Material secondMaterial = Material.getMaterial(Objects.requireNonNull(configuration.getString("StructureLimitations.replacement_blocks." + s)));
blockReplacement.put(firstMaterial, secondMaterial);
}
}
}

public StructureLimitations(List<String> whitelistSpawnBlocks){
public StructureLimitations(List<String> whitelistSpawnBlocks, BlockLevelLimit blockLevelLimit, Map<Material, Material> blockReplacement){
this.whitelistSpawnBlocks = whitelistSpawnBlocks;
this.blockLevelLimit = blockLevelLimit;
this.blockReplacement = blockReplacement;
}

public List<String> getWhitelistBlocks(){
Expand All @@ -33,4 +48,12 @@ public boolean hasBlock(Block b){
}
return false;
}

public BlockLevelLimit getBlockLevelLimit(){
return blockLevelLimit;
}

public Map<Material, Material> getBlockReplacement(){
return blockReplacement;
}
}
Loading

0 comments on commit f3417a1

Please sign in to comment.