Skip to content

Commit

Permalink
chore: Use registry for island settings
Browse files Browse the repository at this point in the history
Signed-off-by: Awakened-Redstone <[email protected]>
  • Loading branch information
Awakened-Redstone committed May 14, 2024
1 parent d257298 commit 68cd497
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 269 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Map.Entry<Identifier, IslandSettings>> entries;
private final List<CurrentSettings> entries;
private final Consumer<SlotGuiInterface> updateGui;
private final Consumer<SlotGuiInterface> simpleUpdateGui;
private int page = 0;
Expand All @@ -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);
Expand All @@ -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<Identifier, IslandSettings> 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);
Expand All @@ -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<Identifier, IslandSettings> pageEntry = entries.get(i);
gui.setSlot(slot++, IslandSettingsManager.getIcon(pageEntry.getKey(), island));
CurrentSettings currentSettings = entries.get(i);

gui.setSlot(slot++, currentSettings.getSettings().buildIcon(island));
}
};
}
Expand Down
57 changes: 36 additions & 21 deletions src/main/java/com/awakenedredstone/neoskies/logic/Island.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -42,7 +43,7 @@
//TODO: Advanced island settings
public class Island {
protected final Fantasy fantasy = Skylands.getInstance().fantasy;
protected final Map<Identifier, IslandSettings> settings = new HashMap<>();
protected final Map<Identifier, CurrentSettings> settings = new HashMap<>();
public final Member owner;
private UUID islandId = UUID.randomUUID();
protected RuntimeWorldConfig islandConfig = null;
Expand Down Expand Up @@ -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);
}
});

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Identifier, IslandSettings> getSettings() {
public Map<Identifier, CurrentSettings> 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() {
Expand Down
Loading

0 comments on commit 68cd497

Please sign in to comment.