copyright © 2015 curt hill java for minecraft those things you should know
TRANSCRIPT
Copyright © 2015 Curt Hill
Java for Minecraft
Those things you should know
Introduction
• As you know Minecraft is written in Java
• In order to create mods for Minecraft there are parts of the language that you may not know and should
• This presentation should start to cover much of this– Well that is the hope anyway
Copyright © 2015 Curt Hill
Topics
• General• Pointers• Final• Visibility• Annotations• Class definition• Inheritance and polymorphism
Copyright © 2015 Curt Hill
General
• C++ and Java have much in common
• There is virtually no difference in:– Declarations and types– If– For– While– Do/while– Method calls
Copyright © 2015 Curt Hill
Exceptions
• Java uses boolean instead of bool• The conditional in if/for/while must
be a Boolean• The C/C++ scheme of zero is false,
everything else is true is not allowed
Copyright © 2015 Curt Hill
Visibility• C++ sets its visibility in sections• Java immediately prefixes any
declaration with the visibility:protected class helper{…}
• Any item without a visibility has “friendly” visibility– This makes it public to anything in the
same file and private to anything not in that file
• Every public class must be in a file of the same name
Copyright © 2015 Curt Hill
Preprocessor
• Java has no preprocessor • Thus it uses import instead of
include• Java also uses the package
statement instead of namespaces• Java has a larger role for final than
C++ does const
Copyright © 2015 Curt Hill
Final
• Java uses final instead of const• Thus a constant variable uses the
final declaration:final String VERSION = “1.1.4”;
• The final keyword may also be used in a class declaration:public final class some{…}– This indicates that no derivation may
be made of class some
Copyright © 2015 Curt Hill
Heap and Stack• C/C++ allows any type to be on
either the heap or stack• Java only allows primitives and
handles on the stack• All objects must be on the heap and
created with new• Java only allows dots and not the ->• Java does not allow delete, garbage
collection is the only way to claim unused heap memory
Copyright © 2015 Curt Hill
Pointers
• Java claims to have no pointers, it calls them handles instead– A handle is a restricted pointer
Copyright © 2015 Curt Hill
Annotations• Java annotations are a feature not
present in C/C++• Annotations given hints to the
compiler or run-time system• An annotation starts with the @
followed by a word which determines the type of annotation
• An annotation may be followed by a parenthesized list of parameters
• There are some standard Java annotations and some particular to Minecraft
Copyright © 2015 Curt Hill
Annotations Again
• @Override• @Mod • @Instance• @SidedMod• @EventHandler• Annotations are not terminated by
a semicolon
Copyright © 2015 Curt Hill
@Override
• Standard Java annotation• Tells the compiler that the method
that follows overrides a method in the ancestral class
• If there is no method that this overrides a compile error occurs• Usually this is a misspelled the name
or made a mistake on the signature • The compile error prevents a run-time
or logic errorCopyright © 2015 Curt Hill
@Mod
• Indicates to forge that this is the Base of a Mod class
• Three parameters:• modid – a unique ID for the mod• name – people readable name • version – the version
• All three are strings
Copyright © 2015 Curt Hill
@Instance
• The instantiation of the mod class• What follows is usually a static
instance of the mod class • See the following example for
@Mod and @Instance
Copyright © 2015 Curt Hill
Example
• Here is some sample code:@Mod(modid = “CurtModID”,, name = “CurtMod”, version = “1.0.2”)public class CurtMod { …@Instancepublic static CurtMod instance = new CurtMod();
Copyright © 2015 Curt Hill
@SidedMod• Announces which proxy is to be
loaded depending on whether a client or server is running– Precedes the declaration
• Two parameters:– clientProxy– serverProxy
• May not use anything but a literal string
• See next slide for example
Copyright © 2015 Curt Hill
Example
• Annotation and instance:@SidedProxy(serverSide= "edu.vscu.ServerProxy", clientSide= "edu.vscu.ClientProxy")public static CommonProxy proxy;
• CommonProxy was the ancestor of both ServerProxy and ClientProxy in this example
Copyright © 2015 Curt Hill
@EventHandler
• Indicates that the method that follows will be an event handler
• Recall that event handlers are typically not called directly by code in the same file
• Instead called unpredictably by system code– In Minecraft’s case usually by the
Forge or Minecraft itself
Copyright © 2015 Curt Hill
Classes
• The class definition is slightly different than C++
• Inheritance and polymorphism are as well
• These we will cover in their own presentation
Copyright © 2015 Curt Hill
Finally
• It is possible, even likely, that despite this presentation you will find features in mod code that are confusing
• Ask!
Copyright © 2015 Curt Hill