Skip to content

Commit

Permalink
Initial custom townblock types draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Warriorrrr committed Oct 20, 2021
1 parent e0991c3 commit df62c45
Show file tree
Hide file tree
Showing 22 changed files with 631 additions and 236 deletions.
14 changes: 13 additions & 1 deletion src/com/palmergames/bukkit/config/ConfigNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,18 @@ public enum ConfigNodes {
"false",
"",
"# When set to true, players with the Frost Walker enchant will need to be able to build where they are attempting to freeze."),
TOWNBLOCKTYPES(
"townblocktypes",
"",
"",
"",
"############################################################",
"# +------------------------------------------------------+ #",
"# | Town Block Types | #",
"# +------------------------------------------------------+ #",
"############################################################",
""),
TOWNBLOCKTYPES_TYPES("townblocktypes.types", ""),
UNCLAIMED_ZONE(
"unclaimed",
"",
Expand Down Expand Up @@ -1410,7 +1422,7 @@ public enum ConfigNodes {
UNCLAIMED_ZONE_SWITCH("unclaimed.unclaimed_zone_switch",
"false",
"",
"# Can players interact with switch blocks listed in the above protectection.switch_ids in the wilderness without restriction?"),
"# Can players interact with switch blocks listed in the above protection.switch_ids in the wilderness without restriction?"),
UNCLAIMED_ZONE_IGNORE(
"unclaimed.unclaimed_zone_ignore",
"SAPLING,GOLD_ORE,IRON_ORE,COAL_ORE,LOG,LEAVES,LAPIS_ORE,LONG_GRASS,YELLOW_FLOWER,RED_ROSE,BROWN_MUSHROOM,RED_MUSHROOM,TORCH,DIAMOND_ORE,LADDER,RAILS,REDSTONE_ORE,GLOWING_REDSTONE_ORE,CACTUS,CLAY,SUGAR_CANE_BLOCK,PUMPKIN,GLOWSTONE,LOG_2,VINE,NETHER_WARTS,COCOA",
Expand Down
10 changes: 5 additions & 5 deletions src/com/palmergames/bukkit/towny/ChunkNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static void loadFormatStrings() {
TownBlock fromTownBlock, toTownBlock = null;
Town fromTown = null, toTown = null;
Resident fromResident = null, toResident = null;
TownBlockType fromPlotType = null, toPlotType = null;
String fromPlotType = null, toPlotType = null;
PlotGroup fromPlotGroup = null, toPlotGroup = null;

public ChunkNotification(WorldCoord from, WorldCoord to) {
Expand All @@ -82,7 +82,7 @@ public ChunkNotification(WorldCoord from, WorldCoord to) {

if (from.hasTownBlock()) {
fromTownBlock = from.getTownBlockOrNull();
fromPlotType = fromTownBlock.getType();
fromPlotType = StringMgmt.capitalize(fromTownBlock.getData().getType());
fromForSale = fromTownBlock.getPlotPrice() != -1;
if (fromTownBlock.hasPlotObjectGroup()) {
fromPlotGroup = fromTownBlock.getPlotObjectGroup();
Expand All @@ -98,7 +98,7 @@ public ChunkNotification(WorldCoord from, WorldCoord to) {

if (to.hasTownBlock()) {
toTownBlock = to.getTownBlockOrNull();
toPlotType = toTownBlock.getType();
toPlotType = StringMgmt.capitalize(toTownBlock.getData().getType());
toTown = toTownBlock.getTownOrNull();
toResident = toTownBlock.getResidentOrNull();
toForSale = toTownBlock.getPlotPrice() != -1;
Expand Down Expand Up @@ -313,8 +313,8 @@ public String getGroupNotification() {

public String getPlotTypeNotification() {

if (fromPlotType != toPlotType && toPlotType != null && toPlotType != TownBlockType.RESIDENTIAL)
return String.format(plotTypeNotificationFormat, toPlotType.toString());
if (toPlotType != null && !toPlotType.equals(fromPlotType) && !TownBlockType.RESIDENTIAL.equals(toPlotType))
return String.format(plotTypeNotificationFormat, toPlotType);
return null;
}
}
6 changes: 5 additions & 1 deletion src/com/palmergames/bukkit/towny/Towny.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.PlayerCache;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.TownBlockTypeHandler;
import com.palmergames.bukkit.towny.object.Translation;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.bukkit.towny.object.metadata.MetadataLoader;
Expand Down Expand Up @@ -233,11 +234,14 @@ public void loadFoundation(boolean reload) {
loadConfig(reload);
// Then load the language files.
loadLocalization(reload);
// Then load the database.
// Then load the database config.
loadDatabaseConfig(reload);
// Then load permissions
loadPermissions(reload);

// Initialize the type handler after the config is loaded and before the database is.
TownBlockTypeHandler.initialize();

// Initialize the special log4j hook logger.
TownyLogger.getInstance();

Expand Down
2 changes: 1 addition & 1 deletion src/com/palmergames/bukkit/towny/TownyAsciiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ else if (nation.hasEnemy(town.getNation()))
} else if (townblock.isHomeBlock())
townyMap[y][x] = townyMap[y][x].content("H");
else
townyMap[y][x] = townyMap[y][x].content(townblock.getType().getAsciiMapKey());
townyMap[y][x] = townyMap[y][x].content(townblock.getData().getMapKey());

TextComponent forSaleComponent = Component.empty();
TextComponent claimedAtComponent = Component.empty();
Expand Down
2 changes: 1 addition & 1 deletion src/com/palmergames/bukkit/towny/TownyFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ public static List<String> getTaxStatus(Resident resident, Locale locale) {
if (town != null) {
if (taxExempt && town.hasResident(resident)) // Resident will not pay any tax for plots owned by their towns.
continue;
plotTax += townBlock.getType().getTax(town);
plotTax += townBlock.getData().getTax(town);
}
}

Expand Down
185 changes: 161 additions & 24 deletions src/com/palmergames/bukkit/towny/TownySettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.palmergames.bukkit.towny.object.NationSpawnLevel.NSpawnLevel;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
import com.palmergames.bukkit.towny.object.TownBlockOwner;
import com.palmergames.bukkit.towny.object.TownSpawnLevel.SpawnLevel;
import com.palmergames.bukkit.towny.object.TownyPermission.ActionType;
Expand All @@ -29,6 +30,7 @@

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;

import java.io.IOException;
Expand All @@ -38,6 +40,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -64,8 +67,8 @@ public enum NationLevel {
private static final SortedMap<Integer, Map<TownySettings.TownLevel, Object>> configTownLevel = Collections.synchronizedSortedMap(new TreeMap<Integer, Map<TownySettings.TownLevel, Object>>(Collections.reverseOrder()));
private static final SortedMap<Integer, Map<TownySettings.NationLevel, Object>> configNationLevel = Collections.synchronizedSortedMap(new TreeMap<Integer, Map<TownySettings.NationLevel, Object>>(Collections.reverseOrder()));

private static final List<String> ItemUseMaterials = new ArrayList<>();
private static final List<String> SwitchUseMaterials = new ArrayList<>();
private static final Set<Material> itemUseMaterials = new HashSet<>();
private static final Set<Material> switchUseMaterials = new HashSet<>();
private static final List<Class<?>> protectedMobs = new ArrayList<>();

public static void newTownLevel(int numResidents, String namePrefix, String namePostfix, String mayorPrefix, String mayorPostfix, int townBlockLimit, double townUpkeepMultiplier, int townOutpostLimit, int townBlockBuyBonusLimit, double debtCapModifier) {
Expand Down Expand Up @@ -314,8 +317,8 @@ private static void loadProtectedMobsList() {

private static void loadSwitchAndItemUseMaterialsLists() {

SwitchUseMaterials.clear();
ItemUseMaterials.clear();
switchUseMaterials.clear();
itemUseMaterials.clear();

/*
* Load switches from config value.
Expand All @@ -325,10 +328,11 @@ private static void loadSwitchAndItemUseMaterialsLists() {
List<String> switches = getStrArr(ConfigNodes.PROT_SWITCH_MAT);
for (String matName : switches) {
if (ItemLists.GROUPS.contains(matName)) {
List<String> group = ItemLists.getGrouping(matName);
SwitchUseMaterials.addAll(group);
switchUseMaterials.addAll(toMaterialSet(ItemLists.getGrouping(matName)));
} else {
SwitchUseMaterials.add(matName);
Material material = Material.matchMaterial(matName);
if (material != null)
itemUseMaterials.add(material);
}
}

Expand All @@ -340,14 +344,26 @@ private static void loadSwitchAndItemUseMaterialsLists() {
List<String> items = getStrArr(ConfigNodes.PROT_ITEM_USE_MAT);
for (String matName : items) {
if (ItemLists.GROUPS.contains(matName)) {
List<String> group = ItemLists.getGrouping(matName);
ItemUseMaterials.addAll(group);
itemUseMaterials.addAll(toMaterialSet(ItemLists.getGrouping(matName)));
} else {
ItemUseMaterials.add(matName);
Material material = Material.matchMaterial(matName);
if (material != null)
itemUseMaterials.add(material);
}
}
}

private static Set<Material> toMaterialSet(List<String> materialList) {
Set<Material> materials = new HashSet<>();
for (String materialName : materialList) {
Material material = Material.matchMaterial(materialName);
if (material != null)
materials.add(material);
}

return materials;
}

private static void loadWarMaterialsLists() {
// Load allowed blocks in warzone.
WarZoneConfig.setEditableMaterialsInWarZone(getAllowedMaterials(ConfigNodes.WAR_WARZONE_EDITABLE_MATERIALS));
Expand Down Expand Up @@ -510,17 +526,13 @@ private static void setDefaults(String version, Path configPath) {

setDefaultLevels();

} else if ( (root.getRoot().equals(ConfigNodes.LEVELS_TOWN_LEVEL.getRoot()))
|| (root.getRoot().equals(ConfigNodes.LEVELS_NATION_LEVEL.getRoot())) ){

// Do nothing here as setDefaultLevels configured town and
// nation levels.

} else if (root.getRoot().equals(ConfigNodes.VERSION.getRoot())) {
setNewProperty(root.getRoot(), version);
} else if (root.getRoot().equals(ConfigNodes.LAST_RUN_VERSION.getRoot())) {
setNewProperty(root.getRoot(), getLastRunVersion(version));
} else
} else if (root.getRoot().equals(ConfigNodes.TOWNBLOCKTYPES_TYPES.getRoot()))
setTownBlockTypes();
else
setNewProperty(root.getRoot(), (config.get(root.getRoot().toLowerCase()) != null) ? config.get(root.getRoot().toLowerCase()) : root.getDefault());

}
Expand Down Expand Up @@ -751,6 +763,102 @@ private static void setDefaultLevels() {
} else
newConfig.set(ConfigNodes.LEVELS_NATION_LEVEL.getRoot(), config.get(ConfigNodes.LEVELS_NATION_LEVEL.getRoot()));
}

private static void setTownBlockTypes() {
addComment(ConfigNodes.TOWNBLOCKTYPES_TYPES.getRoot(),
"# Townblock types config",
"# If empty, itemUseIds & switchIds will use values defined in protection.item_use_ids and protection.switch_ids.",
"# Allowed blocks are blocks that will always be allowed to be placed and broken in a plot.",
"# If tax is set to 0, the towns' plot tax will be used instead."
);

if (!config.contains(ConfigNodes.TOWNBLOCKTYPES_TYPES.getRoot())) {
List<Map<String, Object>> types = new ArrayList<>();
Map<String, Object> type = new LinkedHashMap<>();

type.put("name", "default");
type.put("cost", 0.0);
type.put("tax", 0.0);
type.put("mapKey", "+");
type.put("itemUseIds", "");
type.put("switchIds", "");
type.put("allowedBlocks", "");
types.add(new LinkedHashMap<>(type));
type.clear();

type.put("name", "shop");
type.put("cost", 0.0);
type.put("tax", 0.0);
type.put("mapKey", "C");
type.put("itemUseIds", "");
type.put("switchIds", "");
type.put("allowedBlocks", "");
types.add(new LinkedHashMap<>(type));
type.clear();

type.put("name", "arena");
type.put("cost", 0.0);
type.put("tax", 0.0);
type.put("mapKey", "A");
type.put("itemUseIds", "");
type.put("switchIds", "");
type.put("allowedBlocks", "");
types.add(new LinkedHashMap<>(type));
type.clear();

type.put("name", "wilds");
type.put("cost", 0.0);
type.put("tax", 0.0);
type.put("mapKey", "W");
type.put("itemUseIds", "");
type.put("switchIds", "");
type.put("allowedBlocks", "");
types.add(new LinkedHashMap<>(type));
type.clear();

type.put("name", "inn");
type.put("cost", 0.0);
type.put("tax", 0.0);
type.put("mapKey", "I");
type.put("itemUseIds", "");
type.put("switchIds", "");
type.put("allowedBlocks", "");
types.add(new LinkedHashMap<>(type));
type.clear();

type.put("name", "jail");
type.put("cost", 0.0);
type.put("tax", 0.0);
type.put("mapKey", "J");
type.put("itemUseIds", "");
type.put("switchIds", "");
type.put("allowedBlocks", "");
types.add(new LinkedHashMap<>(type));
type.clear();

type.put("name", "farm");
type.put("cost", 0.0);
type.put("tax", 0.0);
type.put("mapKey", "F");
type.put("itemUseIds", "");
type.put("switchIds", "");
type.put("allowedBlocks", "BAMBOO,BAMBOO_SAPLING,JUNGLE_LOG,JUNGLE_SAPLING,JUNGLE_LEAVES,OAK_LOG,OAK_SAPLING,OAK_LEAVES,BIRCH_LOG,BIRCH_SAPLING,BIRCH_LEAVES,ACACIA_LOG,ACACIA_SAPLING,ACACIA_LEAVES,DARK_OAK_LOG,DARK_OAK_SAPLING,DARK_OAK_LEAVES,SPRUCE_LOG,SPRUCE_SAPLING,SPRUCE_LEAVES,BEETROOTS,COCOA,CHORUS_PLANT,CHORUS_FLOWER,SWEET_BERRY_BUSH,KELP,SEAGRASS,TALL_SEAGRASS,GRASS,TALL_GRASS,FERN,LARGE_FERN,CARROTS,WHEAT,POTATOES,PUMPKIN,PUMPKIN_STEM,ATTACHED_PUMPKIN_STEM,NETHER_WART,COCOA,VINE,MELON,MELON_STEM,ATTACHED_MELON_STEM,SUGAR_CANE,CACTUS,ALLIUM,AZURE_BLUET,BLUE_ORCHID,CORNFLOWER,DANDELION,LILAC,LILY_OF_THE_VALLEY,ORANGE_TULIP,OXEYE_DAISY,PEONY,PINK_TULIP,POPPY,RED_TULIP,ROSE_BUSH,SUNFLOWER,WHITE_TULIP,WITHER_ROSE,CRIMSON_FUNGUS,CRIMSON_STEM,CRIMSON_HYPHAE,CRIMSON_ROOTS,MUSHROOM_STEM,NETHER_WART_BLOCK,BROWN_MUSHROOM,BROWN_MUSHROOM_BLOCK,RED_MUSHROOM,RED_MUSHROOM_BLOCK,SHROOMLIGHT,WARPED_FUNGUS,WARPED_HYPHAE,WARPED_ROOTS,WARPED_STEM,WARPED_WART_BLOCK,WEEPING_VINES_PLANT,WEEPING_VINES,NETHER_SPROUTS,SHEARS");
types.add(new LinkedHashMap<>(type));
type.clear();

type.put("name", "bank");
type.put("cost", 0.0);
type.put("tax", 0.0);
type.put("mapKey", "B");
type.put("itemUseIds", "");
type.put("switchIds", "");
type.put("allowedBlocks", "");
types.add(new LinkedHashMap<>(type));
type.clear();
newConfig.set(ConfigNodes.TOWNBLOCKTYPES_TYPES.getRoot(), types);
} else
newConfig.set(ConfigNodes.TOWNBLOCKTYPES_TYPES.getRoot(), config.get(ConfigNodes.TOWNBLOCKTYPES_TYPES.getRoot()));
}

public static String getKingPrefix(Resident resident) {

Expand Down Expand Up @@ -1404,24 +1512,52 @@ public static double getOutpostCost() {
return getDouble(ConfigNodes.ECO_PRICE_OUTPOST);
}

public static List<String> getSwitchMaterials() {
public static Set<Material> getSwitchMaterials() {

return SwitchUseMaterials;
return switchUseMaterials;
}

public static List<String> getItemUseMaterials() {
public static Set<Material> getItemUseMaterials() {

return ItemUseMaterials;
return itemUseMaterials;
}

/**
* For compatibility with custom plot types, this has been deprecated. Please use {@link #isSwitchMaterial(Material, Location)} instead.
* @param mat The name of the material.
* @return Whether this is a switch material or not.
*/
@Deprecated
public static boolean isSwitchMaterial(String mat) {
return switchUseMaterials.contains(Material.matchMaterial(mat));
}

public static boolean isSwitchMaterial(Material material, Location location) {
if (!TownyAPI.getInstance().isWilderness(location)) {

return SwitchUseMaterials.contains(mat);
TownBlock townBlock = TownyAPI.getInstance().getTownBlock(location);
return townBlock.getData().getSwitchIds().contains(material);
} else
return switchUseMaterials.contains(material);
}

/**
* For compatibility with custom plot types, this has been deprecated. Please use {@link #isItemUseMaterial(Material, Location)} instead.
* @param mat The name of the material.
* @return Whether this is an item use material or not.
*/
@Deprecated
public static boolean isItemUseMaterial(String mat) {
return itemUseMaterials.contains(Material.matchMaterial(mat));
}

public static boolean isItemUseMaterial(Material material, Location location) {
if (!TownyAPI.getInstance().isWilderness(location)) {

return ItemUseMaterials.contains(mat);
TownBlock townBlock = TownyAPI.getInstance().getTownBlock(location);
return townBlock.getData().getItemUseIds().contains(material);
} else
return itemUseMaterials.contains(material);
}

public static List<String> getFireSpreadBypassMaterials() {
Expand Down Expand Up @@ -2723,8 +2859,9 @@ public static double getNationRequiresProximity() {
return getDouble(ConfigNodes.GTOWN_SETTINGS_NATION_REQUIRES_PROXIMITY);
}

@Deprecated
public static List<String> getFarmPlotBlocks() {
return getStrArr(ConfigNodes.GTOWN_FARM_PLOT_ALLOW_BLOCKS);
return Collections.emptyList();
}

public static List<String> getFarmAnimals() {
Expand Down
Loading

0 comments on commit df62c45

Please sign in to comment.