Skip to content

Commit

Permalink
Merge difficulty id fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron1011 committed Sep 4, 2016
2 parents 457030b + a3303c8 commit 6cbead7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.text.translation.SpongeTranslation;

@NonnullByDefault
Expand All @@ -39,17 +42,24 @@ public class MixinEnumDifficulty implements Difficulty {

@Shadow @Final private int difficultyId;
@Shadow @Final private String difficultyResourceKey;

private String id;

private Translation translation;

@Inject(method = "<init>", at = @At(value = "RETURN"))
public void onConstruction(CallbackInfo callbackInfo) {
this.id = this.difficultyResourceKey.replace("options.difficulty.", "minecraft:");
}

@Override
public String getId() {
return this.difficultyResourceKey;
return this.id;
}

@Override
public String getName() {
return this.difficultyResourceKey.replace("options.difficulty.", "");
return this.id;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import com.google.common.collect.ImmutableList;
import net.minecraft.world.EnumDifficulty;
import org.spongepowered.api.registry.CatalogRegistryModule;
import org.spongepowered.api.registry.AlternateCatalogRegistryModule;
import org.spongepowered.api.registry.util.AdditionalRegistration;
import org.spongepowered.api.registry.util.RegisterCatalog;
import org.spongepowered.api.world.difficulty.Difficulties;
Expand All @@ -40,13 +40,17 @@
import java.util.Map;
import java.util.Optional;

public final class DifficultyRegistryModule implements CatalogRegistryModule<Difficulty> {
public final class DifficultyRegistryModule implements AlternateCatalogRegistryModule<Difficulty> {

@RegisterCatalog(Difficulties.class)
private final Map<String, Difficulty> difficultyMappings = new HashMap<>();

@Override
public Optional<Difficulty> getById(String id) {
checkNotNull(id);
if (!id.contains(":") && !id.equals("none")) {
id = "minecraft:" + id; // assume vanilla
}
return Optional.ofNullable(this.difficultyMappings.get(checkNotNull(id).toLowerCase(Locale.ENGLISH)));
}

Expand All @@ -55,12 +59,21 @@ public Collection<Difficulty> getAll() {
return ImmutableList.copyOf(this.difficultyMappings.values());
}

@Override
public Map<String, Difficulty> provideCatalogMap() {
Map<String, Difficulty> newMap = new HashMap<>();
for (Map.Entry<String, Difficulty> entry : this.difficultyMappings.entrySet()) {
newMap.put(entry.getKey().replace("minecraft:", ""), entry.getValue());
}
return newMap;
}

@Override
public void registerDefaults() {
this.difficultyMappings.put("peaceful", (Difficulty) (Object) EnumDifficulty.PEACEFUL);
this.difficultyMappings.put("easy", (Difficulty) (Object) EnumDifficulty.EASY);
this.difficultyMappings.put("normal", (Difficulty) (Object) EnumDifficulty.NORMAL);
this.difficultyMappings.put("hard", (Difficulty) (Object) EnumDifficulty.HARD);
this.difficultyMappings.put("minecraft:peaceful", (Difficulty) (Object) EnumDifficulty.PEACEFUL);
this.difficultyMappings.put("minecraft:easy", (Difficulty) (Object) EnumDifficulty.EASY);
this.difficultyMappings.put("minecraft:normal", (Difficulty) (Object) EnumDifficulty.NORMAL);
this.difficultyMappings.put("minecraft:hard", (Difficulty) (Object) EnumDifficulty.HARD);
}

@AdditionalRegistration
Expand Down

0 comments on commit 6cbead7

Please sign in to comment.