-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+Implemented chunk-based xp limiting: mob farms have been a real issue for PlayerEx. However, limiting xp from mob farms whilst allowing xp from dungeons was complicated to implement. This is a new feature that aims to solve this problem. It works by attaching a factor to each chunk, which sits at 1.0. This factor is the chance for xp to spawn in the chunk (so 1.0 means guaranteed). Every time xp is created in the chunk, the chunk's factor decreases, therefore decreasing the chance for more xp to spawn. Meanwhile, the chunk has a restorative force, acting to bring the factor back to 1.0 over time. All of these values are configurable and this whole system can be disabled. More documentation to come.
- Loading branch information
1 parent
0b58c49
commit d13c190
Showing
13 changed files
with
192 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/github/clevernucleus/playerex/api/ExperienceData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.github.clevernucleus.playerex.api; | ||
|
||
import dev.onyxstudios.cca.api.v3.component.tick.ServerTickingComponent; | ||
|
||
public interface ExperienceData extends ServerTickingComponent { | ||
boolean updateExperienceNegationFactor(final int amount); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/github/clevernucleus/playerex/impl/ExperienceDataContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.clevernucleus.playerex.impl; | ||
|
||
import com.github.clevernucleus.playerex.api.ExAPI; | ||
|
||
import dev.onyxstudios.cca.api.v3.chunk.ChunkComponentFactoryRegistry; | ||
import dev.onyxstudios.cca.api.v3.chunk.ChunkComponentInitializer; | ||
|
||
public final class ExperienceDataContainer implements ChunkComponentInitializer { | ||
|
||
@Override | ||
public void registerChunkComponentFactories(ChunkComponentFactoryRegistry registry) { | ||
registry.register(ExAPI.EXPERIENCE_DATA, ExperienceDataManager.class, ExperienceDataManager::new); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/github/clevernucleus/playerex/impl/ExperienceDataManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.github.clevernucleus.playerex.impl; | ||
|
||
import java.util.Random; | ||
|
||
import com.github.clevernucleus.playerex.api.ExAPI; | ||
import com.github.clevernucleus.playerex.api.ExConfig; | ||
import com.github.clevernucleus.playerex.api.ExperienceData; | ||
|
||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.world.chunk.Chunk; | ||
|
||
public final class ExperienceDataManager implements ExperienceData { | ||
private static final String KEY_EXP_NEGATION_CHANCE = "ExpNegationFactor"; | ||
private static final Random RANDOM = new Random(); | ||
private final Chunk chunk; | ||
private float expNegationFactor; | ||
private int ticks; | ||
private final int restorativeForceTicks; | ||
private final float restorativeForce; | ||
private final float expNegationMultiplier; | ||
|
||
public ExperienceDataManager(final Chunk chunk) { | ||
this.chunk = chunk; | ||
this.expNegationFactor = 1.0F; | ||
this.ticks = 0; | ||
|
||
ExConfig config = ExAPI.getConfig(); | ||
this.restorativeForceTicks = config.restorativeForceTicks(); | ||
this.restorativeForce = config.restorativeForceMultiplier(); | ||
this.expNegationMultiplier = config.expNegationFactor(); | ||
} | ||
|
||
@Override | ||
public boolean updateExperienceNegationFactor(final int amount) { | ||
if(RANDOM.nextFloat() > this.expNegationFactor) return true; | ||
float dynamicMultiplier = this.expNegationMultiplier + ((1.0F - this.expNegationMultiplier) * (1.0F - (0.1F * (float)amount))); | ||
this.expNegationFactor = Math.max(this.expNegationFactor * dynamicMultiplier, 0.0F); | ||
this.chunk.setNeedsSaving(true); | ||
return false; | ||
} | ||
|
||
@Override | ||
public void serverTick() { | ||
if(this.expNegationFactor == 1.0F) return; | ||
if(this.ticks < this.restorativeForceTicks) { | ||
this.ticks++; | ||
} else { | ||
this.ticks = 0; | ||
this.expNegationFactor = Math.min(this.expNegationFactor * this.restorativeForce, 1.0F); | ||
this.chunk.setNeedsSaving(true); | ||
} | ||
} | ||
|
||
@Override | ||
public void readFromNbt(NbtCompound tag) { | ||
this.expNegationFactor = tag.getFloat(KEY_EXP_NEGATION_CHANCE); | ||
} | ||
|
||
@Override | ||
public void writeToNbt(NbtCompound tag) { | ||
tag.putFloat(KEY_EXP_NEGATION_CHANCE, this.expNegationFactor); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/github/clevernucleus/playerex/mixin/ExperienceOrbEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.clevernucleus.playerex.mixin; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import com.github.clevernucleus.playerex.api.ExAPI; | ||
|
||
import net.minecraft.entity.Entity.RemovalReason; | ||
import net.minecraft.entity.ExperienceOrbEntity; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
import net.minecraft.world.chunk.Chunk; | ||
|
||
@Mixin(ExperienceOrbEntity.class) | ||
abstract class ExperienceOrbEntityMixin { | ||
|
||
@Inject(method = "<init>", at = @At("TAIL")) | ||
public void playerex_init(World world, double x, double y, double z, int amount, CallbackInfo ci) { | ||
BlockPos pos = new BlockPos(x, y, z); | ||
Chunk chunk = world.getChunk(pos); | ||
|
||
ExAPI.EXPERIENCE_DATA.maybeGet(chunk).ifPresent(data -> { | ||
if(data.updateExperienceNegationFactor(amount)) { | ||
((ExperienceOrbEntity)(Object)this).remove(RemovalReason.DISCARDED); | ||
} | ||
}); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/github/clevernucleus/playerex/mixin/ServerWorldMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.clevernucleus.playerex.mixin; | ||
|
||
import org.slf4j.Logger; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
import net.minecraft.server.world.ServerWorld; | ||
|
||
@Mixin(ServerWorld.class) | ||
abstract class ServerWorldMixin { | ||
|
||
@Redirect(method = "addEntity", at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;warn(Ljava/lang/String;Ljava/lang/Object;)V")) | ||
private void playerex_addEntity(Logger logger, String arg0, Object arg1) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters