Skip to content

Commit

Permalink
Allowed simple origin registration to use Varargs. Register spawn ori…
Browse files Browse the repository at this point in the history
…gins to vault. Added helper method injectDefaultTag and getFinalDifficulty to Origin class.
  • Loading branch information
arazadaz committed Feb 8, 2024
1 parent 2032749 commit 34522e1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/arazadaz/dd/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.arazadaz.dd;

import com.arazadaz.dd.api.origins.OriginID;
import com.arazadaz.dd.api.origins.OriginManager;
import com.arazadaz.dd.config.Config;
import com.arazadaz.dd.core.DDVault;
import com.mojang.logging.LogUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.storage.LevelData;
import net.minecraft.world.phys.Vec3;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
Expand Down Expand Up @@ -65,6 +70,16 @@ private void commonSetup(final FMLCommonSetupEvent event)
@SubscribeEvent
public void onServerStarting(ServerStartingEvent event)
{
LevelData data = event.getServer().getLevel(Level.OVERWORLD).getLevel().getLevelData();

Vec3 spawnVec = new Vec3(data.getXSpawn(), data.getYSpawn(), data.getZSpawn());

if(Config.useSpawnOrigin) {
OriginID spawnOrigin = OriginManager.registerOrigin(spawnVec, "spawn");
vault.userOrigins.add(spawnOrigin);
}


}

// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static double getWorldSpawnDifficultyHere(Vec3 pos, Level level, Difficul
//Primary getter
public static double getOriginDifficultyHere(Vec3 pos, Level level, DifficultyType type, RadiusMode rMode, String originTag, Optional<LivingEntity> entity){ //Calculates from nearest origin point of specific type

String levelID = level.toString(); //Will have to debug this to see what value is given.
String levelID = level.dimension().location().getPath();

Origin origin = OriginManager.getNearestOrigin(levelID, originTag, pos);

Expand Down
41 changes: 22 additions & 19 deletions src/main/java/com/arazadaz/dd/api/origins/Origin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,9 @@ public Origin(Vec3 pos, String[] formulas, String[] tags, double range, boolean

//Add default type to origin or none if desired
if(defaultTag == 1){ //Default tag

newTags = new String[tags.length+1];
for(int i = 1; i<=tags.length; i++){
newTags[i] = tags[i];
}

newTags[0] = "default";

newTags = injectDefaultTag(tags, "default");
}else if(defaultTag == 2){ //Dynamic tag

newTags = new String[tags.length+1];
for(int i = 1; i<=tags.length; i++){
newTags[i] = tags[i];
}

newTags[0] = "dynamic"; //Dynamic because these are generally going to be used for generated origin points after the world was loaded.

newTags = injectDefaultTag(tags, "dynamic"); //Dynamic because these are generally going to be used for generated origin points after the world was loaded.
}else{ //No default tag
newTags = tags; //Keep original tags, exists to prevent conflicts and in case a mod dev wants to specifically design how their origin point interacts with the game.
}
Expand All @@ -53,6 +39,18 @@ public Origin(Vec3 pos, String[] formulas, String[] tags, double range, boolean

}

private String[] injectDefaultTag(String[] originalTags, String tag){

String[] newTags = new String[originalTags.length+1];
for(int i = 1; i<=originalTags.length; i++){
newTags[i] = originalTags[i-1];
}

newTags[0] = tag; //On top since it'll be checked most probably

return newTags;
}


//Bulk of logic goes here
public double getDifficultyHere(Vec3 pos, DifficultyType type, RadiusMode rMode, DDContext context){
Expand All @@ -69,24 +67,29 @@ public double getDifficultyHere(Vec3 pos, DifficultyType type, RadiusMode rMode,

case CIRCLE -> {
difficulty = runModifiers(modifierIterator, context, 0);
return difficulty;
return getFinalDifficulty(difficulty);
}

case SQUARE -> {
difficulty = runModifiers(modifierIterator, context, 1);
return difficulty;
return getFinalDifficulty(difficulty);
}

case CUSTOM -> {
difficulty = runModifiers(modifierIterator, context, 2);
return difficulty;
return getFinalDifficulty(difficulty);
}

default -> {return 0;} //Should never occur
}

}

private double getFinalDifficulty(double finalDifficulty){
finalDifficulty = noCalculationBound ? finalDifficulty : Math.min(1, finalDifficulty);
return finalDifficulty;
}

private double runModifiers(Iterator<DifficultyModifier> modifierIterator, DDContext context, int formula){ //Formula can be 0, 1, or 2 with those values representing circle, square, and custom respectively.
double base = new FormulaInterpreter().run(Config.formulas[formula], this, pos);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/arazadaz/dd/api/origins/OriginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

public class OriginManager {

private static HashMap<String, ArrayList<Origin>> originMap; //Mapping of origins to each world/all;
private static HashMap<String, ArrayList<Origin>> originMap = new HashMap<>(); //Mapping of origins to each world/all;



public static OriginID registerOrigin(Vec3 pos, String[] tags){ //basic, less control
public static OriginID registerOrigin(Vec3 pos, String... tags){ //basic, less control

double range = 0; //Will get global value from config after it's setup
String[] formulas = null; //will get global formulas from config after it's setup
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/arazadaz/dd/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void mapOrigins(String originSpec){
case 1 -> {} //Do nothing
case 3 -> {
Vec3 srcPos = new Vec3(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
OriginID userOrigin = OriginManager.registerOrigin(srcPos, new String[]{"userOrigin"});
OriginID userOrigin = OriginManager.registerOrigin(srcPos, "userOrigin");

Main.vault.userOrigins.add(userOrigin);
}
Expand Down

0 comments on commit 34522e1

Please sign in to comment.