diff --git a/src/main/java/com/awakenedredstone/neoskies/api/island/CurrentSettings.java b/src/main/java/com/awakenedredstone/neoskies/api/island/CurrentSettings.java new file mode 100644 index 0000000..c73821d --- /dev/null +++ b/src/main/java/com/awakenedredstone/neoskies/api/island/CurrentSettings.java @@ -0,0 +1,25 @@ +package com.awakenedredstone.neoskies.api.island; + +import com.awakenedredstone.neoskies.logic.settings.IslandSettings; + +public class CurrentSettings { + protected final IslandSettings settings; + protected PermissionLevel permissionLevel; + + public CurrentSettings(IslandSettings settings, PermissionLevel permissionLevel) { + this.settings = settings; + this.permissionLevel = permissionLevel; + } + + public IslandSettings getSettings() { + return settings; + } + + public PermissionLevel getPermissionLevel() { + return permissionLevel; + } + + public void setPermissionLevel(PermissionLevel permissionLevel) { + this.permissionLevel = permissionLevel; + } +} diff --git a/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettings.java b/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettings.java deleted file mode 100644 index e032721..0000000 --- a/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettings.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.awakenedredstone.neoskies.api.island; - -public class IslandSettings { - public PermissionLevel permissionLevel; - - public IslandSettings(PermissionLevel permissionLevel) { - this.permissionLevel = permissionLevel; - } -} diff --git a/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettingsList.java b/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettingsList.java deleted file mode 100644 index f87bc11..0000000 --- a/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettingsList.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.awakenedredstone.neoskies.api.island; - -import net.minecraft.util.Identifier; - -public interface IslandSettingsList { - Identifier getIdentifier(); -} diff --git a/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettingsManager.java b/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettingsManager.java deleted file mode 100644 index 2c6342e..0000000 --- a/src/main/java/com/awakenedredstone/neoskies/api/island/IslandSettingsManager.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.awakenedredstone.neoskies.api.island; - -import com.awakenedredstone.neoskies.gui.polymer.CBGuiElement; -import com.awakenedredstone.neoskies.gui.polymer.CBGuiElementBuilder; -import com.awakenedredstone.neoskies.util.MapBuilder; -import net.minecraft.item.Items; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.entry.RegistryEntry; -import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvents; -import net.minecraft.text.Style; -import net.minecraft.text.Text; -import net.minecraft.util.Identifier; -import com.awakenedredstone.neoskies.logic.Island; -import com.awakenedredstone.neoskies.logic.SkylandsRegistries; -import com.awakenedredstone.neoskies.util.Texts; - -import java.util.*; - -@Deprecated //TODO: Replace with just registry -public class IslandSettingsManager { - private static final SortedMap defaultSettings = new TreeMap<>(); - private static final Map icons = new HashMap<>(); - - public static boolean registerIcon(Identifier identifier, CBGuiElement icon) { - if (icons.containsKey(identifier)) return false; - icons.put(identifier, icon); - return true; - } - - public static boolean register(Identifier identifier, IslandSettings settings, CBGuiElement icon) { - registerIcon(identifier, icon); - return register(identifier, settings); - } - - public static boolean register(Identifier identifier, IslandSettings settings) { - if (defaultSettings.containsKey(identifier)) return false; - defaultSettings.put(identifier, settings); - return true; - } - - public static void update(Map toUpdate) { - defaultSettings.forEach((identifier, settings) -> { - if (!toUpdate.containsKey(identifier)) toUpdate.put(identifier, settings); - }); - } - - public static Map getDefaultSettings() { - return defaultSettings; - } - - public static Map getIcons() { - return icons; - } - - public static CBGuiElement getIcon(Identifier identifier, Island island) { - CBGuiElementBuilder builder = icons.getOrDefault(identifier, new CBGuiElementBuilder(Items.PAPER).build()).getBuilder(); - builder.hideDefaultTooltip() - .setLore(buildLore(island, identifier)) - .setName(Texts.of("island_settings." + identifier.toTranslationKey())) - .setCallback((index, type, action, gui) -> { - switch (type) { - case MOUSE_LEFT -> { - gui.getPlayer().playSoundToPlayer(SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.MASTER, 0.3f, 1); - offsetPermission(island.getSettings().get(identifier), 1); - } - case MOUSE_RIGHT -> { - gui.getPlayer().playSoundToPlayer(SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.MASTER, 0.3f, 1); - offsetPermission(island.getSettings().get(identifier), -1); - } - } - }); - return builder.build(); - } - - private static List buildLore(Island island, Identifier identifier) { - List lore = new ArrayList<>(); - lore.add(Text.empty().setStyle(Style.EMPTY.withItalic(false)).append(Texts.of("island_settings." + identifier.toTranslationKey() + ".description"))); - lore.add(Text.empty()); - int value = island.getSettings().get(identifier).permissionLevel.getLevel(); - List levels = new ArrayList<>(); - for (Map.Entry, PermissionLevel> entry : SkylandsRegistries.PERMISSION_LEVELS.getEntrySet()) { - levels.add(entry.getValue().getLevel()); - } - if (!levels.contains(value)) levels.add(value); - levels.sort(Integer::compareTo); - Collections.reverse(levels); - - for (Integer level : levels) { - Text levelText = Texts.of("island_settings/level." + level); - Map placeholders = new MapBuilder() - .put("level", levelText) - .build(); - - if (value == level) { - lore.add(Text.empty().setStyle(Style.EMPTY.withItalic(false)).append(Texts.of(Text.translatable("island_settings/selected"), placeholders))); - } else { - lore.add(Text.empty().setStyle(Style.EMPTY.withItalic(false)).append(Texts.of(Text.translatable("island_settings/unselected"), placeholders))); - } - } - - return lore; - } - - private static void offsetPermission(IslandSettings settings, int offset) { - int position = 0; - List levels = SkylandsRegistries.PERMISSION_LEVELS.streamEntries().map(RegistryEntry.Reference::value).toList(); - int length = levels.size(); - for (int i = 0; i < length; i++) { - if (levels.get(i) == settings.permissionLevel) { - position = i; - break; - } - } - - position += offset; - while (position < 0) { - position += length; - } - - while (position >= length) { - position -= length; - } - - settings.permissionLevel = levels.get(position); - } -} diff --git a/src/main/java/com/awakenedredstone/neoskies/gui/IslandSettingsGui.java b/src/main/java/com/awakenedredstone/neoskies/gui/IslandSettingsGui.java index 130077e..c2eddda 100644 --- a/src/main/java/com/awakenedredstone/neoskies/gui/IslandSettingsGui.java +++ b/src/main/java/com/awakenedredstone/neoskies/gui/IslandSettingsGui.java @@ -1,9 +1,11 @@ package com.awakenedredstone.neoskies.gui; -import com.awakenedredstone.neoskies.api.island.IslandSettingsManager; +import com.awakenedredstone.neoskies.api.island.CurrentSettings; import com.awakenedredstone.neoskies.gui.polymer.CBGuiElement; import com.awakenedredstone.neoskies.gui.polymer.CBGuiElementBuilder; import com.awakenedredstone.neoskies.gui.polymer.CBSimpleGuiBuilder; +import com.awakenedredstone.neoskies.logic.Island; +import com.awakenedredstone.neoskies.util.Texts; import com.awakenedredstone.neoskies.util.UIUtils; import eu.pb4.sgui.api.gui.GuiInterface; import eu.pb4.sgui.api.gui.SimpleGui; @@ -14,21 +16,17 @@ import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; -import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; import org.jetbrains.annotations.Nullable; -import com.awakenedredstone.neoskies.api.island.IslandSettings; -import com.awakenedredstone.neoskies.logic.Island; -import com.awakenedredstone.neoskies.util.Texts; +import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.function.Consumer; public class IslandSettingsGui { private final Island island; private final GuiInterface parent; - private final List> entries; + private final List entries; private final Consumer updateGui; private final Consumer simpleUpdateGui; private int page = 0; @@ -41,7 +39,7 @@ public class IslandSettingsGui { public IslandSettingsGui(Island island, @Nullable GuiInterface parent) { this.island = island; this.parent = parent; - this.entries = island.getSettings().entrySet().stream().toList(); + this.entries = new ArrayList<>(island.getSettings().values()); updateGui = gui -> { UIUtils.fillGui(gui, filler); @@ -52,8 +50,8 @@ public IslandSettingsGui(Island island, @Nullable GuiInterface parent) { int offset = page * 28; for (int i = offset; i < Math.min(offset + 28, island.getSettings().size()); i++) { if ((slot + 1) % 9 == 0 && slot > 10) slot += 2; - Map.Entry pageEntry = entries.get(i); - gui.setSlot(slot++, IslandSettingsManager.getIcon(pageEntry.getKey(), island)); + CurrentSettings currentSettings = entries.get(i); + gui.setSlot(slot++, currentSettings.getSettings().buildIcon(island)); } if (page < getPageMax()) gui.setSlot(gui.getSize() - 8, nextPage); @@ -76,8 +74,9 @@ public IslandSettingsGui(Island island, @Nullable GuiInterface parent) { int offset = page * 28; for (int i = offset; i < Math.min(offset + 28, island.getSettings().size()); i++) { if ((slot + 1) % 9 == 0 && slot > 10) slot += 2; - Map.Entry pageEntry = entries.get(i); - gui.setSlot(slot++, IslandSettingsManager.getIcon(pageEntry.getKey(), island)); + CurrentSettings currentSettings = entries.get(i); + + gui.setSlot(slot++, currentSettings.getSettings().buildIcon(island)); } }; } diff --git a/src/main/java/com/awakenedredstone/neoskies/logic/Island.java b/src/main/java/com/awakenedredstone/neoskies/logic/Island.java index b380615..1231767 100644 --- a/src/main/java/com/awakenedredstone/neoskies/logic/Island.java +++ b/src/main/java/com/awakenedredstone/neoskies/logic/Island.java @@ -1,6 +1,16 @@ package com.awakenedredstone.neoskies.logic; -import com.awakenedredstone.neoskies.api.island.IslandSettingsManager; +import com.awakenedredstone.neoskies.SkylandsMain; +import com.awakenedredstone.neoskies.api.events.IslandEvents; +import com.awakenedredstone.neoskies.api.island.CurrentSettings; +import com.awakenedredstone.neoskies.api.island.PermissionLevel; +import com.awakenedredstone.neoskies.logic.economy.SkylandsEconomyAccount; +import com.awakenedredstone.neoskies.logic.registry.SkylandsRegistries; +import com.awakenedredstone.neoskies.logic.settings.IslandSettings; +import com.awakenedredstone.neoskies.logic.settings.IslandSettingsUtil; +import com.awakenedredstone.neoskies.util.Constants; +import com.awakenedredstone.neoskies.util.Players; +import com.awakenedredstone.neoskies.util.Texts; import eu.pb4.common.economy.api.EconomyAccount; import net.minecraft.block.Block; import net.minecraft.entity.player.PlayerEntity; @@ -17,20 +27,11 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.random.RandomSeed; import net.minecraft.world.Difficulty; -import net.minecraft.world.GameRules; import net.minecraft.world.biome.BiomeKeys; import net.minecraft.world.dimension.DimensionTypes; import net.minecraft.world.gen.chunk.FlatChunkGenerator; import net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig; import org.jetbrains.annotations.Nullable; -import com.awakenedredstone.neoskies.SkylandsMain; -import com.awakenedredstone.neoskies.api.events.IslandEvents; -import com.awakenedredstone.neoskies.api.island.IslandSettings; -import com.awakenedredstone.neoskies.api.island.PermissionLevel; -import com.awakenedredstone.neoskies.logic.economy.SkylandsEconomyAccount; -import com.awakenedredstone.neoskies.util.Constants; -import com.awakenedredstone.neoskies.util.Players; -import com.awakenedredstone.neoskies.util.Texts; import xyz.nucleoid.fantasy.Fantasy; import xyz.nucleoid.fantasy.RuntimeWorldConfig; import xyz.nucleoid.fantasy.RuntimeWorldHandle; @@ -42,7 +43,7 @@ //TODO: Advanced island settings public class Island { protected final Fantasy fantasy = Skylands.getInstance().fantasy; - protected final Map settings = new HashMap<>(); + protected final Map settings = new HashMap<>(); public final Member owner; private UUID islandId = UUID.randomUUID(); protected RuntimeWorldConfig islandConfig = null; @@ -123,13 +124,20 @@ public static Island fromNbt(NbtCompound nbt) { island.bans.add(Member.fromNbt(member)); } + for (IslandSettings islandSetting : SkylandsRegistries.ISLAND_SETTINGS) { + CurrentSettings currentSettings = new CurrentSettings(islandSetting, islandSetting.getDefaultLevel()); + island.settings.put(islandSetting.getIdentifier(), currentSettings); + } + NbtCompound settingsNbt = nbt.getCompound("settings"); settingsNbt.getKeys().forEach(key -> { + Identifier identifier = new Identifier(key); NbtCompound settingsDataNbt = settingsNbt.getCompound(key); PermissionLevel level = PermissionLevel.fromValue(settingsDataNbt.getString("permission")); + IslandSettings islandSettings = SkylandsRegistries.ISLAND_SETTINGS.get(identifier); if (level != null) { - IslandSettings islandSettings = new IslandSettings(level); - island.settings.put(new Identifier(key), islandSettings); + CurrentSettings currentSettings = new CurrentSettings(islandSettings, level); + island.settings.put(identifier, currentSettings); } }); @@ -140,8 +148,6 @@ public static Island fromNbt(NbtCompound nbt) { island.blocks.put(new Identifier(key), amount); }); - IslandSettingsManager.update(island.settings); - //TODO: Load gamerules into island return island; @@ -193,11 +199,15 @@ public NbtCompound toNbt() { } nbt.put("bans", bansNbt); - IslandSettingsManager.update(this.settings); + for (IslandSettings islandSetting : SkylandsRegistries.ISLAND_SETTINGS) { + CurrentSettings currentSettings = new CurrentSettings(islandSetting, islandSetting.getDefaultLevel()); + this.settings.put(islandSetting.getIdentifier(), currentSettings); + } + NbtCompound settingsNbt = new NbtCompound(); this.settings.forEach((identifier, settings) -> { NbtCompound settingsDataNbt = new NbtCompound(); - settingsDataNbt.putString("permission", settings.permissionLevel.getId().toString()); + settingsDataNbt.putString("permission", settings.getPermissionLevel().getId().toString()); settingsNbt.put(identifier.toString(), settingsDataNbt); }); nbt.put("settings", settingsNbt); @@ -277,16 +287,21 @@ public boolean isWithinBorder(BlockPos pos) { return new Box(new BlockPos(0, 0, 0)).expand(radius).withMinY(minY - 1).withMaxY(getOverworld().getTopY() + 1).contains(new Vec3d(pos.getX(), pos.getY(), pos.getZ())); } - public Map getSettings() { + public Map getSettings() { return settings; } - public IslandSettings getSettings(Identifier identifier) { - return settings.computeIfAbsent(identifier, id -> IslandSettingsManager.getDefaultSettings().get(id)); + @Nullable + public CurrentSettings getSettings(Identifier identifier) { + return settings.computeIfAbsent(identifier, IslandSettingsUtil::getModifiable); } public boolean isInteractionAllowed(Identifier identifier, PermissionLevel source) { - return source.getLevel() >= getSettings(identifier).permissionLevel.getLevel(); + CurrentSettings settings = getSettings(identifier); + if (settings == null) { + throw new NullPointerException("No Island Settings exist for the provided identifier " + identifier.toString()); + } + return source.getLevel() >= settings.getPermissionLevel().getLevel(); } public RuntimeWorldHandle getOverworldHandler() { diff --git a/src/main/java/com/awakenedredstone/neoskies/logic/registry/NeoSkiesIslandSettings.java b/src/main/java/com/awakenedredstone/neoskies/logic/registry/NeoSkiesIslandSettings.java index 7217a74..c3032c3 100644 --- a/src/main/java/com/awakenedredstone/neoskies/logic/registry/NeoSkiesIslandSettings.java +++ b/src/main/java/com/awakenedredstone/neoskies/logic/registry/NeoSkiesIslandSettings.java @@ -1,8 +1,8 @@ package com.awakenedredstone.neoskies.logic.registry; import com.awakenedredstone.neoskies.SkylandsMain; +import com.awakenedredstone.neoskies.logic.settings.IslandSettings; import com.awakenedredstone.neoskies.logic.tags.NeoSkiesBlockTags; -import com.awakenedredstone.neoskies.logic.settings.DefaultNeoSkiesIslandSettings; import com.awakenedredstone.neoskies.logic.tags.NeoSkiesEntityTags; import com.awakenedredstone.neoskies.logic.tags.NeoSkiesItemTags; import net.minecraft.block.Block; @@ -10,8 +10,6 @@ import net.minecraft.item.Item; import net.minecraft.item.Items; import net.minecraft.registry.Registry; -import com.awakenedredstone.neoskies.logic.SkylandsRegistries; -import com.awakenedredstone.neoskies.logic.settings.IslandSettings; import net.minecraft.registry.tag.TagKey; import java.lang.reflect.Field; @@ -25,42 +23,42 @@ public class NeoSkiesIslandSettings { protected static final Map>, IslandSettings> RULE_ENTITY_TAG = new HashMap<>(); protected static final Map, IslandSettings> RULE_ITEM_TAG = new HashMap<>(); - public static final IslandSettings PLACE_BLOCKS = new DefaultNeoSkiesIslandSettings("place/blocks", Items.STONE); - public static final IslandSettings PLACE_MINECARTS = new DefaultNeoSkiesIslandSettings("place/minecarts", Items.MINECART); - public static final IslandSettings BREAK_BLOCKS = new DefaultNeoSkiesIslandSettings("break/blocks", Items.WOODEN_PICKAXE); - - public static final IslandSettings USE_ANVIL = new DefaultNeoSkiesIslandSettings("use/anvil", Items.ANVIL); - public static final IslandSettings USE_ARMOR_STAND = new DefaultNeoSkiesIslandSettings("use/armor_stand", Items.ARMOR_STAND); - public static final IslandSettings USE_BEACON = new DefaultNeoSkiesIslandSettings("use/beacon", Items.BEACON); - public static final IslandSettings USE_BREWING_STAND = new DefaultNeoSkiesIslandSettings("use/brewing_stand", Items.BREWING_STAND); - public static final IslandSettings USE_COMPOSTER = new DefaultNeoSkiesIslandSettings("use/composter", Items.COMPOSTER); - public static final IslandSettings USE_CONTAINERS = new DefaultNeoSkiesIslandSettings("use/containers", Items.CHEST); - public static final IslandSettings USE_DOORS = new DefaultNeoSkiesIslandSettings("use/doors", Items.OAK_DOOR); - public static final IslandSettings USE_ITEM_FRAME = new DefaultNeoSkiesIslandSettings("use/item_frame", Items.ITEM_FRAME); - public static final IslandSettings USE_LECTERN = new DefaultNeoSkiesIslandSettings("use/lectern", Items.LECTERN); - public static final IslandSettings USE_LODESTONE = new DefaultNeoSkiesIslandSettings("use/lodestone", Items.LODESTONE); - public static final IslandSettings USE_REDSTONE = new DefaultNeoSkiesIslandSettings("use/redstone", Items.REDSTONE); - public static final IslandSettings USE_RESPAWN_ANCHOR = new DefaultNeoSkiesIslandSettings("use/respawn_anchor", Items.RESPAWN_ANCHOR); - public static final IslandSettings USE_SIGNS = new DefaultNeoSkiesIslandSettings("use/signs", Items.SPAWNER); - public static final IslandSettings USE_SPAWNER = new DefaultNeoSkiesIslandSettings("use/spawner", Items.SPAWNER); - public static final IslandSettings USE_TNT = new DefaultNeoSkiesIslandSettings("use/tnt", Items.TNT); - - public static final IslandSettings INTERACT_DRIPLEAF = new DefaultNeoSkiesIslandSettings("interact/dripleaf", Items.BIG_DRIPLEAF); - public static final IslandSettings INTERACT_DRAGON_EGG = new DefaultNeoSkiesIslandSettings("interact/dragon_egg", Items.DRAGON_EGG); - public static final IslandSettings INTERACT_SCULK = new DefaultNeoSkiesIslandSettings("interact/sculk", Items.SCULK); - public static final IslandSettings INTERACT_OTHER_BLOCKS = new DefaultNeoSkiesIslandSettings("interact/other_blocks", Items.BELL, true); - - public static final IslandSettings HARVEST = new DefaultNeoSkiesIslandSettings("harvest", Items.SWEET_BERRIES); - - public static final IslandSettings RIDE_MINECARTS = new DefaultNeoSkiesIslandSettings("ride/minecarts", Items.MINECART); - public static final IslandSettings RIDE_BOATS = new DefaultNeoSkiesIslandSettings("ride/boats", Items.MINECART); - public static final IslandSettings RIDE_OTHERS = new DefaultNeoSkiesIslandSettings("ride/others", Items.SADDLE); - public static final IslandSettings LEASH_ENTITY = new DefaultNeoSkiesIslandSettings("leash/entity", Items.LEAD); - public static final IslandSettings SHEAR_ENTITY = new DefaultNeoSkiesIslandSettings("shear/entity", Items.SHEARS); - - public static final IslandSettings HURT_HOSTILE = new DefaultNeoSkiesIslandSettings("hurt/hostile", Items.DIAMOND_SWORD); - public static final IslandSettings HURT_PASSIVE = new DefaultNeoSkiesIslandSettings("hurt/passive", Items.WOODEN_SWORD); - public static final IslandSettings BUCKET_PASSIVE = new DefaultNeoSkiesIslandSettings("bucket/passive", Items.AXOLOTL_BUCKET); + public static final IslandSettings PLACE_BLOCKS = register("place/blocks", Items.STONE); + public static final IslandSettings PLACE_MINECARTS = register("place/minecarts", Items.MINECART); + public static final IslandSettings BREAK_BLOCKS = register("break/blocks", Items.WOODEN_PICKAXE); + + public static final IslandSettings USE_ANVIL = register("use/anvil", Items.ANVIL); + public static final IslandSettings USE_ARMOR_STAND = register("use/armor_stand", Items.ARMOR_STAND); + public static final IslandSettings USE_BEACON = register("use/beacon", Items.BEACON); + public static final IslandSettings USE_BREWING_STAND = register("use/brewing_stand", Items.BREWING_STAND); + public static final IslandSettings USE_COMPOSTER = register("use/composter", Items.COMPOSTER); + public static final IslandSettings USE_CONTAINERS = register("use/containers", Items.CHEST); + public static final IslandSettings USE_DOORS = register("use/doors", Items.OAK_DOOR); + public static final IslandSettings USE_ITEM_FRAME = register("use/item_frame", Items.ITEM_FRAME); + public static final IslandSettings USE_LECTERN = register("use/lectern", Items.LECTERN); + public static final IslandSettings USE_LODESTONE = register("use/lodestone", Items.LODESTONE); + public static final IslandSettings USE_REDSTONE = register("use/redstone", Items.REDSTONE); + public static final IslandSettings USE_RESPAWN_ANCHOR = register("use/respawn_anchor", Items.RESPAWN_ANCHOR); + public static final IslandSettings USE_SIGNS = register("use/signs", Items.SPAWNER); + public static final IslandSettings USE_SPAWNER = register("use/spawner", Items.SPAWNER); + public static final IslandSettings USE_TNT = register("use/tnt", Items.TNT); + + public static final IslandSettings INTERACT_DRIPLEAF = register("interact/dripleaf", Items.BIG_DRIPLEAF); + public static final IslandSettings INTERACT_DRAGON_EGG = register("interact/dragon_egg", Items.DRAGON_EGG); + public static final IslandSettings INTERACT_SCULK = register("interact/sculk", Items.SCULK); + public static final IslandSettings INTERACT_OTHER_BLOCKS = register("interact/other_blocks", Items.BELL); + + public static final IslandSettings HARVEST = register("harvest", Items.SWEET_BERRIES); + + public static final IslandSettings RIDE_MINECARTS = register("ride/minecarts", Items.MINECART); + public static final IslandSettings RIDE_BOATS = register("ride/boats", Items.MINECART); + public static final IslandSettings RIDE_OTHERS = register("ride/others", Items.SADDLE); + public static final IslandSettings LEASH_ENTITY = register("leash/entity", Items.LEAD); + public static final IslandSettings SHEAR_ENTITY = register("shear/entity", Items.SHEARS); + + public static final IslandSettings HURT_HOSTILE = register("hurt/hostile", Items.DIAMOND_SWORD); + public static final IslandSettings HURT_PASSIVE = register("hurt/passive", Items.WOODEN_SWORD); + public static final IslandSettings BUCKET_PASSIVE = register("bucket/passive", Items.AXOLOTL_BUCKET); static { addBlockUseTag(NeoSkiesBlockTags.ANVIL, USE_ANVIL); @@ -127,6 +125,10 @@ public static Map, IslandSettings> getRuleItemTags() { return RULE_ITEM_TAG; } + private static IslandSettings register(String id, Item icon) { + return new IslandSettings(SkylandsMain.id(id), icon); + } + public static void init() { Class clazz = NeoSkiesIslandSettings.class; IslandSettings settings; @@ -139,6 +141,7 @@ public static void init() { if (field.getType() == IslandSettings.class) { settings = (IslandSettings) field.get(null); + SkylandsMain.LOGGER.info("Registering {}", settings.getIdentifier()); Registry.register(SkylandsRegistries.ISLAND_SETTINGS, settings.getIdentifier(), settings); } diff --git a/src/main/java/com/awakenedredstone/neoskies/logic/settings/DefaultNeoSkiesIslandSettings.java b/src/main/java/com/awakenedredstone/neoskies/logic/settings/DefaultNeoSkiesIslandSettings.java deleted file mode 100644 index 854f334..0000000 --- a/src/main/java/com/awakenedredstone/neoskies/logic/settings/DefaultNeoSkiesIslandSettings.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.awakenedredstone.neoskies.logic.settings; - -import net.minecraft.item.Item; -import com.awakenedredstone.neoskies.SkylandsMain; - -@Deprecated //TODO: Replace with builder -public class DefaultNeoSkiesIslandSettings extends IslandSettings { - public DefaultNeoSkiesIslandSettings(String path, Item icon) { - super(SkylandsMain.id(path), icon); - } - - public DefaultNeoSkiesIslandSettings(String path, Item icon, boolean silent) { - super(SkylandsMain.id(path), icon, silent); - } -} diff --git a/src/main/java/com/awakenedredstone/neoskies/logic/settings/IslandSettings.java b/src/main/java/com/awakenedredstone/neoskies/logic/settings/IslandSettings.java index 07e1bff..1d29f90 100644 --- a/src/main/java/com/awakenedredstone/neoskies/logic/settings/IslandSettings.java +++ b/src/main/java/com/awakenedredstone/neoskies/logic/settings/IslandSettings.java @@ -1,34 +1,43 @@ package com.awakenedredstone.neoskies.logic.settings; +import com.awakenedredstone.neoskies.api.island.PermissionLevel; import com.awakenedredstone.neoskies.gui.polymer.CBGuiElement; import com.awakenedredstone.neoskies.gui.polymer.CBGuiElementBuilder; +import com.awakenedredstone.neoskies.logic.Island; +import com.awakenedredstone.neoskies.logic.registry.SkylandsPermissionLevels; +import com.awakenedredstone.neoskies.logic.registry.SkylandsRegistries; +import com.awakenedredstone.neoskies.util.MapBuilder; +import com.awakenedredstone.neoskies.util.Texts; import net.minecraft.item.Item; +import net.minecraft.registry.RegistryKey; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvents; +import net.minecraft.text.Style; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; -import com.awakenedredstone.neoskies.api.island.IslandSettingsManager; -import com.awakenedredstone.neoskies.logic.registry.SkylandsPermissionLevels; -public abstract class IslandSettings { +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class IslandSettings { + protected final PermissionLevel defaultLevel; private final Identifier identifier; private final CBGuiElement icon; - private final boolean silent; - - public IslandSettings(Identifier identifier, CBGuiElement icon) { - this(identifier, icon, false); - } - public IslandSettings(Identifier identifier, CBGuiElement icon, boolean silent) { + public IslandSettings(Identifier identifier, CBGuiElement icon, PermissionLevel defaultLevel) { + this.defaultLevel = defaultLevel; this.identifier = identifier; this.icon = icon; - this.silent = silent; - register(); } - public IslandSettings(Identifier identifier, Item icon) { - this(identifier, new CBGuiElementBuilder(icon).build()); + public IslandSettings(Identifier identifier, CBGuiElement icon) { + this(identifier, icon, SkylandsPermissionLevels.MEMBER); } - public IslandSettings(Identifier identifier, Item icon, boolean silent) { - this(identifier, new CBGuiElementBuilder(icon).build(), silent); + public IslandSettings(Identifier identifier, Item icon) { + this(identifier, new CBGuiElementBuilder(icon).build()); } public Identifier getIdentifier() { @@ -39,15 +48,60 @@ public CBGuiElement getIcon() { return icon; } - public boolean isSilent() { - return silent; + public PermissionLevel getDefaultLevel() { + return defaultLevel; } - public String getTranslationKey() { - return identifier.toTranslationKey(); + public CBGuiElement buildIcon(Island island) { + CBGuiElementBuilder builder = icon.getBuilder(); + builder.hideDefaultTooltip() + .setLore(buildLore(island)) + .setName(Texts.of("island_settings." + identifier.toTranslationKey())) + .setCallback((index, type, action, gui) -> { + switch (type) { + case MOUSE_LEFT -> { + gui.getPlayer().playSoundToPlayer(SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.MASTER, 0.3f, 1); + IslandSettingsUtil.offsetPermission(island.getSettings().get(identifier), 1); + } + case MOUSE_RIGHT -> { + gui.getPlayer().playSoundToPlayer(SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.MASTER, 0.3f, 1); + IslandSettingsUtil.offsetPermission(island.getSettings().get(identifier), -1); + } + } + }); + return builder.build(); + } + + public List buildLore(Island island) { + List lore = new ArrayList<>(); + lore.add(Text.empty().setStyle(Style.EMPTY.withItalic(false)).append(Texts.of("island_settings." + identifier.toTranslationKey() + ".description"))); + lore.add(Text.empty()); + int value = island.getSettings().get(identifier).getPermissionLevel().getLevel(); + List levels = new ArrayList<>(); + for (Map.Entry, PermissionLevel> entry : SkylandsRegistries.PERMISSION_LEVELS.getEntrySet()) { + levels.add(entry.getValue().getLevel()); + } + if (!levels.contains(value)) levels.add(value); + levels.sort(Integer::compareTo); + Collections.reverse(levels); + + for (Integer level : levels) { + Text levelText = Texts.of("island_settings/level." + level); + Map placeholders = new MapBuilder() + .put("level", levelText) + .build(); + + if (value == level) { + lore.add(Text.empty().setStyle(Style.EMPTY.withItalic(false)).append(Texts.of(Text.translatable("island_settings/selected"), placeholders))); + } else { + lore.add(Text.empty().setStyle(Style.EMPTY.withItalic(false)).append(Texts.of(Text.translatable("island_settings/unselected"), placeholders))); + } + } + + return lore; } - protected void register() { - IslandSettingsManager.register(identifier, new com.awakenedredstone.neoskies.api.island.IslandSettings(SkylandsPermissionLevels.MEMBER), icon); + public String getTranslationKey() { + return identifier.toTranslationKey(); } } diff --git a/src/main/java/com/awakenedredstone/neoskies/logic/settings/IslandSettingsUtil.java b/src/main/java/com/awakenedredstone/neoskies/logic/settings/IslandSettingsUtil.java new file mode 100644 index 0000000..2d6abcb --- /dev/null +++ b/src/main/java/com/awakenedredstone/neoskies/logic/settings/IslandSettingsUtil.java @@ -0,0 +1,46 @@ +package com.awakenedredstone.neoskies.logic.settings; + +import com.awakenedredstone.neoskies.api.island.CurrentSettings; +import com.awakenedredstone.neoskies.api.island.PermissionLevel; +import com.awakenedredstone.neoskies.logic.registry.SkylandsRegistries; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class IslandSettingsUtil { + public static void offsetPermission(CurrentSettings settings, int offset) { + int position = 0; + List levels = SkylandsRegistries.PERMISSION_LEVELS.streamEntries().map(RegistryEntry.Reference::value).toList(); + int length = levels.size(); + for (int i = 0; i < length; i++) { + if (levels.get(i) == settings.getPermissionLevel()) { + position = i; + break; + } + } + + position += offset; + while (position < 0) { + position += length; + } + + while (position >= length) { + position -= length; + } + + settings.setPermissionLevel(levels.get(position)); + } + + @Nullable + public static CurrentSettings getModifiable(Identifier identifier) { + return getModifiable(SkylandsRegistries.ISLAND_SETTINGS.get(identifier)); + } + + @Nullable + public static CurrentSettings getModifiable(IslandSettings settings) { + if (settings == null) return null; + return new CurrentSettings(settings, settings.getDefaultLevel()); + } +} diff --git a/src/main/java/com/awakenedredstone/neoskies/util/AutoRegister.java b/src/main/java/com/awakenedredstone/neoskies/util/AutoRegister.java deleted file mode 100644 index 8162f15..0000000 --- a/src/main/java/com/awakenedredstone/neoskies/util/AutoRegister.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.awakenedredstone.neoskies.util; - -import net.minecraft.registry.Registry; - -public abstract class AutoRegister { -} diff --git a/src/main/java/com/awakenedredstone/neoskies/util/ServerUtils.java b/src/main/java/com/awakenedredstone/neoskies/util/ServerUtils.java index f1a371a..fb374fd 100644 --- a/src/main/java/com/awakenedredstone/neoskies/util/ServerUtils.java +++ b/src/main/java/com/awakenedredstone/neoskies/util/ServerUtils.java @@ -17,7 +17,7 @@ public static void protectionWarning(PlayerEntity player, String key) { } public static void protectionWarning(PlayerEntity player, IslandSettings settings) { - if (Skylands.getConfig().showProtectionMessages && !settings.isSilent()) { + if (Skylands.getConfig().showProtectionMessages) { actionbarPrefixed(player, "island_protection." + settings.getTranslationKey()); } } diff --git a/src/testmod/java/com/awakenedredstone/neoskies/test/SkylandsTestMain.java b/src/testmod/java/com/awakenedredstone/neoskies/test/SkylandsTestMain.java index bb1bb9d..f7ba0c8 100644 --- a/src/testmod/java/com/awakenedredstone/neoskies/test/SkylandsTestMain.java +++ b/src/testmod/java/com/awakenedredstone/neoskies/test/SkylandsTestMain.java @@ -1,9 +1,8 @@ package com.awakenedredstone.neoskies.test; -import com.awakenedredstone.neoskies.api.island.IslandSettingsManager; import com.awakenedredstone.neoskies.command.utils.CommandUtils; -import com.google.common.collect.ImmutableList; -import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.awakenedredstone.neoskies.logic.registry.SkylandsRegistries; +import com.awakenedredstone.neoskies.logic.settings.IslandSettings; import eu.pb4.polymer.core.api.item.PolymerBlockItem; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; @@ -14,8 +13,6 @@ import net.minecraft.item.Items; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; -import net.minecraft.server.command.CommandManager; -import net.minecraft.server.command.KillCommand; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.MutableText; import net.minecraft.text.Text; @@ -59,9 +56,9 @@ public void onInitialize() { .executes(context -> { ServerCommandSource source = context.getSource(); MutableText text = Text.empty(); - for (Identifier settings : IslandSettingsManager.getDefaultSettings().keySet()) { + for (IslandSettings settings : SkylandsRegistries.ISLAND_SETTINGS) { Language lang = Language.getInstance(); - String translationKey = settings.toTranslationKey(); + String translationKey = settings.getIdentifier().toTranslationKey(); boolean hasTranslation = lang.hasTranslation("island_protection." + translationKey); text.append(Text.literal(translationKey)); text.append(Text.literal(": ")); @@ -77,9 +74,9 @@ public void onInitialize() { .executes(context -> { ServerCommandSource source = context.getSource(); MutableText text = Text.empty(); - for (Identifier settings : IslandSettingsManager.getDefaultSettings().keySet()) { + for (IslandSettings settings : SkylandsRegistries.ISLAND_SETTINGS) { Language lang = Language.getInstance(); - String translationKey = settings.toTranslationKey(); + String translationKey = settings.getIdentifier().toTranslationKey(); boolean hasTranslation = lang.hasTranslation("island_settings." + translationKey); text.append(Text.literal(translationKey)); text.append(Text.literal(": ")); @@ -95,9 +92,9 @@ public void onInitialize() { .executes(context -> { ServerCommandSource source = context.getSource(); MutableText text = Text.empty(); - for (Identifier settings : IslandSettingsManager.getDefaultSettings().keySet()) { + for (IslandSettings settings : SkylandsRegistries.ISLAND_SETTINGS) { Language lang = Language.getInstance(); - String translationKey = settings.toTranslationKey(); + String translationKey = settings.getIdentifier().toTranslationKey(); boolean hasTranslation = lang.hasTranslation("island_settings." + translationKey + ".description"); text.append(Text.literal(translationKey)); text.append(Text.literal(": "));