Skip to content

Commit

Permalink
random improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed Mar 24, 2023
1 parent bd384d1 commit 3ac321a
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 71 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
id "fabric-loom" version "1.1-SNAPSHOT"
id "io.github.juuxel.loom-quiltflower" version "1.8.0"
id "io.github.p03w.machete" version "1.2.0"
}

archivesBaseName = project.archives_base_name
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.parallel = true
# Mod Properties
maven_group = ru.pinkgoosik
archives_base_name = skylands
mod_version = 0.3.4
mod_version = 0.3.5

# Dependencies | Check these on https://fabricmc.net/develop
minecraft_version = 1.19.4
Expand Down
51 changes: 49 additions & 2 deletions src/main/java/skylands/api/SkylandsAPI.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
package skylands.api;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.world.World;
import skylands.SkylandsMod;
import skylands.logic.Island;
import skylands.logic.Skylands;
import skylands.util.Worlds;

import java.util.Optional;
import java.util.UUID;

public class SkylandsAPI {
static Skylands skylands = Skylands.getInstance();

public static final Event<HubVisitEvent> ON_HUB_VISIT = EventFactory.createArrayBacked(HubVisitEvent.class, callbacks -> (player, world) -> {
for (HubVisitEvent callback : callbacks) {
callback.invoke(player, world);
}
});
@FunctionalInterface public interface HubVisitEvent {void invoke(PlayerEntity player, World world);}

public static final Event<GenericIslandEvent> ON_ISLAND_VISIT = EventFactory.createArrayBacked(GenericIslandEvent.class, callbacks -> (player, world, island) -> {
for (GenericIslandEvent callback : callbacks) {
callback.invoke(player, world, island);
}
});

public static final Event<GenericIslandEvent> ON_ISLAND_FIRST_LOAD = EventFactory.createArrayBacked(GenericIslandEvent.class, callbacks -> (player, world, island) -> {
for (GenericIslandEvent callback : callbacks) {
callback.invoke(player, world, island);
}
});
@FunctionalInterface public interface GenericIslandEvent {void invoke(PlayerEntity player, World world, Island island);}

public static final Event<NetherFirstLoad> ON_NETHER_FIRST_LOAD = EventFactory.createArrayBacked(NetherFirstLoad.class, callbacks -> (world, island) -> {
for (NetherFirstLoad callback : callbacks) {
callback.onLoad(world, island);
}
});
@FunctionalInterface public interface NetherFirstLoad {void onLoad(World world, Island island);}

public static Optional<Island> getIsland(PlayerEntity player) {
return skylands.islands.get(player);
}
Expand All @@ -24,8 +54,25 @@ public static Optional<Island> getIsland(UUID playerUuid) {
return skylands.islands.get(playerUuid);
}

public static boolean isIsland(World world) {
return isIsland(world.getRegistryKey());
}

public static boolean isIsland(RegistryKey<World> registryKey) {
var namespace = registryKey.getValue().getNamespace();
return namespace.equals(SkylandsMod.MOD_ID) || namespace.equals("nether") || namespace.equals("end");
}

public static Optional<Island> getIsland(World world) {
return Worlds.getIsland(world);
if (isIsland(world)) {
try {
return Skylands.instance.islands.get(UUID.fromString(world.getRegistryKey().getValue().getPath()));
}
catch (Exception e) {
return Optional.empty();
}
}
return Optional.empty();
}

}
4 changes: 2 additions & 2 deletions src/main/java/skylands/command/AcceptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import skylands.data.Components;
import skylands.data.SkylandsComponents;
import skylands.logic.Skylands;
import skylands.util.Players;
import skylands.util.Texts;
Expand Down Expand Up @@ -38,7 +38,7 @@ static void run(ServerPlayerEntity player, String ownerName) {
if(!invite.get().accepted) {
invite.get().accept(player);
player.sendMessage(Texts.prefixed("message.skylands.accept.success", map -> map.put("%owner%", ownerName)));
Components.PLAYER_DATA.get(player).addIsland(ownerName);
SkylandsComponents.PLAYER_DATA.get(player).addIsland(ownerName);
}
}
else {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/skylands/command/HomeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import skylands.SkylandsMod;
import skylands.data.Components;
import skylands.data.SkylandsComponents;
import skylands.logic.Skylands;
import skylands.util.Texts;

Expand All @@ -28,7 +28,7 @@ static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
var player = context.getSource().getPlayer();

if(player != null) {
var islands = Components.PLAYER_DATA.get(player).getIslands();
var islands = SkylandsComponents.PLAYER_DATA.get(player).getIslands();

String remains = builder.getRemaining();

Expand All @@ -50,7 +50,7 @@ static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
}))));
}

static void run(ServerPlayerEntity player) {
public static void run(ServerPlayerEntity player) {
Skylands.instance.islands.get(player).ifPresentOrElse(island -> {
if(player.getWorld().getRegistryKey().getValue().equals(SkylandsMod.id(player.getUuid().toString()))) {
player.sendMessage(Texts.prefixed("message.skylands.home.fail"));
Expand All @@ -62,7 +62,7 @@ static void run(ServerPlayerEntity player) {
}, () -> player.sendMessage(Texts.prefixed("message.skylands.home.no_island")));
}

static void run(ServerPlayerEntity visitor, String islandOwner) {
public static void run(ServerPlayerEntity visitor, String islandOwner) {
Skylands.instance.islands.get(islandOwner).ifPresentOrElse(island -> {
if(visitor.getWorld().getRegistryKey().getValue().equals(SkylandsMod.id(island.owner.uuid.toString()))) {
visitor.sendMessage(Texts.prefixed("message.skylands.visit_home.fail", map -> map.put("%owner%", islandOwner)));
Expand All @@ -71,16 +71,16 @@ static void run(ServerPlayerEntity visitor, String islandOwner) {
if(island.isMember(visitor)) {
visitor.sendMessage(Texts.prefixed("message.skylands.visit_home.success", map -> map.put("%owner%", islandOwner)));
island.visitAsMember(visitor);
Components.PLAYER_DATA.get(visitor).addIsland(islandOwner);
SkylandsComponents.PLAYER_DATA.get(visitor).addIsland(islandOwner);
}
else {
visitor.sendMessage(Texts.prefixed("message.skylands.visit_home.not_member"));
Components.PLAYER_DATA.get(visitor).removeIsland(islandOwner);
SkylandsComponents.PLAYER_DATA.get(visitor).removeIsland(islandOwner);
}
}
}, () -> {
visitor.sendMessage(Texts.prefixed("message.skylands.visit_home.no_island"));
Components.PLAYER_DATA.get(visitor).removeIsland(islandOwner);
SkylandsComponents.PLAYER_DATA.get(visitor).removeIsland(islandOwner);
});
}
}
2 changes: 1 addition & 1 deletion src/main/java/skylands/command/HubCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
})));
}

static void visit(ServerPlayerEntity player, MinecraftServer server) {
public static void visit(ServerPlayerEntity player, MinecraftServer server) {
player.sendMessage(Texts.prefixed("message.skylands.hub_visit"));
Skylands.instance.hub.visit(player);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/skylands/command/VisitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
}))));
}

static void run(ServerPlayerEntity visitor, ServerPlayerEntity owner) {
public static void run(ServerPlayerEntity visitor, ServerPlayerEntity owner) {
String ownerName = owner.getName().getString();

Skylands.instance.islands.get(owner).ifPresentOrElse(island -> {
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/skylands/data/AbstractPlayerData.java

This file was deleted.

6 changes: 0 additions & 6 deletions src/main/java/skylands/data/AbstractWorldData.java

This file was deleted.

19 changes: 16 additions & 3 deletions src/main/java/skylands/data/PlayerComponent.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package skylands.data;

import dev.onyxstudios.cca.api.v3.component.ComponentV3;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import skylands.logic.Skylands;
import skylands.util.Worlds;

import java.util.ArrayList;
import java.util.UUID;

public class PlayerComponent implements AbstractPlayerData {
public class PlayerComponent implements ComponentV3 {

public PlayerEntity player;

Expand All @@ -15,12 +19,10 @@ public PlayerComponent(PlayerEntity player) {
this.player = player;
}

@Override
public ArrayList<String> getIslands() {
return islands;
}

@Override
public void setIslands(ArrayList<String> islands) {
this.islands = islands;
}
Expand All @@ -43,6 +45,13 @@ public void readFromNbt(NbtCompound tag) {
String owner = islandsNbt.getString(String.valueOf(i));
this.islands.add(owner);
}

if(!tag.getString("lastIsland").isEmpty()) {
Skylands.instance.islands.get(UUID.fromString(tag.getString("lastIsland"))).ifPresent(island -> {
island.getWorld();
if(island.hasNether) island.getNether();
});
}
}

@Override
Expand All @@ -54,5 +63,9 @@ public void writeToNbt(NbtCompound tag) {
islandsNbt.putString(Integer.toString(i), owner);
}
tag.put("islands", islandsNbt);

Worlds.getIsland(player.getWorld()).ifPresent(island -> {
tag.putString("lastIsland", island.owner.uuid.toString());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import dev.onyxstudios.cca.api.v3.world.WorldComponentInitializer;
import skylands.SkylandsMod;

public class Components implements WorldComponentInitializer, EntityComponentInitializer {
public class SkylandsComponents implements WorldComponentInitializer, EntityComponentInitializer {
public static final ComponentKey<WorldComponent> WORLD_DATA = ComponentRegistryV3.INSTANCE.getOrCreate(SkylandsMod.id("world_data"), WorldComponent.class);
public static final ComponentKey<PlayerComponent> PLAYER_DATA = ComponentRegistryV3.INSTANCE.getOrCreate(SkylandsMod.id("player_data"), PlayerComponent.class);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/skylands/data/WorldComponent.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package skylands.data;

import dev.onyxstudios.cca.api.v3.component.ComponentV3;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import skylands.logic.Skylands;

public class WorldComponent implements AbstractWorldData {
public class WorldComponent implements ComponentV3 {
public World world;

public WorldComponent(World world) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/skylands/logic/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.Vec3d;
import skylands.api.SkylandsAPI;

import java.util.Set;

Expand All @@ -29,6 +30,8 @@ public void writeToNbt(NbtCompound nbt) {
}

public void visit(PlayerEntity player) {
player.teleport(Skylands.instance.server.getOverworld(), pos.getX(), pos.getY(), pos.getZ(), Set.of(), 0, 0);
var world = Skylands.getServer().getOverworld();
player.teleport(world, pos.getX(), pos.getY(), pos.getZ(), Set.of(), 0, 0);
SkylandsAPI.ON_HUB_VISIT.invoker().invoke(player, world);
}
}
38 changes: 23 additions & 15 deletions src/main/java/skylands/logic/Island.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.world.gen.chunk.FlatChunkGenerator;
import net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig;
import skylands.SkylandsMod;
import skylands.api.SkylandsAPI;
import skylands.util.Players;
import skylands.util.Texts;
import xyz.nucleoid.fantasy.Fantasy;
Expand Down Expand Up @@ -240,34 +241,40 @@ public ServerWorld getWorld() {
return handler.asWorld();
}

public void visitAsMember(PlayerEntity player) {
public void visit(PlayerEntity player, Vec3d pos) {
ServerWorld world = this.getWorld();
player.teleport(world, this.spawnPos.getX(), this.spawnPos.getY(), this.spawnPos.getZ(), Set.of(), 0, 0);
player.teleport(world, pos.getX(), pos.getY(), pos.getZ(), Set.of(), 0, 0);

if(!isMember(player)) {
Players.get(this.owner.name).ifPresent(owner -> {
if(!player.getUuid().equals(owner.getUuid())) {
owner.sendMessage(Texts.prefixed("message.skylands.island_visit.visit", map -> map.put("%visitor%", player.getName().getString())));
}
});
}

SkylandsAPI.ON_ISLAND_VISIT.invoker().invoke(player, world, this);

if (this.freshCreated) {
this.onFirstLoad();
this.onFirstLoad(player);
this.freshCreated = false;
}
}

public void visitAsMember(PlayerEntity player) {
this.visit(player, this.spawnPos);
}

public void visitAsVisitor(PlayerEntity player) {
ServerWorld world = this.getWorld();
player.teleport(world, this.visitsPos.getX(), this.visitsPos.getY(), this.visitsPos.getZ(), Set.of(), 0, 0);
if (this.freshCreated) {
this.onFirstLoad();
this.freshCreated = false;
}
Players.get(this.owner.name).ifPresent(owner -> {
if(!player.getUuid().equals(owner.getUuid())) {
owner.sendMessage(Texts.prefixed("message.skylands.island_visit.visit", map -> map.put("%visitor%", player.getName().getString())));
}
});
this.visit(player, this.visitsPos);
}

public void onFirstLoad() {
public void onFirstLoad(PlayerEntity player) {
ServerWorld world = this.getWorld();
StructureTemplate structure = server.getStructureTemplateManager().getTemplateOrBlank(SkylandsMod.id("start_island"));
StructurePlacementData data = new StructurePlacementData().setMirror(BlockMirror.NONE).setIgnoreEntities(true);
structure.place(world, new BlockPos(-7, 65, -7), new BlockPos(0, 0, 0), data, world.getRandom(), Block.NOTIFY_ALL);
SkylandsAPI.ON_ISLAND_FIRST_LOAD.invoker().invoke(player, world, this);
}

void onFirstNetherLoad(ServerWorld world) {
Expand All @@ -278,6 +285,7 @@ void onFirstNetherLoad(ServerWorld world) {
StructureTemplate structure = server.getStructureTemplateManager().getTemplateOrBlank(SkylandsMod.id("nether_island"));
StructurePlacementData data = new StructurePlacementData().setMirror(BlockMirror.NONE).setIgnoreEntities(true);
structure.place(world, new BlockPos(-7, 65, -7), new BlockPos(0, 0, 0), data, world.getRandom(), Block.NOTIFY_ALL);
SkylandsAPI.ON_NETHER_FIRST_LOAD.invoker().onLoad(world, this);

this.hasNether = true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/skylands/logic/Skylands.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public void writeToNbt(NbtCompound nbt) {
nbt.put("skylands", skylandsNbt);
}

public static MinecraftServer getServer() {
return getInstance().server;
}

public static Skylands getInstance() {
return Skylands.instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void tick(CallbackInfo ci) {
ServerPlayerEntity player = ServerPlayerEntity.class.cast(this);

if(!WorldProtection.canModify(world, player)) {
if(player.getPos().getY() < -74) {
if(player.getPos().getY() < world.getDimension().minY() - 10) {
Worlds.getIsland(world).ifPresentOrElse(island -> {
var pos = island.spawnPos;
player.teleport(island.getWorld(), pos.getX(), pos.getY(), pos.getZ(), Set.of(), 0, 0);
Expand Down
Loading

0 comments on commit 3ac321a

Please sign in to comment.