Skip to content

Commit 59c9f05

Browse files
committed
Infection command and refactoring
1 parent 8f2acc0 commit 59c9f05

File tree

7 files changed

+134
-14
lines changed

7 files changed

+134
-14
lines changed

src/main/java/world/landfall/infection/ModInfections.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import world.landfall.infection.api.Infection;
1111
import world.landfall.infection.api.InfectionRegistry;
1212
import world.landfall.infection.api.InfectionStage;
13+
import world.landfall.infection.infections.CommonColdInfection;
1314

1415
import java.util.Collection;
1516
import java.util.List;
@@ -20,8 +21,8 @@ public class ModInfections {
2021
private static final DeferredRegister<InfectionStage> STAGES = DeferredRegister.create(InfectionRegistry.STAGE_REGISTRY, InfectionMod.MODID);
2122
public static final DeferredHolder<InfectionStage,InfectionStage> NONE_STAGE = STAGES.register("none", () -> new InfectionStage() {
2223
@Override
23-
public Supplier<? extends InfectionStage> nextStage() {
24-
return NONE_STAGE;
24+
public ResourceLocation nextStage() {
25+
return NONE_STAGE.getId();
2526
}
2627

2728
@Override
@@ -51,15 +52,17 @@ public ResourceLocation location() {
5152
}
5253

5354
@Override
54-
public DeferredHolder<InfectionStage,InfectionStage> initialStage() {
55-
return NONE_STAGE;
55+
public ResourceLocation initialStage() {
56+
return NONE_STAGE.getId();
5657
}
5758

5859
@Override
5960
public Collection<ResourceLocation> validStages() {
6061
return List.of(InfectionMod.path("none"));
6162
}
6263
});
64+
public static final DeferredHolder<InfectionStage, InfectionStage> COMMON_COLD_INITIAL_STAGE = STAGES.register("common_cold_initial", CommonColdInfection.CommonColdStageOne::new);
65+
public static final DeferredHolder<Infection, Infection> COMMON_COLD_INFECTION = INFECTIONS.register("common_cold", CommonColdInfection::new);
6366
public static void register(IEventBus eventBus) {
6467
STAGES.register(eventBus);
6568
INFECTIONS.register(eventBus);

src/main/java/world/landfall/infection/api/Infection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.neoforged.neoforge.event.tick.PlayerTickEvent;
88
import net.neoforged.neoforge.registries.DeferredHolder;
99
import world.landfall.infection.InfectionMod;
10+
import world.landfall.infection.ModInfections;
1011

1112
import java.util.Collection;
1213
import java.util.List;
@@ -22,10 +23,10 @@ public void tick(Player player) {
2223
return;
2324
currentStage.tick(player);
2425
if (currentStage.getTimeExisted() >= currentStage.lengthInTicks() && currentStage.lengthInTicks() >= 0)
25-
currentStage = currentStage.nextStage().get();
26+
currentStage = ModInfections.NONE_STAGE.get();
2627
}
2728
public void activate() {
28-
currentStage = initialStage().get();
29+
currentStage = InfectionRegistry.STAGE_REGISTRY.get(initialStage());
2930
}
3031
public boolean isActive() {
3132
return currentStage != null;
@@ -34,6 +35,6 @@ public InfectionStage getCurrentStage() {
3435
return currentStage;
3536
}
3637
public abstract ResourceLocation location();
37-
public abstract DeferredHolder<InfectionStage,InfectionStage> initialStage();
38+
public abstract ResourceLocation initialStage();
3839
public abstract Collection<ResourceLocation> validStages();
3940
}

src/main/java/world/landfall/infection/api/InfectionRegistry.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ private static void onPlayerPostTick(PlayerTickEvent.Post event) {
4343
throw new IllegalStateException("Error ticking infection "+infection.location().getPath()+" ! Infection is not registered.");
4444
}
4545
if (!infection.isActive()) {
46-
infection.activate(); // TODO: Support infections that "gestate" for a period
4746
return;
4847
}
48+
if (infection.getCurrentStage().location() == null) {
49+
LOGGER.error("Error ticking infection {} ! Current stage has no location.", infection.location().getPath());
50+
}
4951
if (infection.validStages().stream().noneMatch(infection.getCurrentStage().location()::equals)) {
50-
LOGGER.error("Error ticking infection {} ! Current stage is not valid.", infection.location().getPath());
52+
System.out.println(infection.validStages());
53+
System.out.println(infection.getCurrentStage().location());
54+
//LOGGER.error("Error ticking infection {} ! Current stage is not valid.", infection.location().getPath());
55+
player.setData(ModAttachments.ACTIVE_INFECTION, ModInfections.NONE_INFECTION.get());
5156
return;
5257
}
5358
infection.tick(player);

src/main/java/world/landfall/infection/api/InfectionStage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public abstract class InfectionStage {
1515
private int timeExisted = 0;
16-
public abstract Supplier<? extends InfectionStage> nextStage();
16+
public abstract ResourceLocation nextStage();
1717
public abstract int stageNumber();
1818
public abstract int lengthInTicks(); // make this negative if it does not advance to another stage
1919
public abstract Collection<Holder<MobEffect>> currentEffects();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package world.landfall.infection.command;
2+
3+
import com.mojang.brigadier.arguments.StringArgumentType;
4+
import com.mojang.brigadier.arguments.ArgumentType;
5+
import com.mojang.brigadier.builder.ArgumentBuilder;
6+
import com.mojang.brigadier.context.CommandContext;
7+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8+
import net.minecraft.commands.CommandSourceStack;
9+
import net.minecraft.commands.Commands;
10+
import net.minecraft.commands.arguments.EntityArgument;
11+
import net.minecraft.commands.arguments.ResourceArgument;
12+
import net.minecraft.commands.arguments.selector.EntitySelector;
13+
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
14+
import net.minecraft.commands.synchronization.ArgumentUtils;
15+
import net.minecraft.core.Holder;
16+
import net.minecraft.network.chat.Component;
17+
import net.minecraft.resources.ResourceLocation;
18+
import net.minecraft.world.entity.Entity;
19+
import net.neoforged.bus.api.SubscribeEvent;
20+
import net.neoforged.fml.common.EventBusSubscriber;
21+
import net.neoforged.neoforge.event.RegisterCommandsEvent;
22+
import world.landfall.infection.InfectionMod;
23+
import world.landfall.infection.ModAttachments;
24+
import world.landfall.infection.ModInfections;
25+
import world.landfall.infection.api.Infection;
26+
import world.landfall.infection.api.InfectionRegistry;
27+
28+
@EventBusSubscriber(modid = InfectionMod.MODID)
29+
public class InfectionCommand {
30+
@SubscribeEvent
31+
public static void registerCommands(RegisterCommandsEvent event) {
32+
var dispatcher = event.getDispatcher();
33+
dispatcher.register(Commands.literal("infection")
34+
.requires(source -> source.hasPermission(2))
35+
.then(Commands.literal("infect")
36+
.then(Commands.argument("player", EntityArgument.player())
37+
.then(Commands.argument("infection", ResourceArgument.resource(event.getBuildContext(), InfectionRegistry.INFECTION_REGISTRY_KEY))
38+
.executes(InfectionCommand::infect)
39+
)
40+
41+
)
42+
).then(Commands.literal("get")
43+
.then(Commands.argument("player", EntityArgument.player())
44+
.executes(InfectionCommand::get)
45+
)
46+
)
47+
);
48+
}
49+
50+
private static int get(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
51+
var player = ctx.getArgument("player", EntitySelector.class).findSinglePlayer(ctx.getSource());
52+
if (!player.hasData(ModAttachments.ACTIVE_INFECTION)) {
53+
ctx.getSource().sendSystemMessage(Component.translatable("command.infection.get.fail"));
54+
return -1;
55+
}
56+
var infection = player.getData(ModAttachments.ACTIVE_INFECTION);
57+
ctx.getSource().sendSystemMessage(Component.translatable("command.infection.get.success",infection.location().toString()));
58+
return 1;
59+
}
60+
61+
private static int infect(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
62+
var player = ctx.getArgument("player", EntitySelector.class).findSinglePlayer(ctx.getSource());
63+
Holder.Reference<Infection> infectionReference = ctx.getArgument("infection", Holder.Reference.class);
64+
var infection = InfectionRegistry.INFECTION_REGISTRY.get(infectionReference.key());
65+
if (infection == null)
66+
return -1;
67+
player.setData(ModAttachments.ACTIVE_INFECTION, infection);
68+
infection.activate();
69+
ctx.getSource().sendSystemMessage(Component.translatable("command.infection.infect.success",infection.location().toString(), player.getName()));
70+
return 1;
71+
}
72+
73+
}

src/main/java/world/landfall/infection/infections/CommonColdInfection.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package world.landfall.infection.infections;
22

3+
import net.minecraft.core.Holder;
34
import net.minecraft.resources.ResourceLocation;
5+
import net.minecraft.world.effect.MobEffect;
6+
import net.minecraft.world.effect.MobEffectInstance;
7+
import net.minecraft.world.effect.MobEffects;
48
import net.neoforged.neoforge.registries.DeferredHolder;
59
import world.landfall.infection.InfectionMod;
10+
import world.landfall.infection.ModInfections;
611
import world.landfall.infection.api.Infection;
12+
import world.landfall.infection.api.InfectionRegistry;
713
import world.landfall.infection.api.InfectionStage;
814

915
import java.util.Collection;
@@ -18,12 +24,41 @@ public ResourceLocation location() {
1824
}
1925

2026
@Override
21-
public DeferredHolder<InfectionStage, InfectionStage> initialStage() {
22-
return null;
27+
public ResourceLocation initialStage() {
28+
return ResourceLocation.parse("infection:common_cold_initial");
2329
}
2430

2531
@Override
2632
public Collection<ResourceLocation> validStages() {
27-
return List.of();
33+
return List.of(ModInfections.COMMON_COLD_INITIAL_STAGE.getId());
34+
}
35+
public static class CommonColdStageOne extends InfectionStage {
36+
37+
@Override
38+
public ResourceLocation nextStage() {
39+
return ModInfections.NONE_STAGE.getId();
40+
}
41+
42+
@Override
43+
public int stageNumber() {
44+
return 0;
45+
}
46+
47+
@Override
48+
public int lengthInTicks() {
49+
return 200;
50+
}
51+
52+
@Override
53+
public Collection<Holder<MobEffect>> currentEffects() {
54+
return List.of(
55+
MobEffects.WEAKNESS
56+
);
57+
}
58+
59+
@Override
60+
public ResourceLocation location() {
61+
return InfectionMod.path("common_cold_initial");
62+
}
2863
}
2964
}

src/main/resources/assets/examplemod/lang/en_us.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
"examplemod.configuration.items": "Item List",
1010
"examplemod.configuration.logDirtBlock": "Log Dirt Block",
1111
"examplemod.configuration.magicNumberIntroduction": "Magic Number Text",
12-
"examplemod.configuration.magicNumber": "Magic Number"
12+
"examplemod.configuration.magicNumber": "Magic Number",
13+
"command.infection.infect.success": "Applied %s to player %s.",
14+
"command.infection.get.fail": "Player does not have infection component.",
15+
"command.infection.get.success": "Player has infection %s."
1316
}

0 commit comments

Comments
 (0)