Skip to content

Commit

Permalink
More work on upgrade commands, finished draft of chunkloader
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Jul 15, 2022
1 parent c6879e9 commit a38525f
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 49 deletions.
7 changes: 4 additions & 3 deletions src/api/java/dev/compactmods/machines/api/core/Messages.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.compactmods.machines.api.core;

import com.mojang.datafixers.kinds.Const;
import io.netty.util.Constant;
import net.minecraft.resources.ResourceLocation;

public abstract class Messages {
Expand Down Expand Up @@ -36,6 +34,9 @@ public abstract class Messages {
*/
public static final ResourceLocation NOT_ROOM_OWNER = new ResourceLocation(Constants.MOD_ID, "not_the_room_owner");
public static final ResourceLocation UPGRADE_APPLIED = new ResourceLocation(Constants.MOD_ID, "upgrade_applied");
public static final ResourceLocation UPGRADE_FAILED = new ResourceLocation(Constants.MOD_ID, "upgrade_failed");
public static final ResourceLocation UPGRADE_ADD_FAILED = new ResourceLocation(Constants.MOD_ID, "upgrade_add_failed");
public static final ResourceLocation UPGRADE_REMOVED = new ResourceLocation(Constants.MOD_ID, "upgrade_removed");
public static final ResourceLocation UPGRADE_REM_FAILED = new ResourceLocation(Constants.MOD_ID, "upgrade_remove_failed");
public static final ResourceLocation ALREADY_HAS_UPGRADE = new ResourceLocation(Constants.MOD_ID, "upgrade_already_present");
public static final ResourceLocation UPGRADE_NOT_PRESENT = new ResourceLocation(Constants.MOD_ID, "upgrade_not_present");
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public abstract class Tooltips {
public static final ResourceLocation UNKNOWN_TUNNEL_TYPE = new ResourceLocation(Constants.MOD_ID, "unknown_tunnel_type");
public static final ResourceLocation ROOM_NAME = new ResourceLocation(Constants.MOD_ID, "room_name");
public static final ResourceLocation ROOM_UPGRADE_TYPE = new ResourceLocation(Constants.MOD_ID, "room_upgrade_type");
public static final ResourceLocation TUTORIAL_APPLY_ROOM_UPGRADE = new ResourceLocation(Constants.MOD_ID, "tutorial_apply_room_upgrade");

public static abstract class Machines {
public static final ResourceLocation ID = new ResourceLocation(Constants.MOD_ID, "machine.id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public interface RoomUpgrade extends IForgeRegistryEntry<RoomUpgrade> {

String UNNAMED_TRANS_KEY = "item." + MOD_ID + ".upgrades.unnamed";

default String getTranslationKey() {
final var rid = this.getRegistryName();
return "item." + rid.getNamespace() + ".upgrades." + rid.getPath().replace('/', '.');
}

default String getTranslationKey(ItemStack stack) {
final var rid = this.getRegistryName();
return "item." + rid.getNamespace() + ".upgrades." + rid.getPath().replace('/', '.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.compactmods.machines.api.core.CMCommands;
import dev.compactmods.machines.api.core.Messages;
import dev.compactmods.machines.api.core.Tooltips;
import dev.compactmods.machines.api.room.upgrade.RoomUpgrade;
import dev.compactmods.machines.api.tunnels.TunnelDefinition;
import dev.compactmods.machines.core.Registration;
import dev.compactmods.machines.core.Tunnels;
Expand All @@ -15,7 +16,11 @@
import net.minecraft.core.Direction;
import net.minecraft.data.DataGenerator;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraftforge.common.data.LanguageProvider;
import net.minecraftforge.registries.RegistryObject;

import java.util.function.Supplier;

import static org.apache.commons.lang3.StringUtils.capitalize;

Expand Down Expand Up @@ -56,8 +61,14 @@ protected void addTooltip(ResourceLocation id, String translation) {
add(TranslationUtil.tooltipId(id), translation);
}

protected void addTunnel(TunnelDefinition tunnel, String name) {
add(TranslationUtil.tunnelId(tunnel.getRegistryName()), name);
protected void addTunnel(Supplier<TunnelDefinition> tunnel, String name) {
add(TranslationUtil.tunnelId(tunnel.get().getRegistryName()), name);
}

void addUpgradeItem(Supplier<RoomUpgrade> upgrade, String translation) {
final var u = upgrade.get();
if(u != null)
add(u.getTranslationKey(), translation);
}

protected void addAdvancementTranslations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import dev.compactmods.machines.core.Registration;
import dev.compactmods.machines.core.Tunnels;
import dev.compactmods.machines.room.RoomSize;
import dev.compactmods.machines.upgrade.MachineRoomUpgrades;
import net.minecraft.data.DataGenerator;
import net.minecraft.world.item.Item;
import net.minecraftforge.registries.RegistryObject;

import static org.apache.commons.lang3.StringUtils.capitalize;

Expand All @@ -29,6 +32,8 @@ protected void addTranslations() {
addMessage(Messages.TELEPORT_OUT_OF_BOUNDS, "An otherworldly force prevents your teleportation.");
addMessage(Messages.NO_TUNNEL_SIDE, "There are no available sides for this tunnel type.");

addMessage(Messages.NOT_ROOM_OWNER, "You are not the room owner; only %s may make changes.");

// 1 = Display Name, 2 = Chunk, 3 = Size
addMessage(Messages.PLAYER_ROOM_INFO, "Player '%1$s' is inside a %3$s room at %2$s.");
addMessage(Messages.MACHINE_ROOM_INFO, "Machine at %1$s is bound to a %2$s size room at %3$s");
Expand All @@ -48,9 +53,9 @@ protected void addTranslations() {

addAdvancementTranslations();

add(Registration.BLOCK_BREAKABLE_WALL.get(), "Compact Machine Wall");
add(Registration.BLOCK_SOLID_WALL.get(), "Solid Compact Machine Wall");
add(Tunnels.BLOCK_TUNNEL_WALL.get(), "Solid Compact Machine Wall (with Tunnel)");
addBlock(Registration.BLOCK_BREAKABLE_WALL, "Compact Machine Wall");
addBlock(Registration.BLOCK_SOLID_WALL, "Solid Compact Machine Wall");
addBlock(Tunnels.BLOCK_TUNNEL_WALL, "Solid Compact Machine Wall (with Tunnel)");

add(Registration.PERSONAL_SHRINKING_DEVICE.get(), "Personal Shrinking Device");

Expand All @@ -59,9 +64,9 @@ protected void addTranslations() {

add(RoomUpgrade.UNNAMED_TRANS_KEY, "Unnamed Room Upgrade");

addTunnel(Tunnels.ITEM_TUNNEL_DEF.get(), "Item Tunnel");
addTunnel(Tunnels.FLUID_TUNNEL_DEF.get(), "Fluid Tunnel");
addTunnel(Tunnels.FORGE_ENERGY.get(), "Energy Tunnel");
addTunnel(Tunnels.ITEM_TUNNEL_DEF, "Item Tunnel");
addTunnel(Tunnels.FLUID_TUNNEL_DEF, "Fluid Tunnel");
addTunnel(Tunnels.FORGE_ENERGY, "Energy Tunnel");
// addTunnel(Tunnels.REDSTONE_IN_DEF.get(), "Redstone Tunnel (In)");
// addTunnel(Tunnels.REDSTONE_OUT_DEF.get(), "Redstone Tunnel (Out)");

Expand All @@ -80,7 +85,22 @@ protected void addTranslations() {
addTooltip(Tooltips.UNKNOWN_TUNNEL_TYPE, "Unknown Tunnel Type (%s)");

addTooltip(Tooltips.ROOM_NAME, "Bound to room: %s");

//region Upgrades
addUpgradeItem(MachineRoomUpgrades.CHUNKLOAD, "Chunkloader Upgrade");

addMessage(Messages.ALREADY_HAS_UPGRADE, "Upgrade has already been applied to room.");
addMessage(Messages.UPGRADE_NOT_PRESENT, "Upgrade is not applied to the room.");

addMessage(Messages.UPGRADE_APPLIED, "Upgrade applied to room.");
addMessage(Messages.UPGRADE_ADD_FAILED, "Upgrade failed to apply to room.");

addMessage(Messages.UPGRADE_REMOVED, "Upgrade removed from room.");
addMessage(Messages.UPGRADE_REM_FAILED, "Upgrade removal failed to apply to room.");

addTooltip(Tooltips.ROOM_UPGRADE_TYPE, "Type: %s");
addTooltip(Tooltips.TUTORIAL_APPLY_ROOM_UPGRADE, "Use on a bound machine block to apply upgrade.");
//endregion

addCommand(CMCommands.CANNOT_GIVE_MACHINE, "Failed to give a new machine to player.");
addCommand(CMCommands.MACHINE_GIVEN, "Created a new machine item and gave it to %s.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class ServerConfig {
private static ForgeConfigSpec.IntValue GIVE_MACHINE;
private static ForgeConfigSpec.IntValue CHANGE_SPAWN_LEVEL;

private static ForgeConfigSpec.IntValue CHANGE_ROOM_UPGRADES;

static {
generateConfig();
}
Expand Down Expand Up @@ -70,6 +72,10 @@ private static void generateConfig() {
.comment("Command level required for changing room spawn information.")
.defineInRange("spawn", Commands.LEVEL_GAMEMASTERS, Commands.LEVEL_ALL, Commands.LEVEL_OWNERS);

CHANGE_ROOM_UPGRADES = builder
.comment("Command level required for changing room upgrades.")
.defineInRange("upgrades", Commands.LEVEL_GAMEMASTERS, Commands.LEVEL_ALL, Commands.LEVEL_OWNERS);

builder.pop(2);

CONFIG = builder.build();
Expand All @@ -86,4 +92,6 @@ public static int giveMachineLevel() {
public static int changeRoomSpawn() {
return CHANGE_SPAWN_LEVEL.get();
}

public static int changeUpgrades() { return CHANGE_ROOM_UPGRADES.get(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.compactmods.machines.CompactMachines;
import dev.compactmods.machines.api.core.CMTags;
import dev.compactmods.machines.api.core.Messages;
import dev.compactmods.machines.api.upgrade.RoomUpgradeHelper;
import dev.compactmods.machines.config.ServerConfig;
import dev.compactmods.machines.core.*;
import dev.compactmods.machines.i18n.TranslationUtil;
Expand All @@ -17,6 +16,7 @@
import dev.compactmods.machines.room.menu.MachineRoomMenu;
import dev.compactmods.machines.tunnel.graph.TunnelConnectionGraph;
import dev.compactmods.machines.upgrade.MachineRoomUpgrades;
import dev.compactmods.machines.upgrade.RoomUpgradeItem;
import dev.compactmods.machines.upgrade.RoomUpgradeManager;
import dev.compactmods.machines.util.PlayerUtil;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -250,23 +250,20 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player

// Upgrade Item
if (mainItem.is(CMTags.ROOM_UPGRADE_ITEM)) {
RoomUpgradeHelper.getTypeFrom(mainItem).ifPresent(type -> {
final var reg = MachineRoomUpgrades.REGISTRY.get();
if (!reg.containsKey(type))
return;

final var reg = MachineRoomUpgrades.REGISTRY.get();
if(mainItem.getItem() instanceof RoomUpgradeItem upItem) {
if (level.getBlockEntity(pos) instanceof CompactMachineBlockEntity tile) {
tile.getConnectedRoom().ifPresent(room -> {
Rooms.getOwner(server, room).ifPresent(prof -> {
if(!player.getUUID().equals(prof.getId())) {
if (!player.getUUID().equals(prof.getId())) {
player.displayClientMessage(TranslationUtil.message(Messages.NOT_ROOM_OWNER, prof.getName()), true);
return;
}

final var upg = reg.getValue(type);
final var upg = upItem.getUpgradeType();
final var manager = RoomUpgradeManager.get(server.getLevel(Registration.COMPACT_DIMENSION));

if(manager.hasUpgrade(room, upg)) {
if (manager.hasUpgrade(room, upg)) {
player.displayClientMessage(TranslationUtil.message(Messages.ALREADY_HAS_UPGRADE), true);
} else {
final var added = manager.addUpgrade(upg, room);
Expand All @@ -275,14 +272,14 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
player.displayClientMessage(TranslationUtil.message(Messages.UPGRADE_APPLIED)
.withStyle(ChatFormatting.DARK_GREEN), true);
} else {
player.displayClientMessage(TranslationUtil.message(Messages.UPGRADE_FAILED)
player.displayClientMessage(TranslationUtil.message(Messages.UPGRADE_ADD_FAILED)
.withStyle(ChatFormatting.DARK_RED), true);
}
}
});
});
}
});
}
}

return InteractionResult.SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ public class ChunkloadUpgrade extends ForgeRegistryEntry<RoomUpgrade> implements
).apply(i, t -> new ChunkloadUpgrade()));

@Override
public void onAdded(ServerLevel level, ChunkPos room) { forceLoad(level, room); }
public void onAdded(ServerLevel level, ChunkPos room) {
forceLoad(level, room);
}

@Override
public void onRemoved(ServerLevel level, ChunkPos room) { normalLoad(level, room); }
public void onRemoved(ServerLevel level, ChunkPos room) {
normalLoad(level, room);
}

@Override
public void onLevelLoaded(ServerLevel level, ChunkPos room) { forceLoad(level, room); }
public void onLevelLoaded(ServerLevel level, ChunkPos room) {
forceLoad(level, room);
}

@Override
public void onLevelUnloaded(ServerLevel level, ChunkPos room) { normalLoad(level, room); }
public void onLevelUnloaded(ServerLevel level, ChunkPos room) {
normalLoad(level, room);
}

private void forceLoad(ServerLevel level, ChunkPos room) {
final var chunks = level.getChunkSource();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.compactmods.machines.upgrade;

import dev.compactmods.machines.api.room.upgrade.RoomUpgrade;
import dev.compactmods.machines.api.upgrade.RoomUpgradeHelper;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.CreativeModeTab;
Expand All @@ -18,4 +19,9 @@ public void fillItemCategory(CreativeModeTab tab, NonNullList<ItemStack> stacks)

stacks.add(stack);
}

@Override
public RoomUpgrade getUpgradeType() {
return MachineRoomUpgrades.CHUNKLOAD.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MachineRoomUpgrades {
public static final Supplier<IForgeRegistry<RoomUpgrade>> REGISTRY = UPGRADES.makeRegistry(RoomUpgrade.class, RegistryBuilder::new);

// ================================================================================================================
static final RegistryObject<RoomUpgrade> CHUNKLOAD = UPGRADES.register(ChunkloadUpgrade.REG_ID.getPath(), ChunkloadUpgrade::new);
public static final RegistryObject<RoomUpgrade> CHUNKLOAD = UPGRADES.register(ChunkloadUpgrade.REG_ID.getPath(), ChunkloadUpgrade::new);

public static final RegistryObject<Item> CHUNKLOADER = Registration.ITEMS.register("chunkloader_upgrade", () -> new ChunkloadUpgradeItem(new Item.Properties()
.tab(CompactMachines.COMPACT_MACHINES_ITEMS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.compactmods.machines.api.room.upgrade.RoomUpgrade;
import dev.compactmods.machines.api.upgrade.RoomUpgradeHelper;
import dev.compactmods.machines.i18n.TranslationUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
Expand All @@ -21,6 +22,8 @@ public RoomUpgradeItem(Properties props) {
super(props);
}

public abstract RoomUpgrade getUpgradeType();

@Override
public Component getName(ItemStack stack) {
String key = RoomUpgradeHelper.getTypeFrom(stack)
Expand All @@ -33,10 +36,16 @@ public Component getName(ItemStack stack) {

@Override
public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> info, TooltipFlag flag) {
if (Screen.hasShiftDown()) {
info.add(TranslationUtil.tooltip(Tooltips.TUTORIAL_APPLY_ROOM_UPGRADE).withStyle(ChatFormatting.ITALIC));
} else {
info.add(TranslationUtil.tooltip(Tooltips.HINT_HOLD_SHIFT).withStyle(ChatFormatting.DARK_GRAY));
}

// Show upgrade type while sneaking, or if advanced tooltips are on
if(Screen.hasShiftDown() || flag.isAdvanced()) {
if (Screen.hasShiftDown() || flag.isAdvanced()) {
RoomUpgradeHelper.getTypeFrom(stack).ifPresent(upgType -> {
info.add(TranslationUtil.tooltip(Tooltips.ROOM_UPGRADE_TYPE, upgType));
info.add(TranslationUtil.tooltip(Tooltips.ROOM_UPGRADE_TYPE, upgType).withStyle(ChatFormatting.DARK_GRAY));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public <T extends RoomUpgrade> boolean removeUpgrade(T upgrade, ChunkPos room) {

final var uNode = upgradeNodes.get(upgrade.getRegistryName());
final var rNode = roomNodes.get(room);
graph.removeEdge(uNode, rNode);
graph.removeEdge(rNode, uNode);
setDirty();
return true;
}
Expand Down Expand Up @@ -192,6 +192,6 @@ public boolean hasUpgrade(ChunkPos room, RoomUpgrade upgrade) {
final var upgNode = upgradeNodes.get(upgrade.getRegistryName());
final var roomNode = roomNodes.get(room);

return graph.hasEdgeConnecting(upgNode, roomNode);
return graph.hasEdgeConnecting(roomNode, upgNode);
}
}
Loading

0 comments on commit a38525f

Please sign in to comment.