Skip to content

Commit b9b2567

Browse files
Update semester information, disable non-overworld dimensions, and optimize and clean code
1 parent 8e114ee commit b9b2567

15 files changed

+148
-148
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.*
2+
bin
23
gradle
34
run
45
gradlew

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ This is the source code for the CMSC 389E Circuitry mod, created for UMD class C
44

55
However, having this code available is not to be used to cheat or take advantage of. It is only here for academic purposes, debugging, and a means to effectively collaborate on this project.
66

7-
More information on the class and what you are expected to do can be found [here](https://cs.umd.edu/class/fall2020/cmsc389E).
7+
More information on the class and what you are expected to do can be found [here](https://cs.umd.edu/class/spring2021/cmsc389E).

build.gradle

+23-25
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,65 @@
11
buildscript {
2+
dependencies {
3+
classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.4'
4+
classpath 'net.minecraftforge.gradle:ForgeGradle:+'
5+
}
6+
27
repositories {
38
maven {
49
url 'https://files.minecraftforge.net/maven'
510
}
6-
11+
712
jcenter()
813
}
9-
10-
dependencies {
11-
classpath 'net.minecraftforge.gradle:ForgeGradle:+'
12-
}
1314
}
1415

15-
plugins {
16-
id 'com.github.johnrengelman.shadow' version '4.0.4'
17-
}
16+
apply plugin: 'com.github.johnrengelman.shadow'
1817
apply plugin: 'net.minecraftforge.gradle'
1918

2019
archivesBaseName = 'circuitry'
21-
group = 'cmsc389e.circuitry'
22-
version = '1.15.2-1.0.0.0'
20+
group = 'cmsc389e.' + archivesBaseName
21+
version = '1.15.2-1.1.0.0'
2322

2423
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'
2524
compileJava.options.compilerArgs << '-proc:none'
2625

2726
minecraft {
28-
mappings channel: 'snapshot', version: '20200514-1.15.1'
29-
3027
runs {
3128
client {
32-
workingDirectory project.file('run')
33-
3429
mods {
3530
circuitry.source sourceSets.main
3631
}
32+
33+
workingDirectory project.file('run')
3734
}
38-
35+
3936
server {
40-
workingDirectory project.file('run')
41-
4237
mods {
4338
circuitry.source sourceSets.main
4439
}
40+
41+
workingDirectory project.file('run')
4542
}
4643
}
47-
}
4844

49-
dependencies {
50-
minecraft 'net.minecraftforge:forge:1.15.2-31.2.0'
51-
compile 'org.apache.httpcomponents:httpmime:4.3.3'
45+
mappings channel: 'snapshot', version: '20200514-1.15.1'
5246
}
5347

5448
reobf {
55-
shadowJar {
56-
}
49+
shadowJar {}
50+
}
51+
52+
dependencies {
53+
compile 'org.apache.httpcomponents:httpmime:4.3.3'
54+
minecraft 'net.minecraftforge:forge:1.15.2-31.2.0'
5755
}
5856

5957
shadowJar {
6058
dependencies {
6159
include(dependency(':httpmime'))
6260
}
63-
64-
classifier = ''
61+
62+
classifier = ''
6563
manifest.attributes 'Implementation-Version': "${version}"
6664
minimize()
6765
relocate 'org.apache.http.entity.mime', project.group + '.org.apache.http.entity.mime'

src/main/java/cmsc389e/circuitry/Circuitry.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@
2727
import net.minecraftforge.registries.DeferredRegister;
2828
import net.minecraftforge.registries.ForgeRegistries;
2929

30-
@Mod(Circuitry.MODID)
3130
@EventBusSubscriber(bus = Bus.MOD)
31+
@Mod(Circuitry.MODID)
3232
public class Circuitry {
3333
public static final String MODID = "circuitry";
3434

3535
private static final DeferredRegister<Block> BLOCKS = new DeferredRegister<>(ForgeRegistries.BLOCKS, MODID);
36-
private static final DeferredRegister<TileEntityType<?>> TES = new DeferredRegister<>(ForgeRegistries.TILE_ENTITIES,
37-
MODID);
36+
private static final DeferredRegister<TileEntityType<?>> TYPES = new DeferredRegister<>(
37+
ForgeRegistries.TILE_ENTITIES, MODID);
3838

3939
public static final RegistryObject<Block> IN_NODE = BLOCKS.register("in_node", InNodeBlock::new),
4040
OUT_NODE = BLOCKS.register("out_node", OutNodeBlock::new);
41-
public static final RegistryObject<TileEntityType<?>> NODE = TES.register("node",
41+
public static final RegistryObject<TileEntityType<?>> NODE = TYPES.register("node",
4242
() -> Builder.create(NodeTileEntity::new, IN_NODE.get(), OUT_NODE.get()).build(null));
4343

44-
@SuppressWarnings("resource")
4544
@SubscribeEvent
45+
@SuppressWarnings("resource")
4646
public static void onClientSetup(FMLClientSetupEvent event) {
4747
ObfuscationReflectionHelper.setPrivateValue(NewChatGui.class,
4848
event.getMinecraftSupplier().get().ingameGUI.getChatGUI(), new ArrayList<ChatLine>() {
@@ -65,12 +65,12 @@ public int size() {
6565

6666
public Circuitry() {
6767
DeferredRegister<Item> items = new DeferredRegister<>(ForgeRegistries.ITEMS, MODID);
68-
BLOCKS.getEntries().forEach(block -> items.register(block.getId().getPath(),
68+
BLOCKS.getEntries().parallelStream().forEach(block -> items.register(block.getId().getPath(),
6969
() -> new BlockItem(block.get(), new Properties().group(ItemGroup.REDSTONE))));
7070

7171
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
7272
BLOCKS.register(bus);
73-
TES.register(bus);
73+
TYPES.register(bus);
7474
items.register(bus);
7575

7676
Config.register();

src/main/java/cmsc389e/circuitry/client/EventHandler.java

+22-22
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import net.minecraft.world.gen.FlatGenerationSettings;
2929
import net.minecraft.world.storage.WorldInfo;
3030
import net.minecraftforge.api.distmarker.Dist;
31-
import net.minecraftforge.client.event.DrawHighlightEvent.HighlightBlock;
32-
import net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent;
31+
import net.minecraftforge.client.event.DrawHighlightEvent;
32+
import net.minecraftforge.client.event.GuiScreenEvent;
3333
import net.minecraftforge.eventbus.api.SubscribeEvent;
3434
import net.minecraftforge.fml.ModContainer;
3535
import net.minecraftforge.fml.ModList;
@@ -47,36 +47,36 @@ public class EventHandler {
4747
private final static Minecraft MINECRAFT = Minecraft.getInstance();
4848
private final static Field WORLD_SEED = ObfuscationReflectionHelper.findField(CreateWorldScreen.class,
4949
"field_146329_I"); // worldSeed
50-
private final static WorldSettings WORLD_SETTINGS = new WorldSettings(0, GameType.CREATIVE, false, false,
51-
WorldType.FLAT)
52-
.enableCommands()
53-
.setGeneratorOptions(FlatGenerationSettings
54-
.createFlatGeneratorFromString(
55-
"minecraft:bedrock,3*minecraft:stone,52*minecraft:sandstone;minecraft:desert")
56-
.func_210834_a(JsonOps.INSTANCE).getValue());
50+
private final static WorldSettings SETTINGS = new WorldSettings(0, GameType.CREATIVE, false, false, WorldType.FLAT)
51+
.enableCommands()
52+
.setGeneratorOptions(FlatGenerationSettings
53+
.createFlatGeneratorFromString(
54+
"minecraft:bedrock,3*minecraft:stone,52*minecraft:sandstone;minecraft:desert")
55+
.func_210834_a(JsonOps.INSTANCE).getValue());
5756

58-
private static void alert(String line1, String line2, String line3, String button, Consumer<OS> consumer) {
57+
private static void alert(String msg1, String msg2, String msg3, String button, Consumer<OS> consumer) {
5958
MINECRAFT
6059
.displayGuiScreen(new AlertScreen(() -> consumer.accept(Util.getOSType()),
61-
new StringTextComponent(line1).setStyle(new Style().setColor(TextFormatting.RED)),
62-
new StringTextComponent(line2 + "\n\n").appendSibling(
63-
new StringTextComponent(line3).setStyle(new Style().setColor(TextFormatting.AQUA))),
60+
new StringTextComponent(msg1).setStyle(new Style().setColor(TextFormatting.RED)),
61+
new StringTextComponent(msg2 + "\n\n").appendSibling(
62+
new StringTextComponent(msg3).setStyle(new Style().setColor(TextFormatting.AQUA))),
6463
button));
6564
}
6665

6766
@SubscribeEvent
68-
public static void onHighlightBlock(HighlightBlock event) {
69-
TileEntity te = MINECRAFT.world.getTileEntity(event.getTarget().getPos());
67+
public static void onDrawHighlightBlock(DrawHighlightEvent.HighlightBlock event) {
68+
TileEntity entity = MINECRAFT.world.getTileEntity(event.getTarget().getPos());
7069
MINECRAFT.ingameGUI.setOverlayMessage(
71-
te != null && te.getType() == Circuitry.NODE.get() ? ((NodeTileEntity) te).getTag() : "", false);
70+
entity != null && entity.getType() == Circuitry.NODE.get() ? ((NodeTileEntity) entity).getTag() : "",
71+
false);
7272
}
7373

7474
@SubscribeEvent
75-
public static void onPostInitGui(InitGuiEvent.Post event) throws IllegalAccessException {
75+
public static void onGuiScreenInitPost(GuiScreenEvent.InitGuiEvent.Post event) throws IllegalAccessException {
7676
Screen screen = event.getGui();
7777
if (screen instanceof CreateWorldScreen && WORLD_SEED.get(screen).equals(""))
7878
((CreateWorldScreen) screen)
79-
.recreateFromExistingWorld(new WorldInfo(WORLD_SETTINGS, I18n.format("selectWorld.newWorld")));
79+
.recreateFromExistingWorld(new WorldInfo(SETTINGS, I18n.format("selectWorld.newWorld")));
8080
else if (event.getGui() instanceof MainMenuScreen) {
8181
ModList list = ModList.get();
8282

@@ -88,11 +88,11 @@ else if (event.getGui() instanceof MainMenuScreen) {
8888
Arrays.toString(mods), "fml.button.open.mods.folder",
8989
os -> os.openFile(FMLPaths.MODSDIR.get().toFile()));
9090

91-
IModInfo info = list.getModContainerById(Circuitry.MODID).get().getModInfo();
92-
if (!info.getVersion().getQualifier().equals("NONE")) {
93-
CheckResult result = VersionChecker.getResult(info);
91+
IModInfo mod = list.getModContainerById(Circuitry.MODID).get().getModInfo();
92+
if (!mod.getVersion().getQualifier().equals("NONE")) {
93+
CheckResult result = VersionChecker.getResult(mod);
9494
if (result.status == Status.OUTDATED)
95-
alert(info.getDisplayName() + " is out of date.",
95+
alert(mod.getDisplayName() + " is out of date.",
9696
"You must update to version " + result.target
9797
+ " before proceeding.\nOpen Link will bring you to the below page:",
9898
result.url, "Open Link", os -> os.openURI(result.url));

src/main/java/cmsc389e/circuitry/common/Config.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.InputStream;
55
import java.net.URL;
66
import java.nio.charset.Charset;
7+
import java.util.Arrays;
78
import java.util.List;
89

910
import org.apache.commons.io.IOUtils;
@@ -19,30 +20,26 @@ public class Config {
1920

2021
public static boolean loaded;
2122
public static String[] inTags, outTags;
22-
public static Boolean[][] inTests, outTests;
23+
public static String[][] inTests, outTests;
2324

2425
public static void load() throws IOException {
25-
loaded = false;
2626
try (InputStream in = new URL("https://cs.umd.edu/~abrassel/proj" + projectNumber.get() + "tests.txt")
2727
.openStream()) {
2828
List<String> lines = IOUtils.readLines(in, (Charset) null);
2929
String[] tags = lines.get(1).split("\t(?=o)", 2);
30+
31+
loaded = false;
3032
inTags = tags[0].split("\t");
3133
outTags = tags[1].split("\t");
32-
inTests = new Boolean[lines.size() - 2][inTags.length];
33-
outTests = new Boolean[inTests.length][outTags.length];
34+
inTests = new String[lines.size() - 2][];
35+
outTests = new String[inTests.length][];
3436
for (int i = 0; i < inTests.length; i++) {
3537
tags = lines.get(i + 2).split("\t");
36-
for (int j = 0; j < tags.length; j++) {
37-
Boolean value = tags[j].equals("1");
38-
if (j < inTags.length)
39-
inTests[i][j] = value;
40-
else
41-
outTests[i][j - inTags.length] = value;
42-
}
38+
inTests[i] = Arrays.copyOf(tags, inTags.length);
39+
outTests[i] = Arrays.copyOfRange(tags, inTags.length, tags.length);
4340
}
41+
loaded = true;
4442
}
45-
loaded = true;
4643
}
4744

4845
public static void register() {

src/main/java/cmsc389e/circuitry/common/EventHandler.java

+21-14
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,19 @@
1010
import net.minecraft.server.MinecraftServer;
1111
import net.minecraft.world.Difficulty;
1212
import net.minecraft.world.GameRules;
13+
import net.minecraft.world.dimension.DimensionType;
1314
import net.minecraft.world.storage.WorldInfo;
14-
import net.minecraftforge.event.world.WorldEvent.CreateSpawnPosition;
15+
import net.minecraftforge.event.entity.EntityTravelToDimensionEvent;
16+
import net.minecraftforge.event.world.WorldEvent;
1517
import net.minecraftforge.eventbus.api.SubscribeEvent;
1618
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
1719
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
1820

1921
@EventBusSubscriber
2022
public class EventHandler {
2123
@SubscribeEvent
22-
public static void onCreateSpawnPosition(CreateSpawnPosition event) {
23-
WorldInfo info = event.getWorld().getWorldInfo();
24-
info.setDayTime(6000);
25-
info.setDifficulty(Difficulty.PEACEFUL);
26-
27-
GameRules rules = info.getGameRulesInstance();
28-
rules.get(GameRules.DO_DAYLIGHT_CYCLE).set(false, null);
29-
rules.get(GameRules.DO_MOB_SPAWNING).set(false, null);
30-
rules.get(GameRules.DO_TILE_DROPS).set(false, null);
31-
rules.get(GameRules.DO_WEATHER_CYCLE).set(false, null);
24+
public static void onEntityTravelToDimension(EntityTravelToDimensionEvent event) {
25+
event.setCanceled(true);
3226
}
3327

3428
/**
@@ -38,7 +32,8 @@ public static void onCreateSpawnPosition(CreateSpawnPosition event) {
3832
* @param event the {@link FMLServerStartingEvent}
3933
*/
4034
@SubscribeEvent
41-
public static void onServerStarting(FMLServerStartingEvent event) {
35+
@SuppressWarnings("resource")
36+
public static void onFMLServerStarting(FMLServerStartingEvent event) {
4237
CommandDispatcher<CommandSource> dispatcher = event.getCommandDispatcher();
4338
SetCommand.register(dispatcher);
4439
TestCommand.register(dispatcher);
@@ -49,8 +44,20 @@ public static void onServerStarting(FMLServerStartingEvent event) {
4944
e.printStackTrace();
5045
}
5146

52-
@SuppressWarnings("resource")
5347
MinecraftServer server = event.getServer();
54-
server.registerTickable(Tester.INSTANCE = new Tester(server));
48+
server.registerTickable(new Tester(server.getWorld(DimensionType.OVERWORLD)));
49+
}
50+
51+
@SubscribeEvent
52+
public static void onWorldCreateSpawnPosition(WorldEvent.CreateSpawnPosition event) {
53+
WorldInfo info = event.getWorld().getWorldInfo();
54+
info.setDayTime(6000);
55+
info.setDifficulty(Difficulty.PEACEFUL);
56+
57+
GameRules rules = info.getGameRulesInstance();
58+
rules.get(GameRules.DO_DAYLIGHT_CYCLE).set(false, null);
59+
rules.get(GameRules.DO_MOB_SPAWNING).set(false, null);
60+
rules.get(GameRules.DO_TILE_DROPS).set(false, null);
61+
rules.get(GameRules.DO_WEATHER_CYCLE).set(false, null);
5562
}
5663
}

src/main/java/cmsc389e/circuitry/common/NodeTileEntity.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package cmsc389e.circuitry.common;
22

3-
import java.util.function.Consumer;
4-
import java.util.stream.StreamSupport;
3+
import java.util.stream.Stream;
54

65
import cmsc389e.circuitry.Circuitry;
6+
import net.minecraft.block.Block;
77
import net.minecraft.nbt.CompoundNBT;
8-
import net.minecraft.server.MinecraftServer;
98
import net.minecraft.tileentity.TileEntity;
109
import net.minecraft.tileentity.TileEntityType;
10+
import net.minecraft.world.World;
1111

1212
public class NodeTileEntity extends TileEntity {
13-
public static void forEach(MinecraftServer server, Consumer<NodeTileEntity> action) {
13+
public static Stream<NodeTileEntity> stream(World world) {
1414
TileEntityType<?> type = Circuitry.NODE.get();
15-
StreamSupport.stream(server.getWorlds().spliterator(), true)
16-
.flatMap(world -> world.loadedTileEntityList.parallelStream()).filter(te -> te.getType() == type)
17-
.map(te -> (NodeTileEntity) te).forEach(action);
15+
return world.loadedTileEntityList.parallelStream().filter(entity -> entity.getType() == type)
16+
.map(entity -> (NodeTileEntity) entity).sequential();
1817
}
1918

2019
public int index;
@@ -23,6 +22,13 @@ public NodeTileEntity() {
2322
super(Circuitry.NODE.get());
2423
}
2524

25+
public NodeTileEntity(World world, Block block) {
26+
this();
27+
index = stream(world).filter(entity -> entity.getBlockState().getBlock() == block)
28+
.mapToInt(entity -> entity.index).sorted().reduce(-1, (left, right) -> left + 1 < right ? left : right)
29+
+ 1;
30+
}
31+
2632
public String getTag() {
2733
if (Config.loaded) {
2834
String[] tags = getBlockState().getBlock() == Circuitry.IN_NODE.get() ? Config.inTags : Config.outTags;

0 commit comments

Comments
 (0)